Skip to content

Instantly share code, notes, and snippets.

@bitshifter
Created July 22, 2018 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitshifter/1e347736f1d2fe5750c87edfd00a1623 to your computer and use it in GitHub Desktop.
Save bitshifter/1e347736f1d2fe5750c87edfd00a1623 to your computer and use it in GitHub Desktop.
sampling vs instrumentation
#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