Solved

C++ Clock() always zero...

Posted on 2012-04-12
7
524 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

778 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