Last active
January 27, 2018 21:56
-
-
Save YoukaiCat/a5d29129f078bb6cc2c8 to your computer and use it in GitHub Desktop.
Move semantics benchmark
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 <iostream> | |
#include "MoveSemanticsBench.h" | |
using namespace std; | |
int main() | |
{ | |
MoveSemanticsBench::run(); | |
return 0; | |
} |
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 "MoveSemanticsBench.h" | |
#include <vector> | |
#include <algorithm> | |
#include <memory> | |
#include <iostream> | |
#include "Utils.h" | |
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
std::vector<int> fill_vector(std::vector<int> && vector) | |
{ | |
std::iota(vector.begin(), vector.end(), 0); | |
return vector; | |
} | |
std::vector<int> make_vector(int elementCount) | |
{ | |
std::vector<int> vector(elementCount); | |
fill_vector(std::move(vector)); | |
return vector; | |
} | |
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
std::vector<int> fill_vectorEM(std::vector<int> && vector) | |
{ | |
std::iota(vector.begin(), vector.end(), 0); | |
return std::move(vector); | |
} | |
std::vector<int> make_vectorEM(int elementCount) | |
{ | |
std::vector<int> vector(elementCount); | |
fill_vectorEM(std::move(vector)); | |
return std::move(vector); | |
} | |
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
std::vector<int> * fill_vectorRP(std::vector<int> * vector) | |
{ | |
std::iota(vector->begin(), vector->end(), 0); | |
return vector; | |
} | |
std::vector<int> * make_vectorRP(int elementCount) | |
{ | |
auto * vector = new std::vector<int>(elementCount); | |
fill_vectorRP(vector); | |
return vector; | |
} | |
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
std::unique_ptr<std::vector<int>> fill_vectorUP(std::unique_ptr<std::vector<int>> vector) | |
{ | |
std::iota(vector->begin(), vector->end(), 0); | |
return vector; | |
} | |
std::unique_ptr<std::vector<int>> make_vectorUP(int elementCount) | |
{ | |
auto vector = std::make_unique<std::vector<int>>(std::vector<int>(elementCount)); | |
fill_vectorUP(std::move(vector)); | |
return vector; | |
} | |
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
std::shared_ptr<std::vector<int>> fill_vectorSP(std::shared_ptr<std::vector<int>> vector) | |
{ | |
std::iota(vector->begin(), vector->end(), 0); | |
return vector; | |
} | |
std::shared_ptr<std::vector<int>> make_vectorSP(int elementCount) | |
{ | |
auto vector = std::make_shared<std::vector<int>>(std::vector<int>(elementCount)); | |
fill_vectorSP(vector); | |
return vector; | |
} | |
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
void MoveSemanticsBench::run() | |
{ | |
int elementCount = 100000; | |
int times = 100000; | |
auto l1 = [=](){ auto v = make_vector(elementCount); }; | |
auto l2 = [=](){ auto v = make_vectorEM(elementCount); }; | |
auto l3 = [=](){ auto v = make_vectorRP(elementCount); delete v; }; | |
auto l4 = [=](){ auto v = make_vectorUP(elementCount); }; | |
auto l5 = [=](){ auto v = make_vectorSP(elementCount); }; | |
auto resL1 = avgTimeToRun(l1, times); | |
auto resL2 = avgTimeToRun(l2, times); | |
auto resL3 = avgTimeToRun(l3, times); | |
auto resL4 = avgTimeToRun(l4, times); | |
auto resL5 = avgTimeToRun(l5, times); | |
std::cout << "With implicit move: " << resL1.count() << "ms" << std::endl; | |
std::cout << "With explicit move: " << resL2.count() << "ms" << std::endl; | |
std::cout << "With raw pointer: " << resL3.count() << "ms" << std::endl; | |
std::cout << "With unique pointer: " << resL4.count() << "ms" << std::endl; | |
std::cout << "With shared pointer: " << resL5.count() << "ms" << std::endl; | |
} |
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
#ifndef FUN_MOVESEMANTICSBENCH_H | |
#define FUN_MOVESEMANTICSBENCH_H | |
class MoveSemanticsBench | |
{ | |
public: | |
static void run(); | |
}; | |
#endif //FUN_MOVESEMANTICSBENCH_H |
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 "Utils.h" | |
std::chrono::duration<double, std::milli> timeToRun(std::function<void()> f) | |
{ | |
auto t1 = std::chrono::high_resolution_clock::now(); | |
f(); | |
auto t2 = std::chrono::high_resolution_clock::now(); | |
return t2 - t1; | |
} | |
std::chrono::duration<double, std::milli> avgTimeToRun(std::function<void()> f, int times) | |
{ | |
std::chrono::duration<double, std::milli> accumulator; | |
for (int i = 0; i < times; i++) { | |
accumulator += timeToRun(f); | |
} | |
return accumulator / times; | |
} |
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
#ifndef FUN_UTILS_H | |
#define FUN_UTILS_H | |
#include <chrono> | |
#include <functional> | |
std::chrono::duration<double, std::milli> timeToRun(std::function<void()> f); | |
std::chrono::duration<double, std::milli> avgTimeToRun(std::function<void()> f, int times); | |
#endif //FUN_UTILS_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Release:
on
Debug:
on