-
-
Save adler-j/a440d4539ad1bbaa7dc3e2727f93e8b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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