Created
April 14, 2014 13:58
-
-
Save xkyii/10650486 to your computer and use it in GitHub Desktop.
C++11Exeption Performance
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
// VS2013 c++11 gtest | |
#include <chrono> | |
#include "gtest/gtest.h" | |
class ExceptionTest : public testing::Test | |
{ | |
public: | |
class TimeCounter | |
{ | |
public: | |
TimeCounter() | |
{ | |
start_ = std::chrono::system_clock::now(); | |
} | |
~TimeCounter() | |
{ | |
auto end = std::chrono::system_clock::now(); | |
std::chrono::duration<double> elapsed_seconds = end - start_; | |
std::time_t end_time = std::chrono::system_clock::to_time_t(end); | |
std::cout << "finished computation at " << std::ctime(&end_time) | |
<< "elapsed time: " << elapsed_seconds.count() << "s\n"; | |
} | |
std::chrono::time_point<std::chrono::system_clock> start_; | |
}; | |
protected: | |
int test(int in) | |
{ | |
in++; | |
in = in % count_; | |
return in; | |
} | |
const int count_ = 100000; //10W times call | |
}; | |
TEST_F(ExceptionTest, NoException) | |
{ | |
TimeCounter tc; | |
for (int i = 0; i < count_; ++i) | |
{ | |
test(i); | |
} | |
} | |
TEST_F(ExceptionTest, ExceptionNoThrown) | |
{ | |
TimeCounter tc; | |
for (int i = 0; i < count_; ++i) | |
{ | |
try | |
{ | |
test(i); | |
} | |
catch (...) | |
{ | |
} | |
} | |
} | |
TEST_F(ExceptionTest, ExceptionWithIntThrown) | |
{ | |
TimeCounter tc; | |
for (int i = 0; i < count_; ++i) | |
{ | |
try | |
{ | |
test(i); | |
throw 10; | |
} | |
catch (...) | |
{ | |
} | |
} | |
} | |
int main(int argc, char* argv[]) | |
{ | |
printf("Running main() from gtest_main.cc\n"); | |
testing::InitGoogleTest(&argc, argv); | |
RUN_ALL_TESTS(); | |
getchar(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment