Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Calculate the time different in milisecond or microsecond

Posted on 2008-10-16
6
Medium Priority
?
1,713 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 1200 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 400 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
Technology Partners: 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 400 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 1200 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

Technology Partners: 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!

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 how to use strings and some functions related to them in the C programming language.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

610 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