简体   繁体   English

值初始化与Calloc对手动初始化速度

[英]Value Initialization vs Calloc vs Manual Initialization Speed

Which is the fastest? 哪个是最快的?

I tried to test the speeds of the three methods in a basic capacity with this: 我尝试用这个来测试基本容量中三种方法的速度:

#include "stdafx.h"
#include "stdlib.h"
#include "stdio.h"
#include "time.h"

int _tmain(int argc, _TCHAR* argv[])
{
  const unsigned long long ARR_SIZ = 0x4fffffff;
  clock_t val_init_dur, calloc_dur, manual_dur;
  clock_t cur = clock();
  char* val_init = new char[ARR_SIZ]();
  clock_t after = clock();
  val_init_dur = after-cur;
  delete[] val_init;

  cur = clock();
  void* calloc_init = calloc(ARR_SIZ, sizeof(char));
  after = clock();
  calloc_dur = after-cur;
  free(calloc_init);

  cur = clock();
  char* manual_init = new char[ARR_SIZ];
  for (unsigned long i=0; i < ARR_SIZ; i++)
    manual_init[i] = 0;
  after = clock();
  manual_dur = after-cur;
  delete[] manual_init;

  printf("Value Initialization Duration: %d\n", val_init_dur);
  printf("Calloc Initialization Duration: %d\n", calloc_dur);
  printf("Manual Initialization Duration: %d\n", manual_dur);
  fgetc(stdin);
  return 0;
}

My results were: 我的结果是:

Value Initialization Duration: 541 值初始化持续时间:541

Calloc Initialization Duration: 493 Calloc初始化持续时间:493

Manual Initialization Duration: 3424 手动初始化持续时间:3424

But I have several issues with my current test: 但是我目前的测试有几个问题:

  • I don't know if I properly isolated the three different methods of initialization 我不知道我是否正确隔离了三种不同的初始化方法
  • I didn't test all methods of initializing an array of zeros (memset and malloc, which I suspect work like calloc) 我没有测试所有初始化零数组的方法(memset和malloc,我怀疑它的工作方式类似于calloc)
  • The results are in seconds (ew!) which are horribly quantized. 结果是以秒为单位(ew!),这些都是非常量化的。 (No ms time?) (没有时间?)
  • The value for ARR_SIZ is not the maximum permitted size by VS '12 (0x7FFFFFFF). ARR_SIZ的值不是VS '12(0x7FFFFFFF)允许的最大大小。 I was unable to set the value any higher than what is in the code above as I was getting bad_alloc exceptions from the first new call, despite the code compiling. 我无法将值设置为高于上面代码中的值,因为我从第一个新调用获得bad_alloc异常,尽管代码编译。
  • I suspect that there is a faster way to manually initialize an array via iteration than how I did it 我怀疑通过迭代手动初始化数组的方法比我做的更快

I italicized one of the bullet points above because I am very curious as to why that is the case. 我把上面的一个要点用了斜体,因为我很好奇为什么会这样。

Does anyone have advice how to improve my test code? 有没有人有如何改进我的测试代码的建议? Or even better, does anyone have a straightforward answer to the initial question of which is fastest? 或者甚至更好,是否有人能够直接回答最快的问题?

Also: I have turned OFF compiler optimization 另外:我关闭了编译器优化

Edit: I updated my code to use clock(). 编辑:我更新了我的代码以使用clock()。 Results updated as well. 结果也更新了。

The tests are not equivalent because using calloc and maybe value initialization, does not actually 0 the memory until it is touched. 测试不等同,因为使用calloc和可能的值初始化,在触摸之前实际上并不是0。 Whereas your manual setting of the malloc'd memory to 0 causes it to be touched and therefore allocated. 而您将malloc内存手动设置为0会导致触摸并因此分配它。 Try reading each element of the value and calloc'd memory and (assuming optimizer doesn't eliminate read because value not used) you should get the same results. 尝试读取值和calloc内存的每个元素(假设优化器不会因为未使用的值而消除读取),您应该得到相同的结果。

It does depends on OS. 它确实取决于操作系统。 A simpler OS may not do it this way. 更简单的操作系统可能不会这样做。 Often memory is divided into pages. 通常将记忆分为几页。 Commonly a page is not actually allocated by the OS until it is touched by the program. 通常,在程序触摸之前,操作系统实际上不会分配页面。 It is only "reserved". 它只是“保留”。 This is to speed up programs which don't end up using all the memory they allocate. 这是为了加速程序,这些程序最终不会使用它们分配的所有内存。 If you remove the code which modifies the malloc'd memory, maybe it will speed up. 如果删除修改malloc内存的代码,可能会加快速度。

To clarify - all this happens behind the scenes by the OS. 澄清 - 所有这些都发生在操作系统的幕后。 Your program is totally ok to assume the values are ready at 0 at any time, from the value-initialization and calloc. 从值初始化和calloc开始,你的程序可以随时假设值在0处就绪。

Personally I would use vector for all dynamic arrays unless a special case. 就个人而言,除非是特殊情况,否则我会将vector用于所有动态数组。 It will set values to 0 by default. 它默认将值设置为0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM