Skip to content

Commit

Permalink
add benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
omaraflak committed Aug 28, 2023
1 parent 887663d commit f0e070e
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "lib/googletest"]
path = lib/googletest
url = https://github.com/google/googletest
[submodule "lib/benchmark"]
path = lib/benchmark
url = [email protected]:google/benchmark.git
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ target_link_libraries(assembler -lffi banana_lib)
add_subdirectory(lib/googletest)
add_executable(banana_test src/banana_test.cpp)
target_link_libraries(banana_test -lffi banana_lib gtest_main)

file(GLOB BENCH_SOURCES "benchmarks/*.cpp")
add_subdirectory(lib/benchmark)
add_executable(benchmarks benchmarks/benchmarks.cpp)
target_link_libraries(benchmarks -lffi banana_lib benchmark)
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ sudo apt install g++ cmake libffi-dev
Compile Banana:

```
git clone --recurse-submodules [email protected]:omaraflak/banana.git
cd banana
cmake .
make
```
Expand Down
28 changes: 28 additions & 0 deletions benchmarks/benchmarks.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <benchmark/benchmark.h>
#include "../src/lib/ast.h"
#include "../src/lib/vm.h"
#include "../src/lib/instructions.h"
#include "../src/lib/fileutils.h"
#include "../src/lib/scanner.h"
#include "../src/lib/parser.h"

#define PATH(name) "benchmarks/" #name ".na"

#define NA_BENCHMARK(name) \
static void bm_##name(benchmark::State &state) { \
auto content = fileutils::read_string(PATH(name)); \
auto tokens = scanner::scan(content.c_str()); \
auto tree = parser::parse(tokens); \
auto instructions = ast::to_instructions(tree); \
auto bytes = Instruction::to_bytes(instructions); \
for (auto _ : state) { \
Vm(bytes).execute(); \
} \
} \
BENCHMARK(bm_##name) \

NA_BENCHMARK(fib);
NA_BENCHMARK(primes);

// Run the benchmark
BENCHMARK_MAIN();
8 changes: 8 additions & 0 deletions benchmarks/fib.na
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
long fib(long n) {
if (n == 1 or n == 2) {
return n;
}
return fib(n - 1) + fib(n - 2);
}

fib(20);
19 changes: 19 additions & 0 deletions benchmarks/primes.na
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
long isprime(long n) {
for (long i = 2; i < n; i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}

long main() {
long n = 1000;
long i = 3;
while (n > 1) {
if (isprime(i) == 1) {
n--;
}
i += 2;
}
}
1 change: 1 addition & 0 deletions lib/benchmark
Submodule benchmark added at db3e00

0 comments on commit f0e070e

Please sign in to comment.