problem with clock()


 
Thread Tools Search this Thread
Top Forums Programming problem with clock()
# 1  
Old 07-27-2010
problem with clock()

Code:
#include<iostream>
#include<time.h>
using namespace std;
int main()
{
    system("date");
    clock_t start = clock();
         int i=9*8;
         while(i--)
         {
             int j=9999999;
             while(j--);
         }
    clock_t end = clock();
    double elapsed = ((double) (end - start)) / CLOCKS_PER_SEC;
    cout<<"elapsed:"<<elapsed<<"\n";
    system("date");
}

system info :SunOS sundev2 5.9 Generic_Virtual sun4u sparc SUNW,Sun-Fire-V245
g++ (GCC) 3.4.6
It takes around 5 secs to execute,but clock returns 17 secs
Code:
$./a.out
Tue Jul 27 05:14:24 PDT 2010
elapsed:17.9997
Tue Jul 27 05:14:29 PDT 2010

# 2  
Old 07-27-2010
Exact same box/OS I get (gcc 3.2.3):

Code:
> ./a.out
Tue Jul 27 07:34:36 MDT 2010
6.980000
Tue Jul 27 07:34:43 MDT 2010

Solaris 10 with gcc 3.4.3
Code:
# ./a.out
Tue Jul 27 07:43:59 MDT 2010
3.700000
Tue Jul 27 07:44:03 MDT 2010
#

It could be a C runtime problem, since Solaris 9 has been out for years, I would assume some patches are missing on your box. I stopped sysadmin with 8, just started back with 10, I missed 9 all together. Maybe another Solaris person knows what those patches are.
# 3  
Old 07-27-2010
From the manpage:
Code:
       On several other implementations, the value returned  by  clock()  also
       includes  the times of any children whose status has been collected via
       wait(2) (or another wait-type call).  Linux does not include the  times
       of  waited-for children in the value returned by clock().  The times(2)
       function, which explicitly returns  (separate)  information  about  the
       caller and its children, may be preferable.

...so you may be corrupting your clock() call by running commands in system(), though it's still odd that it'd mung it that far. Try removing the system() calls and just printing the clock() values, running the program with 'time ./program' to see how your values compare to the ones time sees.
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 07-27-2010
Following Corona's thought:
Code:
time ./a.out

gives what output?
# 5  
Old 07-29-2010
Code:
 
$cat ts.C
#include<iostream>
#include<time.h>
using namespace std;
int main()
{
clock_t start = clock();
int i=9*8;
while(i--)
{
int j=9999999;
while(j--);
}
clock_t end = clock();
cout<<"elapsed:"<<((end - start) / CLOCKS_PER_SEC)<<"\n";
}
$g++ ts.C
$time ./a.out
elapsed:17
real 0m4.364s
user 0m4.320s
sys 0m0.011s
$

Some patch should be missing
Thanks for the command 'time'
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Simple Chess Clock

I am trying to implement a simple chess clock. It should have the following options: start, stop, reset, read. Reset will set the time to zero Start will start the clock Stop will stop the clock My problem is that I want that start continues counting the time from the time it had when it... (6 Replies)
Discussion started by: kristinu
6 Replies

2. UNIX for Dummies Questions & Answers

Showing Clock

Is it possible to display the clock (timing) on the screen all the time. (3 Replies)
Discussion started by: vino.paal
3 Replies

3. Red Hat

how to reset the hardware clock

Hi all Hi could anyone tell me how i can set the Hardware Clock to the System Time, and set the System Time from the Hardware Clock. i am using RHEL 4.0. Thanks in advance. (1 Reply)
Discussion started by: daya.pandit
1 Replies

4. Solaris

Bugs with clock()

Hi there!!! Need your help in solving some tricky problems. Since clock() as such is buggy on SUN OS 5 we have started using gettimeofday() in our RTOS applications based on Solaris 9. The problems we actually encountered previously were - the applications kind of freeze/hang eternally on... (1 Reply)
Discussion started by: smanu
1 Replies

5. UNIX for Advanced & Expert Users

clock change

Hi We had a AIX box built last year but was set to the correct GMT time, but using DST time zone. In march this year the clocks went forward without issues. (if I remember a couple of weeks early due to the DST zone) This year we decided to change the clock to the correct time zone before... (0 Replies)
Discussion started by: markab2
0 Replies

6. UNIX for Dummies Questions & Answers

clock() call returning zero always

Hi, Is there a chance that the clock() call returns 0 eternally??? Using BSD. My RTOS application freezes inconsistently only on particular hosts. When debugging it, I came to see that the RTOS timer does not tick at times. The underlying system call is clock() & it always returns zero when the... (4 Replies)
Discussion started by: smanu
4 Replies

7. Programming

clock() function

Hey all, i need a program to get the CPU ticks at certain points of my program. So, i thought about using the clock function, but i'm having a hard time figuring out how it really works. I wrote this simple program to try to understand it but it made me feel more confused: #include <stdio.h>... (5 Replies)
Discussion started by: kastrup_carioca
5 Replies

8. UNIX Desktop Questions & Answers

hardware (BIOS) clock

Guys could you please tell me which appropriate command is used to set hardware (BIOS) clock so that the system keeps time when it reboots & how it's used. I use Linux Thank you (2 Replies)
Discussion started by: joseph kembo
2 Replies

9. UNIX for Dummies Questions & Answers

Clock Trouble

Hey ppl, i was wonddering, in mandrake, how to get the clok to display the time in non-military format....hehe thank you im just tired of looking at 18:00 hehe thank you (2 Replies)
Discussion started by: LolapaloL
2 Replies
Login or Register to Ask a Question