Solved

Calculate the time different in milisecond or microsecond

Posted on 2008-10-16
6
1,699 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
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.

691 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