Created
December 3, 2013 20:00
-
-
Save leifwalsh/7776438 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
#include <chrono> | |
#include <iostream> | |
#include <string> | |
#include <boost/scoped_ptr.hpp> | |
#include <boost/optional.hpp> | |
#include <boost/utility/in_place_factory.hpp> | |
class Thing { | |
int x; | |
public: | |
__attribute__((noinline)) | |
Thing() : x(1) {} | |
}; | |
void use_ptrs(int N) { | |
for (int i = 0; i < N; ++i) { | |
boost::scoped_ptr<Thing> p(new Thing); | |
} | |
} | |
void use_optional(int N) { | |
for (int i = 0; i < N; ++i) { | |
boost::optional<Thing> o; | |
o = boost::in_place(); | |
} | |
} | |
class Timer { | |
std::string name; | |
std::chrono::time_point<std::chrono::high_resolution_clock> t0; | |
public: | |
Timer(const std::string& n) : name(n), t0(std::chrono::high_resolution_clock::now()) {} | |
~Timer() { | |
std::chrono::milliseconds elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - t0); | |
std::cout << name << " " << elapsed.count() << "ms" << std::endl; | |
} | |
}; | |
int main(void) { | |
{ | |
Timer t("Pointers"); | |
use_ptrs(10 << 25); | |
} | |
{ | |
Timer t("Optional"); | |
use_optional(10 << 25); | |
} | |
return 0; | |
} | |
/* | |
% make CXXFLAGS=-std=c++11 optional_speed && ./optional_speed | |
g++ -std=c++11 optional_speed.cpp -o optional_speed | |
Pointers 9643ms | |
Optional 19500ms | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment