Created
July 22, 2018 10:41
-
-
Save bitshifter/1e347736f1d2fe5750c87edfd00a1623 to your computer and use it in GitHub Desktop.
sampling vs instrumentation
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 <random> | |
#include <iostream> | |
#include <thread> | |
int fib(int x) { | |
if (x == 0) | |
return 0; | |
if (x == 1) | |
return 1; | |
return fib(x - 1) + fib(x - 2); | |
} | |
__declspec(noinline) void sleep(std::chrono::duration<double, std::milli> time) { | |
std::cout << "sleep(" << time.count() << ")\n"; | |
std::this_thread::sleep_for(time); | |
} | |
int main() { | |
std::mt19937 rng; | |
std::uniform_int_distribution<int> dis(28, 32); | |
for (int i = 0; i < 1000; ++i) { | |
auto start = std::chrono::high_resolution_clock::now(); | |
auto r = dis(rng); | |
auto f = fib(r); | |
std::cout << "fib(" << r << ") = " << f << "\n"; | |
auto end = std::chrono::high_resolution_clock::now(); | |
std::chrono::duration<double, std::milli> delta = end - start; | |
sleep(delta); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment