Skip to content

Instantly share code, notes, and snippets.

@konstantint
Last active September 8, 2019 22:43
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 konstantint/49fc753a420f089b10938f8d81f8ec6b to your computer and use it in GitHub Desktop.
Save konstantint/49fc753a420f089b10938f8d81f8ec6b to your computer and use it in GitHub Desktop.
// https://nauka.leprosorium.ru/comments/2349993/
#include <iostream>
#include <chrono>
using namespace std;
using namespace std::chrono;
long long f(int x) {
if (x < 0) return 0;
else if (x == 0) return 1;
else return f(x-1) + f(x-2) + f(x-3) + f(x-4);
}
long long f2(int x) {
if (x <= 0) return (x > 0) - (x < 0) + 1;
else return f2(x-1) + f2(x-2) + f2(x-3) + f2(x-4);
}
void measure(const char* name, long long (*fn)(int)) {
steady_clock::time_point begin = steady_clock::now();
long long res = fn(30);
steady_clock::time_point end = steady_clock::now();
cout << name << " output " << res << " time " << duration_cast<microseconds>(end - begin).count() << endl;
}
int main() {
measure("Naive", f);
measure("Fancy", f2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment