Thread: Program to characterize performance of C functions?

  1. #1
    Registered User stevespai's Avatar
    Join Date
    Jun 2006
    Posts
    23

    Program to characterize performance of C functions?

    Hi, has anyone ever written a program to characterize the speed of the various memory and stream functions in C? I'm not sure if this is the best way, but I am creating a timestamp by accessing the processor before I perform a function and after, then storing these timestamps.

    EX:

    Code:
             char local[MEM_SIZE];
             char *heap = NULL;
    	int i;
    	FILE *out;
    
    	InitMT();
    	heap = (char *)calloc(1, MEM_SIZE);
    
    	for (i = 0; i < NUM_TESTS; i++)
    	{
    		micro_time(&t[i][0]);
    		memset(local, 0, MEM_SIZE);
    		micro_time(&t[i][1]);
    		memset(heap, 0, MEM_SIZE);
    		micro_time(&t[i][2]);
    		memset(dataSeg, 0, MEM_SIZE);
    		micro_time(&t[i][3]);
    	}
    Code:
    #define NUM_TESTS	50000
    char dataSeg[MEM_SIZE];
    MICRO_TIME_TYPE t[NUM_TESTS][4];
    Has anyone done something simliar? Is there a better way?

    BTW: micro_time will take the argument and set it to the current time.

    Thank You.
    Steve
    Last edited by stevespai; 08-11-2006 at 03:08 PM.

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Don't involve the timing function so much, instead, move it outside the loop. This will help account for timers that don't have a lot of precision (if say, the function is a really fast one). Example:
    Code:
    start_time = get_time_now();
    for(x = 0; x < NUM_TESTS; ++x)
    {
      run_function_to_test();
    }
    end_time = get_time_now();
    Then, average_time = (end_time - start_time) / NUM_TESTS. This of course involves the loop in the timing, but if you're doing 50000 tests then it should be trivial. This also saves you from using 400KB or more just to save timing info, and makes the final calculations easier and (imho) more accurate. I've used this sort of thing myself.

    Unless you're under some weird system, memset() is probably going to fair about the same for all those places, and will depend if any swapping has to occur (which would slow it down, obviously).
    Last edited by Cactus_Hugger; 08-11-2006 at 05:37 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    You said profiler and I remembered that my IDE doesn't have it. VC++6 had but 2005 doesn't. Only team work version has it.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    Registered User stevespai's Avatar
    Join Date
    Jun 2006
    Posts
    23
    Great, thanks for the responses. Let me look into these methods.

    Steve

  6. #6
    Registered User stevespai's Avatar
    Join Date
    Jun 2006
    Posts
    23
    Cactus_Hugger: thanks again for the great advice

    Salem: thanks for the helpful link

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. Help starting a C program with functions
    By jlmac2001 in forum C Programming
    Replies: 6
    Last Post: 10-12-2002, 02:43 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM