Last active
October 9, 2018 12:20
-
-
Save leon-joel/84bf2f06b2ccaf63210e348ab7e40c2f to your computer and use it in GitHub Desktop.
C++11 auto is much faster than std::function.
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
#pragma once | |
#include <chrono> | |
using namespace std::chrono; | |
inline double get_time_sec(void) { | |
return static_cast<double>(duration_cast<nanoseconds>(steady_clock::now().time_since_epoch()).count()) / 1000000000; | |
} | |
// ref: [(c++11)chronoを用いた時間計測(タイムスタンプのとり方) - Qiita](https://qiita.com/hurou927/items/a2d63837e731713c7a22) |
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 "ChronoHelper.h" | |
#include "MyTest.h" | |
int main() | |
{ | |
// Result: x64 x64 x86 | |
// loop count 1B 4B 4B | |
// local : 0.28s 1.32s 1.57s | |
// template : 0.33s 1.32s 1.50s | |
// std:function: 2.18s 9.11s 12.17s | |
// Compiler: VS2017 15.8.6 (C++17) | |
// OS: Windows10 Oct2018Update | |
unsigned long long sum = 0; | |
// local | |
std::cout << "Loop(local)" << std::endl; | |
double start = get_time_sec(); | |
for (unsigned int i = 0; i < CMyTest::LOOP_COUNT; i++) { | |
sum += i; | |
} | |
double end = get_time_sec(); | |
//std::cout << sum << std::endl; | |
std::cout << "elapsed: " << end - start << "sec" << std::endl; | |
// use template | |
sum = 0; | |
std::cout << "LoopByTemplate" << std::endl; | |
start = get_time_sec(); | |
CMyTest::LoopByTemplate([&](auto&& v) { | |
sum += v; | |
}); | |
end = get_time_sec(); | |
//std::cout << sum << std::endl; | |
std::cout << "elapsed: " << end - start << "sec" << std::endl; | |
// use std::function | |
sum = 0; | |
std::cout << "LoopByFunc" << std::endl; | |
start = get_time_sec(); | |
CMyTest::LoopByFunc([&](auto&& v) { | |
sum += v; | |
}); | |
end = get_time_sec(); | |
//std::cout << sum << std::endl; | |
std::cout << "elapsed: " << end - start << "sec" << std::endl; | |
} | |
// ref: [C++11 autoはstd::functionより高速 – Hossy](http://www.ngc.is.ritsumei.ac.jp/~hossy/2015/07/c11-auto%E3%81%AFstdfunction%E3%82%88%E3%82%8A%E9%AB%98%E9%80%9F/) |
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
#pragma once | |
#include <functional> | |
class CMyTest { | |
public: | |
static constexpr unsigned int LOOP_COUNT = 4'000'000'000; | |
public: | |
static void LoopByFunc(const std::function<void(unsigned int)>& func) { | |
for (unsigned int i = 0; i < LOOP_COUNT; i++) { | |
func(i); | |
} | |
} | |
template<typename TFunc> | |
static void LoopByTemplate(const TFunc& func) { | |
for (unsigned int i = 0; i < LOOP_COUNT; i++) { | |
func(i); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment