0

I'm trying to find out the time it takes for a function to execute in C. What i'm trying is the following:

#include <time.h> time_t start; time_t finish; double seconds; time(&start); FUNCTION time(&finish); seconds = difftime(finish,start); printf("Time taken is %.f", seconds); 

However, the returned value is always the same for different functions: 1389133144 seconds.

Any help? Thanks!

7
  • 1
    Have you confirmed the retval from time()? Commented Jan 7, 2014 at 22:25
  • Do you get any compile warning? If you're not including stdio.h then printf may be implicitly prototyped to take int arguments and casting your doubles lossily. Commented Jan 7, 2014 at 22:26
  • @tommyo what do you mean? shouldn't time() give you the current calendar time? Commented Jan 7, 2014 at 22:28
  • @evilotto no i included stdio.h as well, i simply didnt put them in this code. No compiler errors at all. Commented Jan 7, 2014 at 22:29
  • 1
    Please paste your real code. the value you are getting - 1389133144 - is suspiciously close to the current unix time. Commented Jan 7, 2014 at 22:37

3 Answers 3

1

Other approach I commonly use is something like this:

#include <sys/time.h> //this is from http://www.cs.usfca.edu/~peter/ipp/ #define GET_TIME(now) { \ struct timeval t; \ gettimeofday(&t, NULL); \ now = t.tv_sec + t.tv_usec/1000000.0; \ } //in your code double start, end; GET_TIME(start); //... do some stuff... GET_TIME(end); printf("TOTAL TIME = %f secs\n", end-start); 
Sign up to request clarification or add additional context in comments.

5 Comments

i'm using your method, but now, all i get is 0 seconds all the time
strange, i've tested right now putting a for loop (for(int i=0;i<1000;i++) in the ...do some stuff part and i get TOTAL TIME = 0.000015 secs. Probably you need to paste your entire code to see if something is causing this strange behavior
If you get 0 may be it's correct and your function takes no time to run
My bad! I multiplied both finish and start by 1000000 and got a result :) thanks a lot!
I tend to avoid doing the computation in the GET_TIME macro; I minimize the extraneous activity between the first and second call. Otherwise, I do pretty much as you're doing. Beware that this can be confused/confusing if the system clock is adjusted (perhaps by NTP or SNTP daemons) while your test is running. If you have clock_gettime() you may be able to use CLOCK_MONOTONIC to avoid such issues.
1

The time function returns the number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). If the argument is non-NULL, it also stores the time in it.

1389133144 is actually a pretty reasonable timestamp, it is 2014-01-07 22:19:04.

However, the time function is not a very good tool to measure the run time of a function, since the precision is very low. Consider using gettimeofday, clock_gettime or simply clock, all of which should be more precise.

Comments

0

The problem isn't in the time calculations, its in the format specifier for the printf() statement. You are using "%.f", where you probably want "%f". The . means precision in this context.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.