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.
Who is Participating?
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

>> gettickcount() is for windows.

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

Its granularity is generally a few milliseconds though. You can use the value CLOCKS_PER_SEC to know its granularity.
>>>> 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 };

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.

If available, you should use clock_gettime()/clock_getres() instead of ftime().
vietpaAuthor Commented:
@Infinity08: it seems works, may be the time different is to small, how to increase to microsec ?
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 :

(notice the "at least" in the description, which is partly due to this same reason)
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.