• C

Time Elapsed

I have this code that will return in seconds between starting of the program and the time when I do a RETURN.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
      time_t timer1, timer2;



      printf("%lf", difftime(timer2, timer1));

      return EXIT_SUCCESS;

I wish to return in actual millisecond. How to do that?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

difftime returns the elapsed time in seconds.

printf("%lf", (long)((difftime(timer2, timer1))*1000.0));
If you're on Unix/Linux, you can get a smaller granularity using the gettimeofday function.  In Windows, GetTickCount returns the number of milliseconds that have elapsed.

struct timeval start, end;
gettimeofday(&start, NULL);
... // Do some processing
gettimeofday(&end, NULL);

int seconds_diff = end.tv_sec - start.tv_sec;
int microseconds_diff;
if(end.tv_usec < start.tv_usec)
  microseconds_diff = 1000000 + end.tv_usec - start.tv_usec;
  microseconds_diff = end.tv_usec - start.tv_usec;

int milliseconds = (microseconds_diff / 1000) + (seconds_diff * 1000);

DWORD start = GetTickCount();
... // Do the processing
DWORD difference = GetTickCount() - start;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sorry, that was sort of C++ style; move the variable declarations to the top of the function.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

cutie2000Author Commented:
what header file do i need to include for the linux code?
i tried using time.h but cannot work
cutie2000Author Commented:
ok i included stdlib.h and it solve the compilation error
cutie2000Author Commented:
mnashadka, this is actually what I want.

int main()
int start, end;

start = getMilliSec();

//do some processing

end = getMilliSec();

printf("Diff in milli = %d\n", b-a);


int getMilliSec()

I am unable to modify your code to do the above.
Please help
cutie2000Author Commented:
Just wanna ask.
I have this code.
Am I returning the correct value?
Can someone test it out for me?

I wish to get CPU usage from the for loop in millisec.
Are my functions doing fine?

#include <stdio.h>
#include <time.h>
#include <math.h>

clock_t get_clock();
float clock_diff(clock_t c1, clock_t c2);

int main() {
      int i;
      double dummy;
      float clk_diff;
      clock_t begin, end;
      printf("\tperform some computation...\n");
      begin = get_clock();
      for ( i=0; i<10000001; i++ ) {
      end = get_clock();
      printf("\tcomputation done\n");

      clk_diff = clock_diff(begin, end);
      printf("Time take for computation: %f\n", clk_diff);
      return 0;

/* get current CPU clock time */
clock_t get_clock() {
      return clock();

/* get CPU clock difference */
float clock_diff(clock_t c1, clock_t c2) {
      return (float)((c2-c1)/CLOCKS_PER_SEC);

Please reply.
Thanks in advance
cutie2000Author Commented:
Do I have to do this?
return (float)((c2-c1)/CLOCKS_PER_SEC/1000);
My example does give you the milliseconds that are different.  I think that's the best way to go, since if you do it in milliseconds and store it in an int or long, it's possible to go out of range of an int or long.  Of course, you could use an unsigned long long.  But to convert it to milliseconds, it would look like:

int milliseconds = (end.tv_usec / 1000) + (end.tv_sec * 1000);

Good luck.
#include <sys/timeb.h>
int main(void)
     struct timeb      t1,t2;
     ftime (&t1);
     ftime (&t2);
     printf("MILLISECONDS ELAPSED ARE %d\n", (t2.time +1000*t2.millitm) -(t1.time +1000*t1.millitm));
     return EXIT_SUCCESS;
From the man pages of ftime:
       This function is obsolete. Don't use it. If the time  in  seconds  suf-
       fices,   time(2)  can  be  used;  gettimeofday(2)  gives  microseconds;
       clock_gettime(3) gives nanoseconds but is not yet widely available.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.