Skip to content
This repository was archived by the owner on Oct 8, 2022. It is now read-only.

Commit 065f839

Browse files
authored
redo benchmarks a bit
1 parent 07c0125 commit 065f839

File tree

2 files changed

+169
-96
lines changed

2 files changed

+169
-96
lines changed

benchmark/bench_1.d

Lines changed: 0 additions & 96 deletions
This file was deleted.

benchmark/benchmark.d

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
module benchmark.benchmark;
2+
3+
import std.stdio : writef, writeln, stdout;
4+
import std.conv : to, text;
5+
6+
void main()
7+
{
8+
writef("\033[2J");
9+
writef("\033[1;1H");
10+
stdout.flush();
11+
12+
immutable height = 60;
13+
immutable repeatMultiplier = 10_000;
14+
immutable repeat = (height * repeatMultiplier);
15+
16+
void delegate()[] benchmarks;
17+
18+
string[][] results;
19+
20+
/+
21+
benchmarks = [
22+
() {
23+
foreach (i; 0 .. repeat)
24+
{
25+
auto pos = (i % 2) + 1;
26+
writef("\033[%d;%dH", pos, pos);
27+
}
28+
29+
stdout.flush();
30+
}, () {
31+
foreach (i; 0 .. repeat)
32+
{
33+
auto pos = (i % 2) + 1;
34+
writef("\033[%d;%dH", pos, pos);
35+
stdout.flush();
36+
}
37+
}, () {
38+
foreach (i; 0 .. repeatMultiplier)
39+
{
40+
string data = "";
41+
42+
foreach (j; 0 .. height)
43+
{
44+
auto pos = ((j * i) % 2) + 1;
45+
data ~= text("\033[", pos, ";", pos, "H");
46+
}
47+
48+
writef(data);
49+
}
50+
}, () {
51+
foreach (i; 0 .. repeatMultiplier)
52+
{
53+
string data = "";
54+
55+
foreach (j; 0 .. height)
56+
{
57+
auto pos = ((j * i) % 2) + 1;
58+
data ~= text("\033[", pos, ";", pos, "H");
59+
}
60+
61+
writef(data);
62+
stdout.flush();
63+
}
64+
}
65+
];
66+
+/
67+
68+
69+
results ~= benchmark(benchmarks);
70+
71+
/+ sample output on github codespaces
72+
73+
1: 327 ms, 110 μs, and 7 hnsecs
74+
2: 5 secs, 931 ms, 412 μs, and 9 hnsecs
75+
3: 734 ms, 360 μs, and 3 hnsecs
76+
4: 597 ms, 116 μs, and 7 hnsecs
77+
78+
+/
79+
80+
benchmarks = [
81+
() {
82+
foreach (i; 0 .. repeat)
83+
{
84+
auto character = ['1', '2'][i % 2];
85+
writef("\033[1;1H%s %s",
86+
character, character);
87+
}
88+
}, () {
89+
foreach (i; 0 .. repeat)
90+
{
91+
auto character = ['1', '2'][i % 2];
92+
writef("\033[1;1H%s\033[1;80H%s", character, character);
93+
}
94+
}, () {
95+
foreach (i; 0 .. repeat)
96+
{
97+
auto character = ['1', '2'][i % 2];
98+
writef("\033[1;1H%s %s", character, character);
99+
}
100+
}, () {
101+
foreach (i; 0 .. repeat)
102+
{
103+
auto character = ['1', '2'][i % 2];
104+
writef("\033[1;1H%s\033[1;3H%s", character, character);
105+
}
106+
}, () {
107+
foreach (i; 0 .. repeat)
108+
{
109+
// 10 changed characters
110+
auto character = ['1', '2'][i % 2];
111+
writef("\033[1;1H%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
112+
character, " ", character, " ", character, " ",
113+
character, " ", character, " ", character, " ", character,
114+
" ", character, " ", character, " ", character);
115+
}
116+
}, () {
117+
foreach (i; 0 .. repeat)
118+
{
119+
// 10 changed characters
120+
auto character = ['1', '2'][i % 2];
121+
writef("\033[1;1H%s\033[1;3H%s\033[1;5H%s\033[1;7H%s\033[1;9H%s\033[1;11H%s\033[1;13H%s\033[1;15H%s\033[1;17H%s\033[1;19H%s",
122+
character, character, character, character, character,
123+
character, character, character, character, character);
124+
}
125+
},
126+
];
127+
128+
results ~= benchmark(benchmarks);
129+
130+
printResults(results);
131+
}
132+
133+
auto benchmark(void delegate()[] benchmarks)
134+
{
135+
import std.datetime.stopwatch : StopWatch;
136+
137+
auto sw = StopWatch();
138+
sw.stop();
139+
sw.reset();
140+
141+
string[] results;
142+
foreach (benchmark; benchmarks)
143+
{
144+
sw.reset();
145+
sw.start();
146+
benchmark();
147+
sw.stop();
148+
stdout.flush();
149+
results ~= to!string(sw.peek);
150+
}
151+
152+
return results;
153+
}
154+
155+
void printResults(string[][] results)
156+
{
157+
writef("\033[2J");
158+
writef("\033[1;1H");
159+
stdout.flush();
160+
161+
foreach (r; results)
162+
{
163+
writeln;
164+
foreach (n, string result; r)
165+
{
166+
writeln(n + 1, ": ", result);
167+
}
168+
}
169+
}

0 commit comments

Comments
 (0)