Skip to content

Commit

Permalink
Add another bench on strings that works on vectors and basic operation
Browse files Browse the repository at this point in the history
  • Loading branch information
P-p-H-d committed Jun 1, 2024
1 parent ffb7699 commit 22073a4
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
6 changes: 5 additions & 1 deletion bench/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -466,13 +466,17 @@ bench-string-step5:

bench-string-step6:
$(CXX) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) -I $(MLIB) -DBENCH_CAN_USE_STL -DBENCH_CAN_USE_MSTARLIB bench-string.cpp common.c $(BENCH_DEF) -o bench-string.exe
$(CXX) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) -I $(MLIB) -DBENCH_CAN_USE_STL -DBENCH_CAN_USE_MSTARLIB bench-string-2.cpp common.c $(BENCH_DEF) -o bench-string-2.exe
$(CXX) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) -I $(MLIB) -DBENCH_CAN_USE_STL -DBENCH_CAN_USE_MSTARLIB bench-string-2.cpp common.c $(BENCH_DEF) -o bench-string-2.exe
$(CC) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) -I $(MLIB) -DBENCH_CAN_USE_MSTARLIB bench-string-3-mlib.c -o bench-string-3-mlib.exe
$(CXX) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) -DBENCH_CAN_USE_STL bench-string-3-stl.cpp -o bench-string-3-stl.exe
@./bench-string.exe
@./bench-string-2.exe 2000000 0
@./bench-string-2.exe 2000000 1
@./bench-string-2.exe 2000000 2
@./bench-string-2.exe 2000000 4
@./bench-string-2.exe 2000000 5
@./bench-string-3-mlib.exe
@./bench-string-3-stl.exe

bench-string-pgo:
$(MAKE) bench-string XCFLAGS="-fprofile-generate=pgo"
Expand Down
45 changes: 45 additions & 0 deletions bench/bench-string-3-mlib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "m-string.h"
#include "common.h"

void pass1(int n, string_t tab[n])
{
for(int i = 0; i < n; i++)
string_init(tab[i]);
}

void pass2(int n, string_t tab[n])
{
for(int i = 0; i < n; i++)
string_set(tab[i], "THIS IS IT");
}

void pass3(string_t dst, int n, string_t tab[n])
{
for(int i = 0; i < n; i++)
string_cat(dst, tab[i]);
}

void pass4(int n, string_t tab[n])
{
for(int i = 0; i < n; i++)
string_clear(tab[i]);
}

int main(int argc, const char *argv[])
{
int n = argc >= 2 ? atoi(argv[1]): 200000000;
unsigned long long t0 = cputime();
string_t *tab = (string_t *) malloc (n * sizeof(string_t)), dst;
if (!tab) abort();
string_init(dst);
pass1(n, tab);
pass2(n, tab);
pass3(dst, n, tab);
pass4(n, tab);
size_t s = string_size(dst);
string_clear(dst);
free(tab);
unsigned long long t1 = cputime();
printf("BENCH STRING 3 / M*LIB: TIME=%llu ms LENGTH=%zu\n", t1-t0, s);
exit(0);
}
49 changes: 49 additions & 0 deletions bench/bench-string-3-stl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <string>
#include <iostream>

#include "common.h"

using namespace std;

#define INIT(x) (new (&(x)) string())
#define CLEAR(x) ((&(x))->~string())

void pass1(int n, string tab[])
{
for(int i = 0; i < n; i++)
INIT(tab[i]);
}

void pass2(int n, string tab[])
{
for(int i = 0; i < n; i++)
tab[i] = "THIS IS IT";
}

void pass3(string &dst, int n, string tab[])
{
for(int i = 0; i < n; i++)
dst += tab[i];
}

void pass4(int n, string tab[])
{
for(int i = 0; i < n; i++)
CLEAR(tab[i]);
}

int main(int argc, const char *argv[])
{
int n = argc >= 2 ? atoi(argv[1]): 200000000;
unsigned long long t0 = cputime();
string *tab = (string *) malloc (n * sizeof(string)), dst;
if (!tab) abort();
pass1(n, tab);
pass2(n, tab);
pass3(dst, n, tab);
pass4(n, tab);
free(tab);
unsigned long long t1 = cputime();
cout << "BENCH STRING 3 / STL: TIME=" << t1 - t0 << " ms LENGTH=" << dst.size() << endl;
exit(0);
}

0 comments on commit 22073a4

Please sign in to comment.