Thread: Speed test result

  1. #1
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    Speed test result

    Here is speed test result for malloc, realloc and calloc at Dual Core @1.86Ghz, 1.75 GB RAM...

    Code:
    malloc + free:
    Pass 1: 1359 ms
    Pass 2: 1391 ms
    Pass 3: 1359 ms
    Pass 4: 1344 ms
    Pass 5: 1359 ms
    Pass 6: 1328 ms
    Pass 7: 1344 ms
    Pass 8: 1344 ms
    Pass 9: 1375 ms
    Pass 10: 1359 ms
    Average: 1356.20 ms
    
    realloc(NULL) + free: //similar to malloc
    Pass 1: 1344 ms
    Pass 2: 1406 ms
    Pass 3: 1328 ms
    Pass 4: 1329 ms
    Pass 5: 1343 ms
    Pass 6: 1344 ms
    Pass 7: 1328 ms
    Pass 8: 1344 ms
    Pass 9: 1328 ms
    Pass 10: 1328 ms
    Average: 1342.20 ms
    
    calloc + free:
    Pass 1: 4734 ms
    Pass 2: 4672 ms
    Pass 3: 4641 ms
    Pass 4: 4609 ms
    Pass 5: 4641 ms
    Pass 6: 4672 ms
    Pass 7: 4672 ms
    Pass 8: 4640 ms
    Pass 9: 4625 ms
    Pass 10: 4625 ms
    Average: 4653.10 ms
    
    realloc:
    Pass 1: 656 ms
    Pass 2: 672 ms
    Pass 3: 656 ms
    Pass 4: 735 ms
    Pass 5: 671 ms
    Pass 6: 688 ms
    Pass 7: 656 ms
    Pass 8: 672 ms
    Pass 9: 656 ms
    Pass 10: 672 ms
    Average: 673.40 ms
    Source-
    Code:
    int *test = NULL; //int sequence
    
    DWORD start = 0;
    
    unsigned int i, j, total = 0;
    
    for(i=1; i<=10; i++)
    {
        start = GetTickCount();
    
        for(j=1; j<=320000; j++)
        {
            /*
            test = calloc(j, sizeof(int));
            free(test);
            */
    
            /*
            test = malloc(j * sizeof(int));
            free(test);
            */
    
            /*
            test = realloc(NULL, j * sizeof(int)); //same as malloc
            free(test);
            */
    
            /*
            test = malloc(j * sizeof(int));
            free(test);
            */
    
            /*
            test = realloc(test, j * sizeof(int));
            */
        }
    
        total += (GetTickCount() - start);
    
        printf("Pass %d: %d ms\n", i, (unsigned int)(GetTickCount() - start));
    }
    
    //free(test); //for realloc(test, j* sizeof(int));
    
    printf("Average: %.2f ms\n", (float)total / (float)10);
    
    return 0;
    Is there portable version of GetTickCount? I tried _ftime.millis but it show annoying result (negative numbers)...

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    clock()

    ssharish

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    I'm not sure what, precisely, GetTickCount() does, but based on a guess, there is a standard function called clock() which is portable. How effective it is on a particular system, of course, varies...

    By the bye, one reason you may get negative numbers is that you're using %d in printf(). %d means signed int. If you're passing an unsigned value (such as in your code when you cast), it will interpret it as signed and possibly print out as a negative number.

    Anyhow, here's a version of your program using clock():
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
      int *test;
      clock_t start, total = 0;
    
      long i, j;
    
      for(i = 0; i < 10; i++)
      {
        start = clock();
    
        for(j = 0; j < 320000; j++)
        {
          /*
             test = calloc(j, sizeof(int));
             free(test);
           */
    
          /*
             test = malloc(j * sizeof(int));
             free(test);
           */
    
          /*
             test = realloc(NULL, j * sizeof(int)); //same as malloc
             free(test);
           */
    
          /*
             test = malloc(j * sizeof(int));
             free(test);
           */
    
          /*
             test = realloc(test, j * sizeof(int));
           */
        }
    
        total += clock() - start;
    
        printf("Pass %ld: %.2f s\n", i + 1, (total - start) / (double)CLOCKS_PER_SEC);
      }
    
      //free(test); //for realloc(test, j* sizeof(int));
    
      printf("Average: %.2f s\n", (total / 10.0) / CLOCKS_PER_SEC);
    
      return 0;
    }

  4. #4
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Thanks cas, that very helpful ^_^!

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Also, it would probably be a good idea to test free and malloc separately (so, allocate 32k blocks of memory, then free all of them in a separately timed sequence).

    Your test also shows that realloc probably doesn't actually copy the content each time - it would be slower than calloc() if it did.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Integer Emulation
    By Elysia in forum C++ Programming
    Replies: 31
    Last Post: 03-18-2008, 01:03 PM
  2. Point in polygon test - spherical coords
    By bhdz in forum C Programming
    Replies: 1
    Last Post: 11-07-2007, 01:25 PM
  3. Replies: 8
    Last Post: 10-18-2006, 03:14 PM
  4. I need some help with my C progam
    By ces4u in forum C Programming
    Replies: 2
    Last Post: 12-17-2005, 10:05 AM
  5. Wrong declaration, Right result ?
    By The SharK in forum C++ Programming
    Replies: 14
    Last Post: 10-24-2005, 01:11 PM