Solved

Calculate the time different in milisecond or microsecond

Posted on 2008-10-16
6
1,675 Views
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.
0
Comment
Question by:vietpa
6 Comments
 
LVL 53

Expert Comment

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

Accepted Solution

by:
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

0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
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 };
   ftime(&tb);

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Assisted Solution

by:larsga
larsga earned 100 total points
ID: 22734931
If available, you should use clock_gettime()/clock_getres() instead of ftime(). http://www.opengroup.org/onlinepubs/009695399/functions/clock_gettime.html
0
 

Author Comment

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

Assisted Solution

by:Infinity08
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 :

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

(notice the "at least" in the description, which is partly due to this same reason)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now