Thread: how to measure time consumed at runtime

  1. #1
    Eager young mind
    Join Date
    Jun 2006

    how to measure time consumed at runtime

    Sorry, let me rephrase the question i had in mind...
    the time <command> gives the time taken to execute the "command"..
    but , i am required to find out the time between two particular instances during the execution of the program.
    i tried clock() , which gives me the number of clock cycles upto that point
       # define <time.h>
       clock_t  time1,time2;
         /*   codes
    but, it shows both time1 and time2 as being equal to 0.000000
    how else should i go about ?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    A profiler would be your best option.

    >but, it shows both time1 and time2 as being equal to 0.000000
    Yea, your code is probably pretty darn fast. Most of the time to get the clock trick to work, you need to repeat the code multiple times:
    start = clock();
    for ( i = 0; i < 10000; i++ ) {
      /* Code you want to test here */
    /* Print the result here */
    Then once you have a result, divide it by the number of iterations and you have the result for a single run.
    My best code is written with the delete key.

  3. #3
    Eager young mind
    Join Date
    Jun 2006
    well, i got around that.. I am actually doing MPI programming.. so, even in my serial program ,i included the path to mpi so that include MPI_Wtime() that gives me the time correctly..
    what was that profiler u were talking about?
    are there better methods of doing this other than clock()?

  4. #4
    Registered User
    Join Date
    Mar 2006
    Use an external program called a profiler. It typically tracks overhead associated wiith various actions your program makes, and the amounts of time spent in various functions. GNU gprof does a pretty good job at this, but it's nowhere near some of the commercial-grade ones which give very accurate timings.

    Using time() will give a very inaccurate answer unless you perform the benchmark for minutes on end. Some APIs provide functions which can give the time to the millisecond, or even microsecond, if you're lucky. Combine it with Prelude's repeat test example and you should be able to derive fairly accurate timings quickly.
    #include <stdio.h>
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    For those with pentium processors, use the fast clock (rdtsc)
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get current time
    By tsubasa in forum C Programming
    Replies: 3
    Last Post: 05-01-2009, 02:03 AM
  2. time measure (net and gross) with pthreads under linux
    By mynickmynick in forum Linux Programming
    Replies: 12
    Last Post: 12-01-2008, 07:39 AM
  3. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-03-2006, 12:46 AM
  4. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  5. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 09:08 AM