Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C++ Clock() always zero...

Posted on 2012-04-12
7
527 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

808 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