[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

C++ Clock() always zero...

Posted on 2012-04-12
7
Medium Priority
?
538 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
[X]
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
  • 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 248 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
Technology Partners: 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 252 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

649 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