Created
March 12, 2017 22:17
-
-
Save jesyspa/a442a288c5822c0ff33779f5c9532ff3 to your computer and use it in GitHub Desktop.
Test of pow versus multiplication performance
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 <chrono> | |
#include <cmath> | |
#include <iostream> | |
template<typename F> | |
auto timeit(F f) { | |
int const ITERS = 1'000; | |
using namespace std::chrono; | |
auto b = system_clock::now(); | |
volatile int dummy; | |
for (int i = 0; i < ITERS; ++i) | |
dummy = f(); | |
(void)dummy; | |
auto e = system_clock::now(); | |
return duration_cast<nanoseconds>(e - b).count(); | |
} | |
int const TESTS = 1'000'000; | |
double trivial() { | |
return 0; | |
} | |
double total_pow() { | |
double total = 0; | |
for (int i = 0; i < TESTS; ++i) | |
total += std::pow(i, 2); | |
return total; | |
} | |
double total_mul() { | |
double total = 0; | |
for (int i = 0; i < TESTS; ++i) | |
total += i*i; | |
return total; | |
} | |
int main() { | |
std::cout << "trivial: " << timeit(trivial) << '\n'; | |
std::cout << "pow: " << timeit(total_pow) << '\n'; | |
std::cout << "mul: " << timeit(total_mul) << '\n'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment