-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfmt-vs-tostr.cpp
More file actions
87 lines (77 loc) · 2.13 KB
/
fmt-vs-tostr.cpp
File metadata and controls
87 lines (77 loc) · 2.13 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <random>
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <format>
#include <iostream>
#include <sstream>
#include <string>
typedef int64_t msec_t;
#if defined(__WIN32__)
#include <windows.h>
msec_t
currentTimeInMillis(void)
{
return timeGetTime();
}
#else
#include <sys/time.h>
msec_t
currentTimeInMillis(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (msec_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
#endif
int
main(int argc, char* argv[])
{
msec_t start;
msec_t stop;
long MAXCOUNT = 10000000;
std::srand(123);
int* numbers = new int[MAXCOUNT];
for (int i = 0; i < MAXCOUNT; i++) {
numbers[i] = std::rand();
}
{
std::string result;
result.reserve(MAXCOUNT * 100);
start = currentTimeInMillis();
for (int i = 0; i < MAXCOUNT; i++) {
std::format_to(
std::back_inserter(result), "Number {} is great!", numbers[i]);
}
stop = currentTimeInMillis();
assert(result.size() > MAXCOUNT);
std::cout << "timing fmt: " << stop - start
<< " ms / string length: " << result.size() << std::endl;
}
{
std::string result;
result.reserve(MAXCOUNT * 100);
start = currentTimeInMillis();
for (int i = 0; i < MAXCOUNT; i++) {
result += "Number " + std::to_string(numbers[i]) + " is great!";
}
stop = currentTimeInMillis();
assert(result.size() > MAXCOUNT);
std::cout << "timing to_string: " << stop - start
<< " ms / string length: " << result.size() << std::endl;
}
{
std::string result;
result.reserve(MAXCOUNT * 100);
std::ostringstream ss;
start = currentTimeInMillis();
for (int i = 0; i < MAXCOUNT; i++) {
ss << "Number " << numbers[i] << " is great!";
}
result = ss.str();
stop = currentTimeInMillis();
assert(result.size() > MAXCOUNT);
std::cout << "timing stds: " << stop - start
<< " ms / string length: " << result.size() << std::endl;
}
}