Created
January 12, 2018 04:09
-
-
Save hunandy14/d9034e595ce53f3112bbfde16398fe10 to your computer and use it in GitHub Desktop.
計時器
This file contains 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 : Timer.hpp | |
Date : 2017/12/19 | |
By : CharlotteHonG | |
Final: 2017/12/19 | |
*****************************************************************/ | |
#pragma once | |
#include <iostream> | |
#include <string> | |
#include <ctime> | |
class Timer { | |
public: | |
Timer(std::string name=""): name(name){ | |
startTime = clock(); | |
} | |
operator double() { | |
return time; | |
} | |
public: | |
void start() { | |
startTime = clock(); | |
flag=0; | |
} | |
void end() { | |
finalTime = clock(); | |
time = (double)(finalTime - startTime)/CLOCKS_PER_SEC; | |
} | |
void print(std::string name="") { | |
if (flag==0) { | |
flag = 1; | |
end(); | |
} | |
if(name=="") { | |
name=this->name; | |
} | |
if(priSta) { | |
std::cout << "#" << name << ", " << " time = " << time << "s" << std::endl; | |
} | |
} | |
private: | |
std::string name; | |
clock_t startTime; | |
clock_t finalTime; | |
double time; | |
bool flag = 0; | |
public: | |
bool priSta = 1; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment