Skip to content

Instantly share code, notes, and snippets.

@adler-j
Last active August 7, 2017 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adler-j/a440d4539ad1bbaa7dc3e2727f93e8b1 to your computer and use it in GitHub Desktop.
Save adler-j/a440d4539ad1bbaa7dc3e2727f93e8b1 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <chrono>
#include <cmath>
#include <vector>
#define N 100000000
void pow(){
double a = 2.0, b = 3.0, c = 0.0;
for (long int i = 0; i < N; i++)
c += std::pow(a, b);
std::cout << c << std::endl; // Side effect to avoid optimization
}
void explog(){
double a = 2.0, b = 3.0, c = 0.0;
for (long int i = 0; i < N; i++)
c += std::exp(b * std::log(a));
std::cout << c << std::endl;
}
int main()
{
auto t1 = std::chrono::high_resolution_clock::now();
pow();
auto t2 = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
printf("pow : %d ms\n", duration);
t1 = std::chrono::high_resolution_clock::now();
explog();
t2 = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
printf("exp(log) : %d ms.\n", duration);
std::getchar();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment