Solved

C++ Clock() always zero...

Posted on 2012-04-12
7
530 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
ID: 37842401
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
ID: 37843809
#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
ID: 37843858
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:Infinity08
ID: 37844046
>> 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
ID: 37851327
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
ID: 37851332
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
ID: 37852300
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

726 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