Last active
September 8, 2019 22:43
-
-
Save konstantint/49fc753a420f089b10938f8d81f8ec6b 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
// 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