944,191 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 45869
  • C++ RSS
Feb 16th, 2007
0

c++ Timer?

Expand Post »
Hi all

I am new to C++ and have a few questions if that is ok.

I would like to get into good habbits early (i.e., using switch instead of 12 if else statments) and was wondering if anyone knew right off how to get a timer to time the compilation and execution of the program? I would like to test and tweak my work just to see what will make it faster and smoother. My prof said he knew there was one available but he couldn't think of it right off... any clue?

Also, I am looking for an extra credit problem. I was told that I might try writing programming "pearls" - techniques that elegantly solve common problems. If anyone has an idea of something creative please let me know. Note that I only have knowledge on iostream, fstream, iomanip, cmath, and control structures. Not sure how creative I can get with so little under my belt.

Thanks everyone!
Similar Threads
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
Feb 16th, 2007
0

Re: c++ Timer?

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <ctime>
  4.  
  5. int main()
  6. {
  7. std::clock_t start;
  8. double diff;
  9.  
  10. start = std::clock();
  11. for ( int i = 0; i < 1000000; i++ )
  12. printf ( "iamthwee" );
  13. diff = ( std::clock() - start ) / (double)CLOCKS_PER_SEC;
  14.  
  15. std::cout<<"printf: "<< diff <<'\n';
  16. }
Last edited by iamthwee; Feb 16th, 2007 at 4:01 pm.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Feb 16th, 2007
0

Re: c++ Timer?

>> was told that I might try writing programming "pearls

A common problem is to write a function that validates user integer input. It would get the value from the keyboard as a string then parse the string for illegal characters. If any illegal characters are found display an error message and ask for input again. Finally when no illegals are found convert the string to int and return the value.
Last edited by Ancient Dragon; Feb 16th, 2007 at 5:10 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Feb 16th, 2007
0

Re: c++ Timer?

Click to Expand / Collapse  Quote originally posted by iamthwee ...
  
c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <ctime>
  4.  
  5. int main()
  6. {
  7. std::clock_t start;
  8. double diff;
  9.  
  10. start = std::clock();
  11. for ( int i = 0; i < 1000000; i++ )
  12. printf ( "iamthwee" );
  13. diff = ( std::clock() - start ) / (double)CLOCKS_PER_SEC;
  14.  
  15. std::cout<<"printf: "<< diff <<'\n';
  16. }

How will I use this? part at the beginning and part at the end or what? thanks! :mrgreen:
Last edited by Duki; Feb 16th, 2007 at 11:24 pm.
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
Feb 17th, 2007
0

Re: c++ Timer?

call clock() before starting the code you want to profile then again after the code finished. The difference is the approximate executation time. Since the code runs very quickly you probably have to run it quite a few times to get any measurable results. In his example he ran the print function 1,000,000 times. If you want the execution time per iteration then just make the simple division. (total time / number of iterations).
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Feb 17th, 2007
0

Re: c++ Timer?

call clock() before starting the code you want to profile then again after the code finished. The difference is the approximate executation time. Since the code runs very quickly you probably have to run it quite a few times to get any measurable results. In his example he ran the print function 1,000,000 times. If you want the execution time per iteration then just make the simple division. (total time / number of iterations).

ah ok. i set it to fixed and showpoint, which gave more input for smaller apps.

could you explain the code to me? Sorry I'm asking you to tutor me, but I'm just interested in this.

mainly this part:
diff = ( std::clock() - start ) / (double)CLOCKS_PER_SEC;


but i'm also lost on how the timer works... what does what in the code? it looks like a lot of uninitialized variables, but can't be because it works? :eek:
Last edited by Duki; Feb 17th, 2007 at 2:36 am.
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
Feb 17th, 2007
1

Re: c++ Timer?

The clock function returns the number of clock ticks that have elapsed since the program started. One clock tick is typically 1 millionth of a second (called a millisecond) but it doesn't have to be. The macro CLOCKS_PER_SEC is the number of ticks per second. So if clock() returns a value of 2000000 then that is 2000000/CLOCK_PER_SEC or 2 seconds.

The line diff = ( std::clock() - start ) / (double)CLOCKS_PER_SEC; is just calculating the difference between the current time and the previous time, then converting it to seconds.

>>it looks like a lot of uninitialized variables
Look closely and you will see that his code initializes the variables correctly. Initializing variables to zero or some other value when declared is a good habit to get into, but its not absolutely required as long as the variables are initialized someplace before attempting to access them.
Last edited by Ancient Dragon; Feb 17th, 2007 at 7:51 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Feb 17th, 2007
0

Re: c++ Timer?

>How will I use this?
Wrap it in a class. It's much easier to use that way:
C++ Syntax (Toggle Plain Text)
  1. #include <ctime>
  2.  
  3. class jsw_timer {
  4. public:
  5. typedef double diff_type;
  6. public:
  7. jsw_timer(): start ( std::clock() ), elapsed ( 0.0 ) {}
  8. public:
  9. void begin() { start = std::clock(); elapsed = 0.0; }
  10. diff_type end() {
  11. elapsed = static_cast<diff_type> ( std::clock() ) - start;
  12. return elapsed /= CLOCKS_PER_SEC;
  13. }
  14. diff_type last() const { return elapsed; }
  15. private:
  16. std::clock_t start;
  17. diff_type elapsed;
  18. };
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: NTVDM uses 95% of CPU
Next Thread in C++ Forum Timeline: how can i use an iterator for a 2d vector?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC