Skip to content

Instantly share code, notes, and snippets.

@donalmacc
Created May 21, 2014 14:58
Show Gist options
  • Save donalmacc/02d7161cd4b28afc253b to your computer and use it in GitHub Desktop.
Save donalmacc/02d7161cd4b28afc253b to your computer and use it in GitHub Desktop.
invsqrt vs fastinvsqrt
#include "glm.h"
#include<vector>
#include <iostream>
#include<Windows.h>
double PCFreq = 0.0;
__int64 CounterStart = 0;
void StartCounter()
{
LARGE_INTEGER li;
if(!QueryPerformanceFrequency(&li))
std::cout << "QueryPerformanceFrequency failed!\n";
PCFreq = double(li.QuadPart); // Use seconds
QueryPerformanceCounter(&li);
CounterStart = li.QuadPart;
}
double GetCounter()
{
LARGE_INTEGER li;
QueryPerformanceCounter(&li);
return double(li.QuadPart-CounterStart)/PCFreq;
}
const int NUMRESULTS = 10000000;
const int NUMITERATIONS = 100;
int wmain(int argc, char* argv[])
{
std::vector<float> nums, results;
nums.reserve(NUMRESULTS);
results.reserve(NUMRESULTS);
for(int i = 0; i < NUMRESULTS; i++)
{
nums.push_back(static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/10000.f)) + 0.5f);
results.push_back(0.f);
}
// Get the start time
StartCounter();
for(int iterations = 0; iterations < NUMITERATIONS; iterations++)
{
for(int i = 0; i < NUMRESULTS; i++)
{
results[i] = glm::inversesqrt(nums[i]);
}
}
double res = GetCounter();
std::cout << "Time taken for " << NUMITERATIONS << " of " << NUMRESULTS << " floats; Regular Method: " << res << std::endl;
// Repeat for fastinvsqrt
StartCounter();
for(int iterations = 0; iterations < NUMITERATIONS; iterations++)
{
for(int i = 0; i < NUMRESULTS; i++)
{
results[i] = glm::fastInverseSqrt(nums[i]);
}
}
res = GetCounter();
std::cout << "Time taken for " << NUMITERATIONS << " of " << NUMRESULTS << " floats; Faster Method: " << res << std::endl;
getchar();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment