Created
November 27, 2013 09:02
-
-
Save retep998/7672754 to your computer and use it in GitHub Desktop.
double vs int for iteration
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 <chrono> | |
#include <array> | |
using namespace std; | |
using namespace chrono; | |
std::array<double, 0x1000> a; | |
void doubletest() { | |
double sum = 0; | |
for (double i = 0; i < 0x1000; ++i) { | |
for (double i = 0; i < 0x1000; ++i) { | |
sum += a[i]; | |
} | |
} | |
volatile double result = sum; | |
} | |
void inttest() { | |
double sum = 0; | |
for (int i = 0; i < 0x1000; ++i) { | |
for (int i = 0; i < 0x1000; ++i) { | |
sum += a[i]; | |
} | |
} | |
volatile double result = sum; | |
} | |
template <typename T> | |
void Test(T func) { | |
milliseconds best = seconds(1000); | |
for (int i = 0x40; i; --i) { | |
auto a = high_resolution_clock::now(); | |
func(); | |
auto b = high_resolution_clock::now(); | |
auto dif = duration_cast<milliseconds>(b - a); | |
if (dif < best) | |
best = dif; | |
} | |
cout << best.count() << "ms" << endl; | |
} | |
int main() { | |
a.fill(1); | |
Test(doubletest); | |
Test(inttest); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment