Skip to content

Instantly share code, notes, and snippets.

@xkyii
Created April 14, 2014 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xkyii/10650486 to your computer and use it in GitHub Desktop.
Save xkyii/10650486 to your computer and use it in GitHub Desktop.
C++11Exeption Performance
// 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