Calculate the time different in milisecond or microsecond

Posted on 2008-10-16
Last Modified: 2012-05-05
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.
Question by:vietpa
LVL 53

Expert Comment

ID: 22732501
>> 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.
LVL 53

Accepted Solution

Infinity08 earned 300 total points
ID: 22732549
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

LVL 39

Assisted Solution

itsmeandnobodyelse earned 100 total points
ID: 22732943
>>>> 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 };

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.


Assisted Solution

larsga earned 100 total points
ID: 22734931
If available, you should use clock_gettime()/clock_getres() instead of ftime().

Author Comment

ID: 22737173
@Infinity08: it seems works, may be the time different is to small, how to increase to microsec ?
LVL 53

Assisted Solution

Infinity08 earned 300 total points
ID: 22738179
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)

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

772 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