Thread: Benchmarking

  1. #1
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37

    Benchmarking

    Hi, I've seen cases where people benchmark their programs, for example at the bottom of this page the author highlights the effect of copying strings in different ways and the effect on execution speed.

    HowStuffWorks "How C Programming Works"

    Does anyone know of any software to benchmark your programs, i don't plan on making any unnecessary confusing compact code, but I'd just like to see how different things effect speed. Hoping to find something that's f/oss and will run on the command line. Thanks.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Apparently the "stopwatch method" is a valid one, you can probably guess how that works.

    You can also use time() to do that:
    Code:
    #include <time.h>
    
    int main() {
         time_t start, end;
         double difference;
         start=time(NULL);
         [whatever you want to time]
         end=time(NULL);
         if (end != start) difference=difftime(end,start);
         printf("%d seconds\n",(int)difference);
         return 0;
    }
    This will give a duration in seconds, so to make it meaningful, you want to time something that takes enough time, especially if you are making a comparison.

    Have a look at this thread, where by the end I time 4.7 billion isdigit() calls in 2 seconds on a single 2.2Ghz processor. I didn't think anything could happen 4.7 billion times in 2 seconds, but there you go...

    There is also something called profiling, where you investigate which parts of a program take the most time. gprof, on linux, does this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    First you should make sure though that the code is correct.

    There are some problems: for example the pointer versions of strcpy fail to copy the null terminator, even though the author mentions it is important.

    Code:
    void strcpy(char *s1, const char *s2)
    { 
        while (*s2) //loop not entered if '\0'
            *s1++ = *s2++;
        *s1 = '\0'; //s1 needs terminating
    }
    The most condensed correct form would be:
    Code:
    void strcpy(char *s1, const char *s2)
    {
        while ( *s1++ = *s2++);
    }
    The character is first copied and then, if it turns out the character was '\0', the loop is exited.


    When I compile these three pieces of code on a MicroVAX with gcc, using no optimization...
    If I'm not mistaken, benchmarking unoptimized code doesn't allow you to draw any conclusions - particularly if you are interested in micro-optimizations like this.
    Last edited by anon; 06-18-2009 at 05:19 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    if you are on linux, use the command

    time ./[progname]

    or

    gprof ./[progname]

    for different information about the runtime

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You can also do stuff like this with grof:
    Code:
    [root~/C/functions] gcc -pg -fprofile-arcs -ftest-coverage -o factor factorial.c 
    Press any key to continue...
    [root~/C/functions] ./factor  X X X X X
    720
    /* "factor" uses the value of argc, eg, 6 = 720 */
    /* also, you must run the executable once to produce a dump for grof */
    
    [root~/C/functions] gprof -b factor
    Flat profile:
    
    Each sample counts as 0.01 seconds.
     no time accumulated
    
      %   cumulative   self              self     total           
     time   seconds   seconds    calls  Ts/call  Ts/call  name    
      0.00      0.00     0.00        1     0.00     0.00  factorial         /* recursion not apparent */
    
    
    			Call graph
    
    
    granularity: each sample hit covers 2 byte(s) no time propagated
    
    index % time    self  children    called     name
                                       5             factorial [1]
                    0.00    0.00       1/1           main [12]
    [1]      0.0    0.00    0.00       1+5       factorial [1]           /* here's the recursion */
                                       5             factorial [1]
    -----------------------------------------------
    
    
    Index by function name
    
       [1] factorial
    Then you can do this with gcov:
    Code:
    [root~/C/functions] gcov factorial.c
    File 'factorial.c'
    Lines executed:100.00% of 7
    factorial.c:creating 'factorial.c.gcov'
    
    [root~/C/functions] cat factorial.c.gcov
            -:    0:Source:factorial.c
            -:    0:Graph:factorial.gcno
            -:    0:Data:factorial.gcda
            -:    0:Runs:2
            -:    0:Programs:1
            -:    1:#include <stdio.h>
            -:    2:
            7:    3:int factorial (int n) {        /* recursion evident */
            7:    4:	if (n==1) return n;
            5:    5:	return n*factorial(n-1);
            -:    6:}
            -:    7:
            2:    8:int main(int argc, char **argv) {
            2:    9:	int x=factorial(argc);
            2:   10:	printf("%d\n",x);
            2:   11:	return 0;
            -:   12:}
    I included this because IMO it's hard to figure the "first step" to use these tools with the man page, but once you have the first step the man page makes sense. There are some crazy looking GUI front ends to this stuff which produce pie graphs and flow charts, but I haven't tried them...

    NB. that gcov output is cumulative, which is why the numbers are +1 (I actually ran factor once without args, eg, n=1, then once with 5 args, n=6).
    Last edited by MK27; 06-18-2009 at 07:50 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

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