Skip to content

Instantly share code, notes, and snippets.

@carasuca
Created July 19, 2015 19:34
Show Gist options
  • Save carasuca/001e98f0b9351c76a606 to your computer and use it in GitHub Desktop.
Save carasuca/001e98f0b9351c76a606 to your computer and use it in GitHub Desktop.
Test int vs float performance with simplest operations.
#include "stdafx.h"
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <array>
#include <vector>
#include <iostream>
#include <sstream>
#include <time.h>
std::ostringstream oss;
size_t tick()
{
LARGE_INTEGER li;
::QueryPerformanceCounter(&li);
return (size_t&)li;
}
template<typename T>
size_t test_one(std::vector<T>& values)
{
for (T& v : values) v = T(::rand());
T sum = T(0);
size_t start = tick();
for (auto v : values) sum += v;
//for (auto v : values) sum *= v; // halves win, floaters 60% slower than fixed
//for (auto v : values) sum += v; // halves win, floaters suck
//
// changes stuff diametrically, chars win, floaters are not left behind
//for (auto v : values) sum += v*values.front();
size_t delta = tick() - start;
oss << sum;
return delta;
}
template<typename T>
void test(const char msg[], size_t loops, size_t count)
{
std::vector<size_t> times(loops);
std::vector<T> values(count);
size_t avg = 0;
for (size_t& t: times) avg += t = test_one<T>(values);
for (size_t t : times) printf(" %i", t);
printf("|%s %i\n", msg, avg / loops);
}
void test_all(size_t loops, size_t count)
{
test<char>("c", loops, count);
test<short>("h", loops, count);
test<unsigned short>("uh", loops, count);
test<int>("i", loops, count);
test<unsigned int>("ui", loops, count);
test<float>("f", loops, count);
test<double>("d", loops, count);
test<size_t>("s", loops, count);
}
void main()
{
::srand((int)::time(nullptr));
size_t loops = 10;
for (auto count : { 1000, 10*1000, 100 * 1000, 1000 * 1000 })
{
printf("\nTesting %i values %i times\n", count, loops);
test_all(loops, count);
}
printf("%i", oss.str().length());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment