C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-09-2008, 03:24 AM   #1
Registered User
 
Join Date: Jan 2008
Posts: 8
Angry Time in milliseconds

Hi,

I've searched the topic on google and this website, but i could not find the exact answer what i was looking for.

For a certain scenario in my application (timestamps related) i need time in milliseconds. What i am currently doing is to get time in seconds using time() function and convert it into milliseconds by multiplying it with 1000.

But the problem is that time_t is signed long (32 bit). When i multiply time_in_seconds with 1000, it overflows 32 bits and the resulting time in milliseconds becomes -ve.

Please tell me how can i get time in milliseconds ? Also, i don't want processor time taken, i want wallclock time.

OS: Unix


Thanx
coder_009 is offline   Reply With Quote
Old 05-09-2008, 03:29 AM   #2
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
If you only need time(), then cast it to unsigned long long, then multiply it by 1000. That way it won't overflow.

But... it seems you might need more precision if you're targeting milliseconds...
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 05-09-2008, 03:31 AM   #3
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Perhaps you can use long-long. In at least Linux, you have gettimeofday - it may exist in other Unix variants too. It gives you time in microseconds (although it may jump in steps of several microseconds at a time).

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 05-09-2008, 03:34 AM   #4
Registered User
 
Join Date: Jan 2008
Posts: 8
Isn't there any function on Unix or Linux that gives time from start of the process rather than the epoch? Such a long time period from 1970 to now makes it so big in magnitude.
coder_009 is offline   Reply With Quote
Old 05-09-2008, 03:36 AM   #5
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by coder_009 View Post
Isn't there any function on Unix or Linux that gives time from start of the process rather than the epoch? Such a long time period from 1970 to now makes it so big in magnitude.
Just take the time at the start of main, and then show the difference between that and "current time" when you want to show how long you've been running [or some such].

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Reply

Tags
milliseconds, time, unix

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Execution Time - Rijandael encryption gamer4life687 C++ Programming 5 09-20-2008 09:25 PM
Sending an email in C program Moony C Programming 28 10-19-2006 10:42 AM
Journey time prog 1 minute wrong mike_g C Programming 4 10-12-2006 03:41 AM
time class Unregistered C++ Programming 1 12-11-2001 10:12 PM


All times are GMT -6. The time now is 12:13 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22