Solved

C++ Clock() always zero...

Posted on 2012-04-12
7
521 Views
Last Modified: 2012-04-16
#include <cstdlib>
int main()
{
clock_t endWait = clock () + 120 * CLOCKS_PER_SEC ;

  while (clock() < endWait)
   {
       std::cout <<  clock()) << std::endl;
   }  
}

I want to run the above loop for 2 minutes. But it seems the clock() always return 0 and does not increment. What's up with this? Any ideas?
0
Comment
Question by:prain
  • 3
  • 2
  • 2
7 Comments
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
The code you posted won't compile.

Assuming you fix the issues with it, and make it compile, then you should see increasing values on standard output.

What is the actual code you tried ? What platform did you run it on ?
0
 

Author Comment

by:prain
Comment Utility
#include <cstdlib>
int main()
{
 
   clock_t endWait = clock () + 120 * CLOCKS_PER_SEC ;
 
   while (clock()  < endWait)
    {
      std::cout <<  clock() << std::endl;
    }
}


I just compiled this in Unix/GCC compiler.  Infact <cstdlib> is not required.
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 62 total points
Comment Utility
Infinity08 is right. My clock() initially returned a 15 instead of a 0. You should try the example from this link to verify that you get the same result as they show.
     http://www.cplusplus.com/reference/clibrary/ctime/clock/

In your program, I set your value to 10 instead of 120 and here are the results:
$ ./a | head
endWait = 10015
15
15
15
15
15
15
15
15
15

$ time ./a | tail
10015
10015
10015
10015
10015
10015
10015
10015
10015
10015

real    0m10.593s
user    0m5.983s
sys     0m8.545s
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> Infact <cstdlib> is not required.

No, but <ctime> and <iostream> are ;)


Did you check the whole output ? It might start with a whole lot of zeroes, but eventually, you should get higher values.
0
 

Author Comment

by:prain
Comment Utility
Well, I appreciate all your help. But the example in    http://www.cplusplus.com/reference/clibrary/ctime/clock/

was my basis too. It simple delays the program for 1 second every iteration. But what I want is different. what I want is to run the loop for 2 minutes and complete stop. So here is my code again...

#include <cstdlib>
int main()
{
clock_t endWait = clock () + 120 * CLOCKS_PER_SEC ;

  while (clock() < endWait)
   {
       std::cout <<  clock()) << std::endl;
   }  
}

I expected the clock() to tick and increment the time every iteration. Is my thinking wrong? May be.
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 63 total points
Comment Utility
It's unlikely to increment for every iteration of your loop (because your loop is a lot faster than most clocks).
But it should eventually increment.

Please refer to the previous responses.
0
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
Infinity08 is right. Recall that in my previous comment, I did a head and a tail:
$ ./a | head
endWait = 10015
15
15

$ time ./a | tail
10015
10015

Notice that the repetitive clock() results repeat for quite awhile (relatively slow if printing to the console). If not having to take time to print to console, they only change their value once a second.

I happened to start off with a 15, but in your system, you could have started off with another number (possibly even 0 as infinity08 already pointed out). So, do the head and the tail as shown, and see if it sheds some light. Since you ran the demo and it counted down correctly, it appears that your system is working. Also, change your 120 seconds to 10 seconds to see if that works.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now