Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1717
  • Last Modified:

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.
4 Solutions
>> 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.
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

>>>> 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 };

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now