Thread: benchmarking

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    7

    benchmarking

    I guess this is a very broad question, but I'm looking for information on how I can benchmark my code. How do you guys check to see how long it takes to execute a given block of statements? How do you determine that code written one way is faster than it is another way? Any general advice and/or tips would be appreciated. Thanks.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Get the time before you start the block of code, and get the time after you've finished the block of code. Subtract A from B, and there you have it. The method of getting times and their differences depends on the operating system you are using though. On a windows computer, one function you can use is timeGetTime().

  3. #3
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main ()
    {
      time_t start,end;
      char string[20];
      double dif;
    
      time (&start);
      printf ("Insert string : ");
      scanf("%s", &string);
      time (&end);
      dif = difftime (end,start);
      printf ("\nFor inserting string you need %2lf seconds\n", dif );
    
      return 0;
    }
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  4. #4
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    That example will only give you time in seconds, which wouldn't be very effective for benchmarking. You'll want to measure it in milliseconds.

    Using ftime would be much more effective.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    You can use gprof, which is available in Linux. you can goggle gprof to learn how to use, which is quite easy.

    It basically records the time taken by each of the functions in your problem

  6. #6
    .
    Join Date
    Nov 2003
    Posts
    307
    For windows, you buy/find a good profiling tool, in unix you use one that is part of the standard toolset, like gprof or prof. Compile with profiling and debugging (symbol table) enabled.

    run the program.

    You get output like this (mangled names have an underline _ character in front of them):
    For the OS this runs on the $$ signified millicode.

    Code:
     %Time Seconds Cumsecs  #Calls   msec/call  Name
    
      14.5    0.43    0.43                      $$mulI
      14.2    0.42    0.85                      $$remoI
      10.7    0.32    1.17                      _mcount
       9.6    0.28    1.45  600000        0.00  gnustrstr
       7.1    0.21    1.66  900000        0.00  bld
       6.2    0.18    1.85  600000        0.00  mstrstr
       5.1    0.15    2.00  300000        0.00  bld_good
       4.4    0.13    2.13 1212702        0.00  _rand
       4.4    0.13    2.26  600000        0.00  _strstr
       4.4    0.13    2.39                      __strlen20
       4.0    0.12    2.51 1200000        0.00  strlen
       3.7    0.11    2.62       1      110.00  main
       3.3    0.10    2.72                      _monstartup
       1.7    0.05    2.77  303360        0.00  _strncmp
       1.7    0.05    2.82                      __strncmp20
       0.7    0.02    2.84                      __strcpy20
       0.1    0.00    2.84                      $$divU
       0.1    0.00    2.84                      __rand_r_posix

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    14
    A similar profiling function is available in MS Visual C++ 6, you enable it in the project options menu and then profile the application to view a breakdown of the functions which use the most time or processing power and various other parameters can be identified. There is no point in optimising some piece of code which isn’t slow to start with or the application spends little time in.
    Currently Reading:

    Mathematic from the birth of numbers,
    Effective TCP/IP programming,
    Data Compression: The Complete Reference,
    C Interfaces and Implementations: Techniques for Creating Reusable Software,
    An Introduction to Genetic Algorithms for Scientists and Engineers.

  8. #8
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49
    does ftime work in windows?
    "Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life."

    "They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance."

    "Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it."
    [all]- Terry Pratchett

  9. #9
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    Yes ftime will work in Windows I think. I do this code!
    Tell me is this good code for ftime! Is that that!

    Code:
    #include <stdio.h>                                            
    #include <dos.h>                                              
    #include <sys\timeb.h>                                       
                                                                  
    void main(void)                                               
    {                                                            
       struct timeb t_start, t_end;                           
       char array[80]; char ch;
       int i;
       int t_diff;    
                                                   
       ftime(&t_start);
       while( (ch = getchar()) != '\n')
       {
              array[i] = ch;
              i++;    
       }                                          
       ftime(&t_end);
       
       t_diff =  (int) (1000*(t_end.time - t_start.time)
            + (t_end.millitm - t_start.millitm));
            
       printf("milliseconds   %d", t_diff);
       
       return 0;                                   
    }
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Benchmarking
    By redruby147 in forum C Programming
    Replies: 4
    Last Post: 06-18-2009, 07:44 AM
  2. Benchmarking c compilers
    By freevryheid in forum C Programming
    Replies: 6
    Last Post: 01-23-2009, 05:41 PM
  3. benchmarking?
    By cs32 in forum C Programming
    Replies: 5
    Last Post: 02-14-2008, 07:37 AM
  4. Benchmarking and Optimisation Q's
    By studiesrule in forum C++ Programming
    Replies: 11
    Last Post: 10-19-2006, 07:57 AM
  5. Benchmarking
    By RoD in forum Tech Board
    Replies: 5
    Last Post: 10-14-2002, 02:04 PM