|
Home > Archive > Unix Programming > May 2005 > clock() wrap around
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
clock() wrap around
|
|
|
| I have problems with clock_t wrap around in GNU/Linux. Is there some
alternative to clock()/clock_t that doesn't wrap around after 72
minutes on 32 bit systems? Or is there some standard hack to circumvent
the wrap around?
If I'm posting this to the wrong ng please let me know.
TIA!
| |
| Pascal Bourguignon 2005-05-14, 1:23 pm |
| "jEL" <emanuel.landeholm@gmail.com> writes:
> I have problems with clock_t wrap around in GNU/Linux. Is there some
> alternative to clock()/clock_t that doesn't wrap around after 72
> minutes on 32 bit systems? Or is there some standard hack to circumvent
> the wrap around?
Nothing prevents clock_t to be defined as long long, even on 32-bit
systems.
If the libc you're using doesn't help, then the program can track how
many wrap-arrounds occured, by periodically checking if the new
clock() is less than the previous.
--
__Pascal Bourguignon__ http://www.informatimago.com/
This is a signature virus. Add me to your signature and help me to live
| |
|
| > Nothing prevents clock_t to be defined as long long, even on 32-bit
systems.
Right.
>If the libc you're using doesn't help, then the program can track how
many wrap-arrounds occured, by periodically checking if the new
clock() is less than the previous.
GNU libc is not helping, no. I ended up doing something like the below:
--8<--
long long msecs;
long long last, start, current;
int clockwraps;
...
last = start = clock();
clockwraps = 0;
...
/* hack to circumvent 32 bit wrap around */
current = clock();
if(current < last)
++clockwraps;
last = current;
current += clockwraps * (1LL << 32);
msecs = (1000 * (current - start)) / (CLOCKS_PER_SEC);
--8<--
I'm not entirely convinced of the correctness of my solution. I suppose
I'll find out if it works in 72 minutes but I'd gladly accept any
comments on the code.
| |
| Dmitry Mityugov 2005-05-14, 1:23 pm |
| jEL wrote:
>I have problems with clock_t wrap around in GNU/Linux. Is there some
> alternative to clock()/clock_t that doesn't wrap around after 72
> minutes on 32 bit systems? Or is there some standard hack to circumvent
> the wrap around?
>
> If I'm posting this to the wrong ng please let me know.
In addition to other replies, I am wondering why you can't use time() or a
similar function to replace clock(), or at least to calculate how many times
clock_t wrapped around in your program?
Regards
Dmitry
| |
| Pascal Bourguignon 2005-05-14, 1:23 pm |
| "jEL" <emanuel.landeholm@gmail.com> writes:
> systems.
>
> Right.
>
> many wrap-arrounds occured, by periodically checking if the new
> clock() is less than the previous.
>
> GNU libc is not helping, no. I ended up doing something like the below:
>
> --8<--
> long long msecs;
> long long last, start, current;
> int clockwraps;
> ...
> last = start = clock();
> clockwraps = 0;
> ...
> /* hack to circumvent 32 bit wrap around */
>
> current = clock();
> if(current < last)
> ++clockwraps;
> last = current;
> current += clockwraps * (1LL << 32);
>
> msecs = (1000 * (current - start)) / (CLOCKS_PER_SEC);
> --8<--
>
> I'm not entirely convinced of the correctness of my solution. I suppose
> I'll find out if it works in 72 minutes but I'd gladly accept any
> comments on the code.
Well I'd use a more proactive approach. Your code works correctly
only when it's run at least onece every 72 minutes of execution time.
But what if you run a big factorization that takes two hours of CPU
time?
So I'd use a timer (setitimer) to check the rollover more often.
--
__Pascal Bourguignon__ http://www.informatimago.com/
Nobody can fix the economy. Nobody can be trusted with their finger
on the button. Nobody's perfect. VOTE FOR NOBODY.
| |
|
| Right, I see what you mean. My call to clock() runs at least once every
36 mins. so I guess I'm OK as far as Shanning's theorem goes . The code
seems to work fine.
Thanks for your advice though! It really got me started in the right
direction.
| |
|
| Okay, that should obviously be Shannon's theorem.
| |
| Chris Croughton 2005-05-17, 7:48 am |
| On Sat, 14 May 2005 11:53:05 +0400, Dmitry Mityugov
<dmitry.mityugov@gmail.com> wrote:
> jEL wrote:
>
> In addition to other replies, I am wondering why you can't use time() or a
> similar function to replace clock(), or at least to calculate how many times
> clock_t wrapped around in your program?
time() returns elapsed ("wall clock") time, clock() returns CPU time.
Yes, I had the same idea...
On Linux (and some other POSIX systems) you can use getrusage(2). This
gives (among other things) times as struct timeval, which has seconds
and microseconds, for user and system times of the process. (I
qualified "other POSIX systems" because the type used for the
'microseconds' part of the time seems to be optional as I read it.)
Chris C
|
|
|
|
|