Skip to content

Instantly share code, notes, and snippets.

@retep998
Created November 27, 2013 09:02
Show Gist options
  • Save retep998/7672754 to your computer and use it in GitHub Desktop.
Save retep998/7672754 to your computer and use it in GitHub Desktop.
double vs int for iteration
#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