Value initialization vs Calloc vs Manual initialization speed

Which is the fastest?

I tried to check the speed of three methods in the base 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:

Initialization of the value Duration: 541

Calloc Initialization Duration: 493

Manual initialization Duration: 3424

But I have a few problems with my current test:

  • I don't know if I correctly isolated three different initialization methods
  • I have not tested all methods of initializing an array of zeros (memset and malloc, which, I suspect, work like calloc)
  • Results in seconds (ew!) That are terribly quantized. (No time ms?)
  • ARR_SIZ VS'12 (0x7FFFFFFF). , , , bad_alloc , .
  • , , , .

, , .

- ? , - , ?

:

: , clock(). .

+4
1

, calloc , , , , . malloc'd 0 , , . calloc'd ( , ), .

. . . , . "". , , . , malloc'd, , .

- . , , 0 , calloc.

, . 0.

+2

All Articles