-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_printf.cpp
More file actions
139 lines (121 loc) · 3.68 KB
/
string_printf.cpp
File metadata and controls
139 lines (121 loc) · 3.68 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <assert.h>
#include <iostream>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <typeinfo>
static struct timeval
tv_now()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv;
}
static long
tv_sub_msec(struct timeval end, struct timeval start)
{
return (end.tv_sec - start.tv_sec) * 1000 + (end.tv_usec - start.tv_usec) / 1000;
}
struct v1 {
public:
inline int
string_printf_impl(std::string &output, const char *format, ...)
{
// Tru to the space at the end of output for our output buffer.
// Find out write point then inflate its size temporarily to its
// capacity; we will later shrink it to the size needed to represent
// the formatted string. If this buffer isn't large enough, we do a
// resize and try again.
va_list args;
va_start(args, format);
const int write_point = output.size();
int remaining = output.capacity() - write_point;
output.resize(output.capacity());
va_list copied_args;
va_copy(copied_args, args);
int bytes_used = vsnprintf(&output[write_point], remaining, format, copied_args);
va_end(copied_args);
if (bytes_used < 0) {
return -1;
} else if (bytes_used < remaining) {
// There was enough room, just shrink and return.
output.resize(write_point + bytes_used);
} else {
output.resize(write_point + bytes_used + 1);
remaining = bytes_used + 1;
bytes_used = vsnprintf(&output[write_point], remaining, format, args);
if (bytes_used + 1 != remaining) {
return -1;
}
output.resize(write_point + bytes_used);
}
return 0;
}
};
static __thread char s_tls_printf_buf[4096];
struct v2 {
public:
inline int
string_printf_impl(std::string &output, const char *format, ...)
{
va_list args;
va_start(args, format);
va_list copied_args;
va_copy(copied_args, args);
int bytes_used = vsnprintf(s_tls_printf_buf, sizeof(s_tls_printf_buf), format, copied_args);
va_end(copied_args);
if (bytes_used < 0) {
return -1;
}
if (bytes_used >= sizeof(s_tls_printf_buf)) {
size_t bufsz = bytes_used + 1;
char *buf = (char *)malloc(bufsz);
bytes_used = vsnprintf(buf, bufsz, format, args);
if (bytes_used + 1 != bufsz) {
free(buf);
return -1;
}
output.append(buf, bytes_used);
free(buf);
} else {
output.append(s_tls_printf_buf, bytes_used);
}
return 0;
}
};
template <typename T>
void
test(int n)
{
struct timeval tv = tv_now();
std::string s;
T t;
for (int i = 0; i < n; i++) {
int rc = t.string_printf_impl(s,
"lsjdflsjdlfjsldfjlsdjflsdjflsjdlfkjasljflasjflajslfjaslkdjflasjdfljasldfjlasjdflasjldf"
"jasljflkasjdflkajslfjlasjflajslkdfjalksdjflasjdlfjasldjflasjdlfjasldfjlasjdflasjlkdfjl"
"asdjflasjdlfjsd i is %d\n",
i);
assert(rc == 0);
}
printf("%s %s: loop %d times, taken %ld msec, s.length = %ld\n", __func__, typeid(T).name(), n,
tv_sub_msec(tv_now(), tv), s.length());
}
int
main(int argc, const char **argv)
{
int n = 30000;
if (argc > 1) {
n = atoi(argv[1]);
}
test<v1>(n);
test<v2>(n);
}
/*
* Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
$ ./string_printf 100000
test 2v1: loop 100000 times, taken 12539 msec, s.length = 19888890
test 2v2: loop 100000 times, taken 25 msec, s.length = 19888890
*
*/