Created
June 15, 2013 03:32
-
-
Save kf4x/5786778 to your computer and use it in GitHub Desktop.
C++ Timer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Name Javier Chavez | |
| email [email protected] | |
| oct 31,2012 | |
| */ | |
| #include "Timer.h" | |
| #include <stdlib.h> | |
| Timer::Timer() | |
| { | |
| beginTiming.tv_sec = beginTiming.tv_usec = 0; | |
| endTiming.tv_sec = endTiming.tv_usec = 0; | |
| stopped = false; | |
| startTime = 0; | |
| endTime = 0; | |
| } | |
| void Timer::start() | |
| { | |
| stopped = false; | |
| gettimeofday(&beginTiming, NULL); | |
| } | |
| void Timer::stop() | |
| { | |
| stopped = true; | |
| gettimeofday(&endTiming, NULL); | |
| } | |
| double Timer::getElapsedTime() | |
| { | |
| timeval stopWatch; | |
| stopWatch.tv_sec = stopWatch.tv_usec = 0; | |
| if(!stopped) | |
| { | |
| gettimeofday(&stopWatch, NULL); | |
| double SWend = (stopWatch.tv_sec* 1000000.0) + stopWatch.tv_usec; | |
| double SWstart = (beginTiming.tv_sec* 1000000.0) + beginTiming.tv_usec; | |
| return (SWend - SWstart)* 0.000001; | |
| } | |
| startTime = (beginTiming.tv_sec * 1000000.0) + beginTiming.tv_usec; | |
| endTime = (endTiming.tv_sec * 1000000.0) + endTiming.tv_usec; | |
| return (endTime - startTime)* 0.000001; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Name Javier Chavez | |
| email [email protected] | |
| oct 31,2012 | |
| */ | |
| #ifndef _TIMER_H_ | |
| #define _TIMER_H_ | |
| #include <sys/time.h> | |
| class Timer | |
| { | |
| public: | |
| Timer(); | |
| void start(); | |
| void stop(); | |
| double getElapsedTime(); | |
| private: | |
| double startTime; | |
| double endTime; | |
| bool stopped; | |
| timeval beginTiming; | |
| timeval endTiming; | |
| }; | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment