Calculate the time different in milisecond or microsecond

I like to do the job above and i know time_t, unfortunately, in only result in second, and gettickcount() is for windows.
Anyone has the solution to do in Linux. Thanks in advance.
vietpaAsked:
Who is Participating?
 
Infinity08Commented:
So, something like the following to get similar behavior as GetTickCount on Windows :

unsigned int getTickCount() {
  return ((clock() * 1000) / CLOCKS_PER_SEC);
}

Open in new window

0
 
Infinity08Commented:
>> gettickcount() is for windows.

You can use the standard clock() which will return the number of elapsed clock ticks :

        http://www.cplusplus.com/reference/clibrary/ctime/clock.html

Its granularity is generally a few milliseconds though. You can use the value CLOCKS_PER_SEC to know its granularity.
0
 
itsmeandnobodyelseCommented:
>>>> time_t, unfortunately, in only result in second

There is a timeb struct counting milliseconds. You may retrieve the current timeb calling ftime:

#include <sys/timeb.h>

   ...
   struct timeb tb = { 0 };
   ftime(&tb);

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
larsgaCommented:
If available, you should use clock_gettime()/clock_getres() instead of ftime(). http://www.opengroup.org/onlinepubs/009695399/functions/clock_gettime.html
0
 
vietpaAuthor Commented:
@Infinity08: it seems works, may be the time different is to small, how to increase to microsec ?
0
 
Infinity08Commented:
The problem is that the system clock does not have a sufficient resolution to arrive at microsecond precision.

However, you can take a look at usleep for example :

        http://linux.die.net/man/3/usleep

(notice the "at least" in the description, which is partly due to this same reason)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.