Solved

Time Elapsed

Posted on 2003-10-25
11
356 Views
Last Modified: 2012-05-04
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;

      time(&timer1);
      fflush(stdin);
      getchar();

      time(&timer2);

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


      return EXIT_SUCCESS;
}

I wish to return in actual millisecond. How to do that?
0
Comment
Question by:cutie2000
11 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 9619743
difftime returns the elapsed time in seconds.

printf("%lf", (long)((difftime(timer2, timer1))*1000.0));
0
 
LVL 8

Accepted Solution

by:
mnashadka earned 50 total points
ID: 9619928
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.

Unix/Linux:
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;
  --seconds_diff;
}
else
{
  microseconds_diff = end.tv_usec - start.tv_usec;
}

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

Windows:
DWORD start = GetTickCount();
... // Do the processing
DWORD difference = GetTickCount() - start;
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 9619935
Sorry, that was sort of C++ style; move the variable declarations to the top of the function.
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:cutie2000
ID: 9621297
what header file do i need to include for the linux code?
i tried using time.h but cannot work
0
 

Author Comment

by:cutie2000
ID: 9621314
ok i included stdlib.h and it solve the compilation error
0
 

Author Comment

by:cutie2000
ID: 9621362
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
0
 

Author Comment

by:cutie2000
ID: 9622356
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++ ) {
            dummy=i-1.0/sqrt(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
0
 

Author Comment

by:cutie2000
ID: 9622357
Do I have to do this?
return (float)((c2-c1)/CLOCKS_PER_SEC/1000);
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 9622555
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.
0
 
LVL 6

Expert Comment

by:Ajar
ID: 9625712
#include <sys/timeb.h>
int main(void)
{
     struct timeb      t1,t2;
     ftime (&t1);
     fflush(stdin);
     getchar();
     ftime (&t2);
     printf("MILLISECONDS ELAPSED ARE %d\n", (t2.time +1000*t2.millitm) -(t1.time +1000*t1.millitm));
     return EXIT_SUCCESS;
}
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 9627273
From the man pages of ftime:
BUGS
       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.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question