Skip to content

Instantly share code, notes, and snippets.

@yohm
Created August 23, 2011 02:20
Show Gist options
  • Save yohm/1164172 to your computer and use it in GitHub Desktop.
Save yohm/1164172 to your computer and use it in GitHub Desktop.
performance measurement of basic arithmetical operations
#include <iostream>
#include <ctime>
#include <cmath>
// ----------------------------------------------
size_t CountUpInteger( size_t nMax) {
size_t nSum = 0;
for( size_t i=0; i<nMax; i++) {
nSum += i;
}
return nSum;
}
// ----------------------------------------------
double CountUpDouble( size_t nMax) {
double dSum = 0;
for( size_t i=0; i<nMax; i++) {
dSum += i;
}
return dSum;
}
// ----------------------------------------------
double CountUpDoubleWithIf( size_t nMax) {
double dSum = 0;
for( size_t i=0; i<nMax; i++) {
dSum += i;
if( dSum == 23.0 ) break; // must not happen
}
return dSum;
}
// ----------------------------------------------
double CountUpSin( size_t nMax) {
double dSum = 0;
for( size_t i=0; i<nMax; i++) {
dSum += sin(i);
}
return dSum;
}
// ----------------------------------------------
double CountUpLog( size_t nMax) {
double dSum = 0;
for( size_t i=1; i<nMax; i++) {
dSum += log(i);
}
return dSum;
}
// ----------------------------------------------
double CountUpLogUnroll( size_t nMax) {
double dSum = 0;
for( size_t i=1; i<nMax; i+=4) {
dSum += log(i) + log(i+1) + log(i+2) + log(i+3);
}
return dSum;
}
// ----------------------------------------------
double CountUpInverse( size_t nMax) {
double dSum = 0;
for( size_t i=1; i<nMax; i++) {
dSum += 1.0 / i;
}
return dSum;
}
// ----------------------------------------------
int main() {
clock_t start, end;
const double dCpuClock = 1.6;
const size_t nFactor = 8;
const size_t nMax = (1 << 30) / nFactor;
double dRet = 0.0;
double dElapsed = 0.0;
// -------------------------------------------------------------
std::cout << "CountUpInteger" << std::endl;
start = clock();
dRet = CountUpInteger( nMax);
end = clock();
std::cout << "dRet : " << dRet << std::endl;
dElapsed = static_cast<double>(end-start)/(CLOCKS_PER_SEC);
std::cout << "Elapased time : " << dElapsed << std::endl;
std::cout << "Elapsed clock : " << dElapsed / dCpuClock * nFactor << std::endl << std::endl;
// -------------------------------------------------------------
std::cout << "CountUpDoulbe" << std::endl;
start = clock();
dRet = CountUpDouble( nMax);
end = clock();
std::cout << "dRet : " << dRet << std::endl;
dElapsed = static_cast<double>(end-start)/(CLOCKS_PER_SEC);
std::cout << "Elapased time : " << dElapsed << std::endl;
std::cout << "Elapsed clock : " << dElapsed / dCpuClock * nFactor << std::endl << std::endl;
// -------------------------------------------------------------
std::cout << "CountUpDoubleWithIf" << std::endl;
start = clock();
dRet = CountUpDoubleWithIf( nMax);
end = clock();
std::cout << "dRet : " << dRet << std::endl;
dElapsed = static_cast<double>(end-start)/(CLOCKS_PER_SEC);
std::cout << "Elapased time : " << dElapsed << std::endl;
std::cout << "Elapsed clock : " << dElapsed / dCpuClock * nFactor << std::endl << std::endl;
// -------------------------------------------------------------
std::cout << "CountUpSin" << std::endl;
start = clock();
dRet = CountUpSin( nMax);
end = clock();
std::cout << "dRet : " << dRet << std::endl;
dElapsed = static_cast<double>(end-start)/(CLOCKS_PER_SEC);
std::cout << "Elapased time : " << dElapsed << std::endl;
std::cout << "Elapsed clock : " << dElapsed / dCpuClock * nFactor << std::endl << std::endl;
// -------------------------------------------------------------
std::cout << "CountUpLog" << std::endl;
start = clock();
dRet = CountUpLog( nMax);
end = clock();
std::cout << "dRet : " << dRet << std::endl;
dElapsed = static_cast<double>(end-start)/(CLOCKS_PER_SEC);
std::cout << "Elapased time : " << dElapsed << std::endl;
std::cout << "Elapsed clock : " << dElapsed / dCpuClock * nFactor << std::endl << std::endl;
// -------------------------------------------------------------
std::cout << "CountUpLogUnroll" << std::endl;
start = clock();
dRet = CountUpLogUnroll( nMax);
end = clock();
std::cout << "dRet : " << dRet << std::endl;
dElapsed = static_cast<double>(end-start)/(CLOCKS_PER_SEC);
std::cout << "Elapased time : " << dElapsed << std::endl;
std::cout << "Elapsed clock : " << dElapsed / dCpuClock * nFactor << std::endl << std::endl;
// -------------------------------------------------------------
std::cout << "CountUpInverse" << std::endl;
start = clock();
dRet = CountUpInverse( nMax);
end = clock();
std::cout << "dRet : " << dRet << std::endl;
dElapsed = static_cast<double>(end-start)/(CLOCKS_PER_SEC);
std::cout << "Elapased time : " << dElapsed << std::endl;
std::cout << "Elapsed clock : " << dElapsed / dCpuClock * nFactor << std::endl << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment