Created
May 24, 2017 13:17
-
-
Save xaedes/313c151b8374c8187689bf8b0d332871 to your computer and use it in GitHub Desktop.
benchmark ptr access
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
#include <memory> | |
#include <iostream> | |
#include "common/measureTime.h" | |
using namespace common; | |
int main(int argc, char* argv[]) | |
{ | |
int n = 100000000; | |
MeasureTime benchmarkShared; | |
MeasureTime benchmarkRaw; | |
MeasureTime benchmarkWeak1; | |
MeasureTime benchmarkWeak2; | |
int* foo = (int*)malloc(sizeof(int)); | |
*foo = 1; | |
std::shared_ptr<int> shptr(foo); | |
std::weak_ptr<int> wkptr(shptr); | |
int* rwptr = foo; | |
benchmarkShared.measureStart(); | |
for (int i=0; i < n;) {i += *shptr.get();} | |
benchmarkShared.measureEnd(); | |
benchmarkWeak1.measureStart(); | |
for (int i=0; i < n;) {i += *wkptr.lock().get();} | |
benchmarkWeak1.measureEnd(); | |
benchmarkWeak2.measureStart(); | |
auto wkptr_lock = wkptr.lock(); | |
for (int i=0; i < n;) {i += *wkptr_lock.get();} | |
benchmarkWeak2.measureEnd(); | |
benchmarkRaw.measureStart(); | |
for (int i=0; i < n;) {i += *rwptr;} | |
benchmarkRaw.measureEnd(); | |
std::cout << "benchmarkShared" << std::endl; | |
benchmarkShared.printStats(); | |
std::cout << "benchmarkWeak1" << std::endl; | |
benchmarkWeak1.printStats(); | |
std::cout << "benchmarkWeak2" << std::endl; | |
benchmarkWeak2.printStats(); | |
std::cout << "benchmarkRaw" << std::endl; | |
benchmarkRaw.printStats(); | |
return 0; | |
} | |
// Results (compiled in debug mode): | |
// benchmarkShared | |
// n 1 | |
// mean 0.633789 | |
// std 0 | |
// min 0.633789 | |
// max 0.633789 | |
// benchmarkWeak1 | |
// n 1 | |
// mean 8.56923 | |
// std 0 | |
// min 8.56923 | |
// max 8.56923 | |
// benchmarkWeak2 | |
// n 1 | |
// mean 0.582547 | |
// std 0 | |
// min 0.582547 | |
// max 0.582547 | |
// benchmarkRaw | |
// n 1 | |
// mean 0.309793 | |
// std 0 | |
// min 0.309793 | |
// max 0.309793 | |
// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment