Skip to content
This repository was archived by the owner on Dec 1, 2017. It is now read-only.

Commit 668443e

Browse files
committed
add benchmark for genRandomId method
1 parent 8051589 commit 668443e

File tree

5 files changed

+68
-1
lines changed

5 files changed

+68
-1
lines changed

benchmark/meson.build

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
randIdGen_exe = executable('randIdGen', 'randomIdGen.cpp')
2+
benchmark('randIdGen, not optimized', randIdGen_exe)
3+
4+
op_randIdGen_exe = executable('op_randIdGen', 'op_randomIdGen.cpp')
5+
benchmark('op_randIdGen, optimized', op_randIdGen_exe)

benchmark/op_randomIdGen.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <random>
2+
#include <string>
3+
#include <vector>
4+
5+
6+
void genAndFill(std::vector<std::string> &v)
7+
{
8+
std::string ss;
9+
ss.resize(8, ' ');
10+
11+
constexpr char alphanum[] = "0123456789"
12+
"abcdefghijklmnopqrstuvwxyz"
13+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
14+
15+
static const auto seed = std::random_device()();
16+
static std::mt19937 gen(seed);
17+
std::uniform_int_distribution<> dis(0, 61);
18+
19+
for (int i = 0; i < 8; ++i) {
20+
ss[i] = alphanum[dis(gen)];
21+
}
22+
v.push_back(ss);
23+
}
24+
25+
int main()
26+
{
27+
std::vector<std::string> v;
28+
v.reserve(500000);
29+
30+
for (auto l = 0; l < 500000; ++l) {
31+
genAndFill(v);
32+
}
33+
}

benchmark/randomIdGen.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <random>
2+
#include <string>
3+
#include <vector>
4+
5+
6+
void genAndFill(std::vector<std::string> &v)
7+
{
8+
const char alphanum[] = "0123456789"
9+
"abcdefghijklmnopqrstuvwxyz"
10+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
11+
char ss[8];
12+
std::random_device rd;
13+
std::mt19937 gen(rd());
14+
15+
for (int i = 0; i < 8; i++) {
16+
std::uniform_int_distribution<> dis(0, 61);
17+
ss[i] = alphanum[dis(gen)];
18+
}
19+
v.push_back(ss);
20+
}
21+
22+
int main()
23+
{
24+
std::vector<std::string> v;
25+
for (auto l = 0; l < 500000; l++) {
26+
genAndFill(v);
27+
}
28+
}

build.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ set -ex
44

55
mkdir -p build
66
cd build/
7-
(meson --default-library=static .. && ninja) || (ninja && ./test/testexe)
7+
(meson --default-library=static .. --warnlevel=3 && ninja) || (ninja && ./test/testexe)

meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ endif
3131
subdir('src')
3232
subdir('app')
3333
subdir('test')
34+
subdir('benchmark')
3435

3536
emulate_rasp = find_program('scripts/emulate_rasp.sh')
3637
run_target('emulate', command : emulate_rasp.path())

0 commit comments

Comments
 (0)