Created
June 9, 2015 15:51
-
-
Save ryutorion/8d57d86038cf29a4152d 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 <iostream> | |
#include <random> | |
#include <chrono> | |
#include <limits> | |
#include <vector> | |
#include <functional> | |
#include <cmath> | |
using namespace std; | |
int main() | |
{ | |
cout << "乱数の生成" << endl; | |
int const Size = 0x7ffffff; | |
random_device rd; | |
mt19937 mt(rd()); | |
auto value = bind(uniform_real_distribution<float>(1.f, 10.f), mt); | |
vector<float> values(Size); | |
for(auto i = 0; i < Size; ++i) | |
{ | |
values[i] = value(); | |
} | |
vector<int> int_values(Size); | |
cout << "キャストによる変換" << endl; | |
auto t1 = chrono::high_resolution_clock::now(); | |
for(auto i = 0; i < Size; ++i) | |
{ | |
int_values[i] = static_cast<int>(values[i]); | |
} | |
auto t2 = chrono::high_resolution_clock::now(); | |
cout << "経過時間 : " << chrono::duration_cast<chrono::milliseconds>(t2 - t1).count() << endl; | |
union { | |
int i; | |
float f; | |
} v, bias; | |
bias.i = (23 + 127) << 23; | |
cout << "共用体による整数への変換" << endl; | |
auto t3 = chrono::high_resolution_clock::now(); | |
for(auto i = 0; i < Size; ++i) | |
{ | |
v.f = values[i]; | |
v.f += bias.f; | |
v.i -= bias.i; | |
int_values[i] = v.i; | |
} | |
auto t4 = chrono::high_resolution_clock::now(); | |
cout << "経過時間 : " << chrono::duration_cast<chrono::milliseconds>(t4 - t3).count() << endl; | |
vector<float> float_values(Size); | |
cout << "absによる絶対値への変換" << endl; | |
auto t5 = chrono::high_resolution_clock::now(); | |
for(auto i = 0; i < Size; ++i) | |
{ | |
float_values[i] = abs(values[i]); | |
} | |
auto t6 = chrono::high_resolution_clock::now(); | |
cout << "経過時間 : " << chrono::duration_cast<chrono::milliseconds>(t6 - t5).count() << endl; | |
cout << "共用体による絶対値への変換" << endl; | |
auto t7 = chrono::high_resolution_clock::now(); | |
for(auto i = 0; i < Size; ++i) | |
{ | |
v.f = values[i]; | |
v.i &= 0x7fffffff; | |
float_values[i] = v.f; | |
} | |
auto t8 = chrono::high_resolution_clock::now(); | |
cout << "経過時間 : " << chrono::duration_cast<chrono::milliseconds>(t8 - t7).count() << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment