-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
66 lines (56 loc) · 4.72 KB
/
main.cpp
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
#include <iostream>
#include <ctime>
#include <fmt/format.h>
#include <fmt/compile.h>
#include <QString>
int main() {
struct timespec before{}, after{};
std::string a{
R"(To use the {fmt} library, add fmt/core.h, fmt/format.h, fmt/format-inl.h, src/format.cc and optionally other headers fromTo use the {fmt} library, add fmt/core.h, fmt/format.h, fmt/format-inl.h, src/format.cc and optionally other headers fromTo use the {fmt} library, add fmt/core.h, fmt/format.h, fmt/format-inl.h, src/format.cc and optionally other headers fromTo use the {fmt} library, add fmt/core.h, fmt/format.h, fmt/format-inl.h, src/format.cc and optionally other headers fromTo use the {fmt} library, add fmt/core.h, fmt/format.h, fmt/format-inl.h, src/format.cc and optionally other headers from)"};
std::string b{
R"(To use the {fmt} library, add fmt/core.h, fmt/format.h, fmt/format-inl.h, src/format.cc and optionally other headers fromTo use the {fmt} library, add fmt/core.h, fmt/format.h, fmt/format-inl.h, src/format.cc and optionally other headers fromTo use the {fmt} library, add fmt/core.h, fmt/format.h, fmt/format-inl.h, src/format.cc and optionally other headers fromTo use the {fmt} library, add fmt/core.h, fmt/format.h, fmt/format-inl.h, src/format.cc and optionally other headers fromTo use the {fmt} library, add fmt/core.h, fmt/format.h, fmt/format-inl.h, src/format.cc and optionally other headers fromTo use the {fmt} library, add fmt/core.h, fmt/format.h, fmt/format-inl.h, src/format.cc and optionally other headers fromTo use the {fmt} library, add fmt/core.h, fmt/format.h, fmt/format-inl.h, src/format.cc and optionally other headers fromTo use the {fmt} library, add fmt/core.h, fmt/format.h, fmt/format-inl.h, src/format.cc and optionally other headers fromTo use the {fmt} library, add fmt/core.h, fmt/format.h, fmt/format-inl.h, src/format.cc and optionally other headers fromTo use the {fmt} library, add fmt/core.h, fmt/format.h, fmt/format-inl.h, src/format.cc and optionally other headers fromTo use the {fmt} library, add fmt/core.h, fmt/format.h, fmt/format-inl.h, src/format.cc and optionally other headers fromTo use the {fmt} library, add fmt/core.h, fmt/format.h, fmt/format-inl.h, src/format.cc and optionally other headers fromTo use the {fmt} library, add fmt/core.h, fmt/format.h, fmt/format-inl.h, src/format.cc and optionally other headers fromTo use the {fmt} library, add fmt/core.h, fmt/format.h, fmt/format-inl.h, src/format.cc and optionally other headers from)"};
std::string c{
R"(To use the {fmt} library, add fmt/core.h, fmt/format.h, fmt/format-inl.h, src/format.cc and optionally other headers from)"};
unsigned long a1 = 0, a2 = 0, a3 = 0, a4 = 0, a5 = 0;
#define LOOP 4
std::string r1{}, r2{}, r3{}, r4{}, r5{};
for (int i = 0; i < LOOP; ++i) {
clock_gettime(CLOCK_MONOTONIC, &before);
r1 = QString(R"(%1Long text Long text Long text Long text%2Long text Long text Long text Long text%3)").arg(
a.c_str(), b.c_str(), c.c_str()).toStdString();
clock_gettime(CLOCK_MONOTONIC, &after);
a1 += after.tv_nsec - before.tv_nsec;
clock_gettime(CLOCK_MONOTONIC, &before);
r2 = fmt::format("{}Long text Long text Long text Long text{}Long text Long textLong text Long text{}", a, b,
c);
clock_gettime(CLOCK_MONOTONIC, &after);
a2 += after.tv_nsec - before.tv_nsec;
clock_gettime(CLOCK_MONOTONIC, &before);
r3 = fmt::format(
FMT_COMPILE(R"({}Long text Long text Long text Long text{}Long text Long textLong text Long text{})"),
a, b, c);
clock_gettime(CLOCK_MONOTONIC, &after);
a3 += after.tv_nsec - before.tv_nsec;
clock_gettime(CLOCK_MONOTONIC, &before);
r4 = a;
r4.append("Long text Long text Long text Long text");
r4.append(b);
r4.append("Long text Long text Long text Long text");
r4.append(c);
clock_gettime(CLOCK_MONOTONIC, &after);
a4 += after.tv_nsec - before.tv_nsec;
clock_gettime(CLOCK_MONOTONIC, &before);
r5 = a;
fmt::format_to(std::back_inserter(r5),
FMT_COMPILE(R"({}Long text Long text Long text Long text{}Long text Long textLong text Long text{})"),
a, b, c);
clock_gettime(CLOCK_MONOTONIC, &after);
a5 += after.tv_nsec - before.tv_nsec;
}
std::cout << a1 / LOOP << " ns QString arg" << std::endl;
std::cout << a2 / LOOP << " ns fmt::format" << std::endl;
std::cout << a3 / LOOP << " ns fmt::format+FMT_COMPILE" << std::endl;
std::cout << a4 / LOOP << " ns just append" << std::endl;
std::cout << a5 / LOOP << " ns format_to + FMT_COMPILE" << std::endl;
return 0;
}