Added String benchmark function
All checks were successful
clang-build / clang-build (push) Successful in 30s
gcc-build / gcc-build (push) Successful in 21s
clang-build / clang-build (pull_request) Successful in 31s
gcc-build / gcc-build (pull_request) Successful in 20s

This commit is contained in:
2026-03-16 09:53:07 +01:00
parent f8a77a6056
commit de22c706af
3 changed files with 37 additions and 5 deletions

View File

@@ -51,7 +51,7 @@ $(OBJ_DIR):
mkdir -p $(OBJ_DIR)
# Benchmark rules
$(BENCH_TARGET): $(BENCH_OBJ_DIR)/bench.o $(BENCH_OBJ_DIR)/vector.o $(BENCH_OBJ_DIR)/map.o $(BENCH_OBJ_DIR)/bigint.o
$(BENCH_TARGET): $(BENCH_OBJ_DIR)/bench.o $(BENCH_OBJ_DIR)/vector.o $(BENCH_OBJ_DIR)/map.o $(BENCH_OBJ_DIR)/bigint.o $(BENCH_OBJ_DIR)/string.o
$(CC) $(BENCH_FLAGS) -o $@ $^
$(BENCH_OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(BENCH_OBJ_DIR)

View File

@@ -187,14 +187,16 @@ $ ./test_bigint
```
## Benchmark
Under the [`benchmark/`](/benchmark/) folder, you can find a simple benchmark program that stress the `Vector` and the `Map` data structures. You can run it by issuing the following command:
Under the [`benchmark/`](/benchmark/) folder, you can find a very simple benchmark program that stress the data structures.
You can run it by issuing the following command:
```sh
$ make clean all CC=clang
$ ./benchmark_datum
omputing Vector average time...average time: 8 ms
Computing Map average time...average time: 53 ms
Computing BigInt average time...average time: 76 ms
Computing Vector average time...average time: 6 ms
Computing Map average time...average time: 49 ms
Computing BigInt average time...average time: 67 ms
Computing String average time...average time: 13 ms
```

View File

@@ -7,6 +7,7 @@
#include "../src/vector.h"
#include "../src/map.h"
#include "../src/bigint.h"
#include "../src/string.h"
typedef void (*test_fn_t)(size_t iterations);
@@ -116,6 +117,31 @@ void test_bigint(size_t iterations) {
}
}
void test_string(size_t iterations) {
volatile size_t total_len = 0;
for (size_t idx = 0; idx < iterations; idx++) {
string_t *str1 = string_new("hello").value.string;
string_t *str2 = string_new(" World").value.string;
string_result_t concat = string_concat(str1, str2);
string_result_t upper = string_to_upper(concat.value.string);
total_len += string_size(upper.value.string);
string_result_t needle = string_new("WORLD");
string_result_t contains = string_contains(upper.value.string, needle.value.string);
if (contains.value.idx >= 0) {
total_len += contains.value.idx;
}
string_destroy(str1);
string_destroy(str2);
string_destroy(concat.value.string);
string_destroy(upper.value.string);
string_destroy(needle.value.string);
}
}
static inline uint64_t now_ns(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
@@ -155,5 +181,9 @@ int main(void) {
fflush(stdout);
printf("average time: %lld ms\n", benchmark(test_bigint, 1e5, 30));
printf("Computing String average time...");
fflush(stdout);
printf("average time: %lld ms\n", benchmark(test_string, 1e5, 30));
return 0;
}