Skip to content

Commit 813c397

Browse files
committed
example benchmark
1 parent f5fe1cd commit 813c397

6 files changed

Lines changed: 47 additions & 3 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ target_include_directories(pytokenizer_compile_config
2323
add_subdirectory(csrc/src/core)
2424
add_subdirectory(csrc/bindings)
2525
add_subdirectory(test)
26+
add_subdirectory(benchmarks)
2627

2728
add_library(pytokenizer INTERFACE)
2829
target_link_libraries(pytokenizer INTERFACE pytokenizer_core)

benchmarks/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
project(benchmarks LANGUAGES CXX)
2+
3+
# FETCH CONTENT ---------------------------------
4+
include(FetchContent)
5+
set(FETCHCONTENT_TRY_FIND_PACKAGE TRUE)
6+
7+
FetchContent_Declare(
8+
benchmark
9+
GIT_REPOSITORY https://github.com/google/benchmark.git
10+
GIT_TAG v1.9.0
11+
GIT_SHALLOW TRUE
12+
FIND_PACKAGE_ARGS CONFIG
13+
)
14+
FetchContent_MakeAvailable(benchmark)
15+
16+
# EXECUTABLES ---------------------------------
17+
add_executable(bench bench.cpp)
18+
target_link_libraries(bench
19+
PRIVATE pytokenizer benchmark::benchmark
20+
)

benchmarks/bench.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <benchmark/benchmark.h>
2+
#include "libtokenizer/libtokenizer.hpp"
3+
4+
void register_bench(const std::string& name) {
5+
benchmark::RegisterBenchmark(
6+
name,
7+
[](benchmark::State& state) {
8+
for (auto _ : state) {
9+
pytokenizer::core::Tokenizer tokenizer;
10+
tokenizer.foo();
11+
}
12+
});
13+
}
14+
15+
int main(int argc, char** argv) {
16+
benchmark::Initialize(&argc, argv);
17+
register_bench("foo");
18+
19+
benchmark::RunSpecifiedBenchmarks();
20+
return 0;
21+
}

csrc/include/libtokenizer/core/tokenizer.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#pragma once
22

3+
#include <string>
4+
35
namespace pytokenizer {
46
namespace core {
57

68
class Tokenizer {
79
public:
8-
void foo();
10+
std::string foo();
911
};
1012

1113
} // namespace core

csrc/src/core/tokenizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace pytokenizer {
55
namespace core {
66

7-
void Tokenizer::foo() { std::cout << "foo" << std::endl; }
7+
std::string Tokenizer::foo() { return "foo"; }
88

99
} // namespace core
1010
} // namespace pytokenizer

test/playground.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66

77
int main() {
88
pytokenizer::core::Tokenizer tokenizer;
9-
tokenizer.foo();
9+
std::cout << tokenizer.foo() << std::endl;
1010
return 0;
1111
}

0 commit comments

Comments
 (0)