-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbench.cpp
More file actions
68 lines (57 loc) · 2.03 KB
/
bench.cpp
File metadata and controls
68 lines (57 loc) · 2.03 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <chrono>
#include <ctime>
#include "url.h"
/**
* Run func() `count` times in each of `runs` experiments, where `name` provides a
* meaningful description of the task being benchmarked. Prints out the time for each
* run, the average time, and rate.
*/
template<typename Functor>
void bench(const std::string& name, size_t count, size_t runs, Functor func)
{
std::cout << "Benchmarking " << name << " with " << count << " per run:" << std::endl;
double total(0);
for (size_t run = 0; run < runs; ++run)
{
auto start = std::chrono::high_resolution_clock::now();
for (size_t it = 0; it < count; ++it)
{
func();
}
auto end = std::chrono::high_resolution_clock::now();
double duration = std::chrono::duration<double, std::milli>(end - start).count();
total += duration;
std::cout << " Run " << run << ": " << duration << " ms" << std::endl;
}
std::cout << " Average: " << (total / runs) << " ms" << std::endl;
std::cout << " Rate: " << ((count * runs) / total) << " k-iter / s" << std::endl;
}
int main(int argc, char* argv[]) {
if (argc != 3) {
std::cerr << "bench <base> <relative>" << std::endl;
std::cerr << " Benchmark various transforms of base + relative." << std::endl;
return 1;
}
std::string base(argv[1]);
std::string relative(argv[2]);
size_t count = 1000000;
size_t runs = 5;
Url::Url base_url(base);
std::string full = Url::Url(relative).relative_to(base_url).str();
bench("parse", count, runs, [full]() {
Url::Url parsed(full);
});
bench("relative", count, runs, [base_url, relative]() {
Url::Url(relative).relative_to(base_url);
});
bench("parse + escape", count, runs, [full]() {
Url::Url(full).escape();
});
bench("parse + abspath", count, runs, [full]() {
Url::Url(full).abspath();
});
bench("parse + punycode", count, runs, [full]() {
Url::Url(full).punycode();
});
}