-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.cpp
60 lines (46 loc) · 1.31 KB
/
benchmark.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <chrono>
#include <iostream>
#include "compiler.hpp"
#include "parser.hpp"
#include "vm.hpp"
int main() {
std::string input = R"(
let fibonacci = fn(x) {
if (x == 0) {
return 0;
} else {
if (x == 1) {
return 1;
} else {
fibonacci(x - 1) + fibonacci(x - 2);
}
}
};
fibonacci(35);
)";
auto l = Lexer(input);
auto p = Parser(std::move(l));
auto program = p.parse_program();
auto comp = new_compiler();
auto err = comp->compile(program);
if (err) {
std::cerr << "compiler error: " << err->message << std::endl;
return EXIT_FAILURE;
}
auto machine = VM(comp->bytecode());
using std::chrono::high_resolution_clock;
using std::chrono::duration;
using std::chrono::milliseconds;
auto t1 = high_resolution_clock::now();
// Execute benchmark run
err = machine.run();
if (err) {
std::cerr << "vm error: " << err->message << std::endl;
return EXIT_FAILURE;
}
auto t2 = high_resolution_clock::now();
duration<double, std::milli> benchmark_time = t2 - t1;
auto result = machine.last_popped_stack_elem();
std::cout << "engine=vm, result=" << result->inspect() << ", duration=" << benchmark_time.count() / 1000 << std::endl;
return EXIT_SUCCESS;
}