Last active
November 8, 2018 20:50
-
-
Save gubatron/612118b529bc7428325bb17bb5f4acef to your computer and use it in GitHub Desktop.
parity_benchmark.cpp
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
// g++ -std=c++11 parity_benchmark.cpp -o parity_benchmark | |
#include <iostream> | |
#include <chrono> | |
#include <functional> | |
std::int64_t now() { | |
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_\ | |
epoch()).count(); | |
} | |
bool isEven1(int n) { | |
return n % 2 == 0; | |
} | |
bool isEven2(int n) { | |
return n & 1 == 0; | |
} | |
void testFunction(int iterations, std::function<bool(int)> f, std::string testName) { | |
std::int64_t a = now(); | |
for (int i=0; i < iterations; i++) | |
f(i); | |
std::int64_t duration = now() - a; | |
std::cout << testName << ": " << iterations << " iterations in " << duration << "ms." << std::endl; | |
} | |
int main() { | |
for (int i=0; i < 10; i++) { | |
std::cout << "Running test " << (i+1) << "..." << std::endl; | |
testFunction(99999999, isEven1, "modulus parity test"); | |
testFunction(99999999, isEven2, "bit parity test"); | |
std::cout << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output