-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.cpp
More file actions
44 lines (39 loc) · 1.06 KB
/
file.cpp
File metadata and controls
44 lines (39 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <benchmark/benchmark.h>
#include <cstdio>
#include <fstream>
void BM_file_open_close(benchmark::State &state) {
std::ios::sync_with_stdio(true);
while (state.KeepRunning()) {
std::fstream f("main.cpp");
}
}
BENCHMARK(BM_file_open_close);
void BM_file_open_close_no_sync(benchmark::State &state) {
std::ios::sync_with_stdio(false);
while (state.KeepRunning()) {
std::fstream f("main.cpp");
}
}
BENCHMARK(BM_file_open_close_no_sync);
void BM_file_read_1M(benchmark::State &state) {
std::vector<char> buffer(1024 * 1024, 0);
{
std::fstream f("text");
f.write(buffer.data(), buffer.size());
}
std::ios::sync_with_stdio(false);
while (state.KeepRunning()) {
std::fstream f("text");
f.read(buffer.data(), buffer.size());
}
remove("text");
}
BENCHMARK(BM_file_read_1M);
void BM_cfile_open_close(benchmark::State &state) {
while (state.KeepRunning()) {
if (auto f = std::fopen("main.cpp", "r")) {
fclose(f);
}
}
}
BENCHMARK(BM_cfile_open_close);