Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Any idea why clock() returns 0 or 10000

Reply
Thread Tools

Any idea why clock() returns 0 or 10000

 
 
none
Guest
Posts: n/a
 
      07-10-2007
Hello,
can you help?
if I include:
#include <time.h>

and run the code:
clock_t t = clock();
cout << "t is " << t << endl;

I get (seemingly randomly) "t is 0" or "t is 10000"

Any ideas?
thank you.

I am using:
gcc version 4.1.2 (Ubuntu 4.1.2-0ubuntu4) via Eclipse.

Thanks you in advance.

John
 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      07-10-2007
"none" <""john\"@(none)"> wrote in message
news:4693690c$0$15858$(E-Mail Removed)...
> Hello,
> can you help?
> if I include:
> #include <time.h>
>
> and run the code:
> clock_t t = clock();
> cout << "t is " << t << endl;
>
> I get (seemingly randomly) "t is 0" or "t is 10000"
>
> Any ideas?
> thank you.
>
> I am using:
> gcc version 4.1.2 (Ubuntu 4.1.2-0ubuntu4) via Eclipse.


Apparently you clock has a detail of 10,000ns (I think it's nano seconds?).
clock() says how many clock ticks have happended since your program started.
since it's a short program, it usually hasn't hit 10000 by the time clock_tt
= clock(); is reached, hence you get 0. Every now and then, maybe cause of
other background processes, it takes a little longer so you see the tick
over.

Try something like:

for ( int i = 0; i < 100; ++i )
cout << "clock is " << clock() << "\n";

and you should see it change



 
Reply With Quote
 
 
 
 
none
Guest
Posts: n/a
 
      07-10-2007
Jim Langston wrote:
> "none" <""john\"@(none)"> wrote in message
> news:4693690c$0$15858$(E-Mail Removed)...
>> Hello,
>> can you help?
>> if I include:
>> #include <time.h>
>>
>> and run the code:
>> clock_t t = clock();
>> cout << "t is " << t << endl;
>>
>> I get (seemingly randomly) "t is 0" or "t is 10000"
>>
>> Any ideas?
>> thank you.
>>
>> I am using:
>> gcc version 4.1.2 (Ubuntu 4.1.2-0ubuntu4) via Eclipse.

>
> Apparently you clock has a detail of 10,000ns (I think it's nano seconds?).
> clock() says how many clock ticks have happended since your program started.
> since it's a short program, it usually hasn't hit 10000 by the time clock_tt
> = clock(); is reached, hence you get 0. Every now and then, maybe cause of
> other background processes, it takes a little longer so you see the tick
> over.
>
> Try something like:
>
> for ( int i = 0; i < 100; ++i )
> cout << "clock is " << clock() << "\n";
>
> and you should see it change
>
>
>

Thanks for the reply Jim,
Unfortunately it results in 100 x "clock is 0", or 100 x "clock is 10000"

Interesting.
 
Reply With Quote
 
anon
Guest
Posts: n/a
 
      07-10-2007
none wrote:
> Jim Langston wrote:
>> "none" <""john\"@(none)"> wrote in message
>> news:4693690c$0$15858$(E-Mail Removed)...
>>> Hello,
>>> can you help?
>>> if I include:
>>> #include <time.h>
>>>
>>> and run the code:
>>> clock_t t = clock();
>>> cout << "t is " << t << endl;
>>>
>>> I get (seemingly randomly) "t is 0" or "t is 10000"
>>>
>>> Any ideas?
>>> thank you.
>>>
>>> I am using:
>>> gcc version 4.1.2 (Ubuntu 4.1.2-0ubuntu4) via Eclipse.

>>
>> Apparently you clock has a detail of 10,000ns (I think it's nano
>> seconds?). clock() says how many clock ticks have happended since your
>> program started. since it's a short program, it usually hasn't hit
>> 10000 by the time clock_tt = clock(); is reached, hence you get 0.
>> Every now and then, maybe cause of other background processes, it
>> takes a little longer so you see the tick over.
>>
>> Try something like:
>>
>> for ( int i = 0; i < 100; ++i )
>> cout << "clock is " << clock() << "\n";
>>
>> and you should see it change
>>
>>
>>

> Thanks for the reply Jim,
> Unfortunately it results in 100 x "clock is 0", or 100 x "clock is 10000"
>
> Interesting.


http://linux.die.net/man/3/clock

Try this:

for ( long i = 0; i < 100000000; ++i )
cout << "clock is " << clock() << "\n";

and I am sure you will see it changing
 
Reply With Quote
 
none
Guest
Posts: n/a
 
      07-10-2007
Thanks very much anon and Jim - you're right it does change.


>
> Try this:
>
> for ( long i = 0; i < 100000000; ++i )
> cout << "clock is " << clock() << "\n";
>
> and I am sure you will see it changing

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      07-11-2007
On Jul 10, 1:27 pm, "Jim Langston" <(E-Mail Removed)> wrote:
> "none" <""john\"@(none)"> wrote in message
> news:4693690c$0$15858$(E-Mail Removed)...
> > can you help?
> > if I include:
> > #include <time.h>


> > and run the code:
> > clock_t t = clock();
> > cout << "t is " << t << endl;


> > I get (seemingly randomly) "t is 0" or "t is 10000"


> > Any ideas?


> > I am using:
> > gcc version 4.1.2 (Ubuntu 4.1.2-0ubuntu4) via Eclipse.


Knowing the OS would help as well. clock() usually resolves to
a system function.

> Apparently you clock has a detail of 10,000ns (I think it's nano seconds?).


It depends on the system. And there are two separate issues:
units (i.e. 1/CLOCKS_PER_SEC) and resolution. The Posix
standard requires that CLOCKS_PER_SEC be 1000000, so the unit
will always be a microsecond. The resolution varies, but I
suspect that it's rarely this much.

The value you get on the first call to clock isn't specified,
either (although it shouldn't represent much more than the time
the program has already run), as does what clock() actually
measures.

> clock() says how many clock ticks have happended since your
> program started. since it's a short program, it usually
> hasn't hit 10000 by the time clock_tt = clock(); is reached,
> hence you get 0. Every now and then, maybe cause of other
> background processes, it takes a little longer so you see the
> tick over.


The actual clock ticks come at specific intervals, independantly
of when the program actually started. The actual clock ticks
are probably of much greater granularity than 1 microsecond, or
whatever. If the first clock tick happens to fall between the
start of his program and his call to clock(), he could get the
granularity. (Under Solaris, the first call to clock() always
returns 0, but this isn't true for other systems.)

Note too that clock() returns "the implementation\u2019s best
approximation to the processor time used by the program". A
system in which clock() increments due to activity by other
programs is broken.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: App idea, Any idea on implementation? Matthew_WARREN@bnpparibas.com Python 0 02-05-2008 05:50 PM
App idea, Any idea on implementation? Dr Mephesto Python 3 02-05-2008 06:55 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
10000 Router CPU mib Kevin Cisco 4 12-02-2003 06:27 PM



Advertisments