Last active
March 28, 2017 06:48
-
-
Save tforgione/2bb8b0da61d63d09a8cfb3654811c02b 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> | |
int main(int argc, char *argv[]) | |
{ | |
constexpr std::size_t CONSTEXPR_SIZE = 1000000000000000000; | |
// The good way to use this program is to call it like so : | |
// ./main 1000000000000000000 | |
std::size_t DYNAMIC_SIZE = std::atoll(argv[1]); | |
// Ensure the values are the same | |
// Compiler is smart, if you do this verification, he'll inline the last | |
// computation | |
// if (CONSTEXPR_SIZE != DYNAMIC_SIZE) { | |
// std::cerr << "Wrong size" << std::endl; | |
// return 1; | |
// } | |
// CONSTEXPR Loop | |
// O(n^3) computation executed instantly | |
std::size_t count = 0; | |
auto before = std::chrono::high_resolution_clock::now(); | |
for (std::size_t i = 0; i < CONSTEXPR_SIZE; i++) { | |
for (std::size_t j = 0; j < CONSTEXPR_SIZE; j++) { | |
for (std::size_t k = 0; k < CONSTEXPR_SIZE; k++) { | |
count++; | |
} | |
} | |
} | |
auto after = std::chrono::high_resolution_clock::now(); | |
// Print count so the compiler doesn't avoid the computation | |
std::cout << count << std::endl; | |
std::cout << (after - before).count() << std::endl; | |
// DYNAMIC Loop | |
// super long O(n) computation | |
count = 0; | |
before = std::chrono::high_resolution_clock::now(); | |
for (std::size_t i = 0; i < DYNAMIC_SIZE; i++) { | |
count++; | |
} | |
after = std::chrono::high_resolution_clock::now(); | |
std::cout << count << std::endl; | |
std::cout << (after - before).count() << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment