c++ Timer?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

c++ Timer?

 
0
  #1
Feb 16th, 2007
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!
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.

-Edsger Dijkstra
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: c++ Timer?

 
0
  #2
Feb 16th, 2007
  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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,602
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1489
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: c++ Timer?

 
0
  #3
Feb 16th, 2007
>> 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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: c++ Timer?

 
0
  #4
Feb 16th, 2007
Originally Posted by iamthwee View Post
  
  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.
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.

-Edsger Dijkstra
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,602
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1489
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: c++ Timer?

 
0
  #5
Feb 17th, 2007
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).
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: c++ Timer?

 
0
  #6
Feb 17th, 2007
Originally Posted by Ancient Dragon View Post
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.
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.

-Edsger Dijkstra
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,602
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1489
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: c++ Timer?

 
0
  #7
Feb 17th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,827
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 750
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: c++ Timer?

 
0
  #8
Feb 17th, 2007
>How will I use this?
Wrap it in a class. It's much easier to use that way:
  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. };
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC