Skip to content

Instantly share code, notes, and snippets.

@Wizmann

Wizmann/benchmark.cc Secret

Created Jan 31, 2019
Embed
What would you like to do?
#include <iostream>
#include <functional>
#include <string>
#include <cassert>
using namespace std;
#define print(x) cout << x << endl
#define input(x) cin >> x
const uint64_t MOD = 0xdeadbeefcafebabeULL;
uint64_t fib(uint64_t& a, uint64_t& b) {
uint64_t c = a + b;
a = b;
b = c;
return c;
}
class ICalc
{
public:
virtual uint64_t fib(uint64_t& a, uint64_t& b) = 0;
};
class Calc : public ICalc
{
public:
virtual uint64_t fib(uint64_t& a, uint64_t& b) override {
uint64_t c = a + b;
a = b;
b = c;
return c;
}
};
class Calc2 : public ICalc
{
public:
virtual uint64_t fib(uint64_t& a, uint64_t& b) override {
return 0;
}
};
int main(int argc, char** argv) {
const int N = 1000000009;
assert(argc == 2);
string cmd = argv[1];
uint64_t a = 1;
uint64_t b = 1;
if (cmd == "FUNC") {
for (int i = 0; i < N; i++) {
fib(a, b);
}
} else if (cmd == "FUNC_PTR") {
uint64_t (*ptr)(uint64_t& a, uint64_t& b) = fib;
for (int i = 0; i < N; i++) {
ptr(a, b);
}
} else if (cmd == "LAMBDA") {
auto lambda = [](uint64_t& a, uint64_t& b) -> uint64_t {
uint64_t c = a + b;
a = b;
b = c;
return c;
};
for (int i = 0; i < N; i++) {
lambda(a, b);
}
} else if (cmd == "VIRTUAL") {
ICalc* c = new Calc();
for (int i = 0; i < N; i++) {
c->fib(a, b);
}
} else if (cmd == "STDFUNCTION") {
function<uint64_t(uint64_t& a, uint64_t& b)> func = fib;
for (int i = 0; i < N; i++) {
func(a, b);
}
} else {
assert(false);
}
print(b);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment