Advertisement
jacknpoe

malloc(), calloc() and realloc() benchmarks.

Oct 25th, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <time.h>
  4.  
  5. #define ITERATIONS 800000
  6.  
  7. int main() {
  8.     clock_t clock1, clock2;
  9.     char *buffer;
  10.  
  11.     std::cout.precision(15);
  12.  
  13.     std::cout << "Resolution: " << CLOCKS_PER_SEC << " ticks per second." << std::endl;
  14.  
  15.     clock1 = clock();
  16.     for( int index = 0; index <= ITERATIONS; index++)
  17.     {
  18.         buffer = (char *) malloc( index+1);
  19.         buffer[index] = index;
  20.         free( buffer);
  21.     }
  22.     clock2 = clock();
  23.  
  24.     std::cout << "Malloc: " << double( (clock2 - clock1 )) / CLOCKS_PER_SEC << "s" << std::endl;
  25.  
  26.     clock1 = clock();
  27.     for( int index = 0; index <= ITERATIONS; index++)
  28.     {
  29.         buffer = (char *) calloc( index+1, 1);
  30.         buffer[index] = index;
  31.         free( buffer);
  32.     }
  33.     clock2 = clock();
  34.  
  35.     std::cout << "Calloc: " << double( ( clock2 - clock1 )) / CLOCKS_PER_SEC << "s" << std::endl;
  36.  
  37.     buffer = (char *) malloc( 1);
  38.     clock1 = clock();
  39.     for( int index = 0; index <= ITERATIONS; index++)
  40.     {
  41.         buffer = (char *) realloc( buffer, index+1);
  42.         buffer[index] = index;
  43.     }
  44.     free( buffer);
  45.     clock2 = clock();
  46.  
  47.     std::cout << "Realloc: " << double( (clock2 - clock1 )) / CLOCKS_PER_SEC << "s" << std::endl;
  48.  
  49.     clock1 = clock();
  50.     for( int index = 0; index <= ITERATIONS; index++)
  51.     {
  52.         buffer = (char *) malloc( ( index+1) * sizeof(long) );
  53.         buffer[index] = index;
  54.         free( buffer);
  55.     }
  56.     clock2 = clock();
  57.  
  58.     std::cout << "Malloc 2: " << double( (clock2 - clock1 )) / CLOCKS_PER_SEC << "s" << std::endl;
  59.  
  60.     clock1 = clock();
  61.     for( int index = 0; index <= ITERATIONS; index++)
  62.     {
  63.         buffer = (char *) calloc( index+1, sizeof(long));
  64.         buffer[index] = index;
  65.         free( buffer);
  66.     }
  67.     clock2 = clock();
  68.  
  69.     std::cout << "Calloc 2: " << double( ( clock2 - clock1 )) / CLOCKS_PER_SEC << "s" << std::endl;
  70.  
  71.     buffer = (char *) malloc( sizeof(long));
  72.     clock1 = clock();
  73.     for( int index = 0; index <= ITERATIONS; index++)
  74.     {
  75.         buffer = (char *) realloc( buffer, (index+1) * sizeof(long));
  76.         buffer[index] = index;
  77.     }
  78.     free( buffer);
  79.     clock2 = clock();
  80.  
  81.     std::cout << "Realloc 2: " << double( (clock2 - clock1 )) / CLOCKS_PER_SEC << "s" << std::endl;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement