Skip to content

Instantly share code, notes, and snippets.

@YoukaiCat
Last active January 27, 2018 21:56
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 YoukaiCat/a5d29129f078bb6cc2c8 to your computer and use it in GitHub Desktop.
Save YoukaiCat/a5d29129f078bb6cc2c8 to your computer and use it in GitHub Desktop.
Move semantics benchmark
#include <iostream>
#include "MoveSemanticsBench.h"
using namespace std;
int main()
{
MoveSemanticsBench::run();
return 0;
}
#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;
}
#ifndef FUN_MOVESEMANTICSBENCH_H
#define FUN_MOVESEMANTICSBENCH_H
class MoveSemanticsBench
{
public:
static void run();
};
#endif //FUN_MOVESEMANTICSBENCH_H
#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;
}
#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
@YoukaiCat
Copy link
Author

Release:
on

    int elementCount = 100000;
    int times = 100000;
With implicit move: 0.320376ms
With explicit move: 0.365217ms
With raw pointer: 0.410296ms
With unique pointer: 0.455465ms
With shared pointer: 0.500957ms

Debug:
on

    int elementCount = 10000;
    int times = 100000;
With implicit move: 0.186052ms
With explicit move: 0.18357ms
With raw pointer: 0.183459ms
With unique pointer: 0.18384ms
With shared pointer: 0.183929ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment