-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstring_conversion.cpp
More file actions
271 lines (243 loc) · 8.62 KB
/
Copy pathstring_conversion.cpp
File metadata and controls
271 lines (243 loc) · 8.62 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/************************************************************
* String conversion using std::from_chars *
* and std::to_chars *
************************************************************/
/*!
* @brief C++17 introduces std::from_chars and std::to_chars
* to perform fast, low-level string conversions.
*
* See https://en.cppreference.com/w/cpp/utility/from_chars
* and https://en.cppreference.com/w/cpp/utility/to_chars
* for more details.
*/
/*!
* @note Here are a list of conversion functions
* that are provided by C++ :
* - sprintf
* - snprintf
* - sscanf
* - atol
* - strtol
* - strstream
* - stringstream
* - to_string
* - stoi
*
* We will try to compare their performances
* with a basic benchmark.
* We will perform comparison between the following :
* - from_chars/to_chars
* - stoi/to_string
* - atoi/sprintf
* - stringstream/ostringstream
*/
#include <iostream>
#include <string>
#include <array>
#include <algorithm>
#include <charconv>
#include <random>
#include <chrono>
#include <thread>
#include <sstream>
#include <stdlib.h>
#define CYCLE_NB 1000
#define ELEMENTS 1000
//////////////////////////////////////////////////////////////////////////////////////////
/*
* @brief A stopwatch class to perform measures
*/
template < typename Clock = std::chrono::high_resolution_clock >
class stopwatch
{
private:
const std::string m_title;
const typename Clock::time_point m_start;
public:
stopwatch(const std::string& p_title = "") : m_title(p_title), m_start( Clock::now() ) {}
~stopwatch() {
std::cout << "Computation using " << m_title << " performed in "
<< elapsed_time<unsigned int, std::chrono::milliseconds>() << " ms\n";
}
template < typename Rep = typename Clock::duration::rep,
typename Units = typename Clock::duration >
Rep elapsed_time(void) const
{
std::atomic_thread_fence(std::memory_order_relaxed);
auto l_time = std::chrono::duration_cast<Units>(Clock::now() - m_start).count();
std::atomic_thread_fence(std::memory_order_relaxed);
return static_cast<Rep>(l_time);
}
};
using precise_stopwatch = stopwatch<>;
using system_stopwatch = stopwatch<std::chrono::system_clock>;
using monotonic_stopwatch = stopwatch<std::chrono::steady_clock>;
//////////////////////////////////////////////////////////////////////////////////////////
/*!
* @brief Creates a vector of size p_size
* filled with random integers.
*/
std::vector<int> createIntVector(int p_size)
{
std::uniform_int_distribution<int> distrib( 0, INT_MAX );
std::default_random_engine random_engine;
std::vector<int> myVec( p_size );
for ( auto& v : myVec ) { v = distrib(random_engine); }
return myVec;
}
//////////////////////////////////////////////////////////////////////////////////////////
int main()
{
std::cout << "\n--------------------------------------------------\n";
std::cout << "\t\tPARAMETERS\nElements - " << ELEMENTS
<< "\nNumber of cycles - " << CYCLE_NB;
std::cout << "\n--------------------------------------------------\n";
auto myIntVec{createIntVector (ELEMENTS)};// Vector of integers to convert to string
std::vector<std::string> myStrVec(ELEMENTS); // Vector containing the conversion results
std::vector<int> myResVec(ELEMENTS); // Vector containing the back conversion results
std::string resStr (15, ' '); // String holding the result of std::to_chars()
// std::to_chars() and st::from_chars()
{
{
stopwatch myWatch("std::to_chars()");
for ( size_t cycle = 0; cycle < CYCLE_NB; ++cycle )
{
for ( size_t elm = 0; elm < myIntVec.size(); ++elm )
{
const auto l_res = std::to_chars( resStr.data(),
resStr.data() + resStr.size(),
myIntVec[elm] );
myStrVec[elm] = std::string_view( resStr.data(),
l_res.ptr - resStr.data());
}
}
}
{
stopwatch myWatch("std::from_chars()");
for ( size_t cycle = 0; cycle < CYCLE_NB; ++cycle )
{
for ( size_t elm = 0; elm < myIntVec.size(); ++elm )
{
std::from_chars( myStrVec[elm].data(),
myStrVec[elm].data() + myStrVec[elm].size(),
myResVec[elm] );
}
}
}
// Make sure final output is the same as input
// i.e. myResVec == myIntVec for each element
if ( myIntVec != myResVec )
{
std::cout << "SOMETHING WENT WRONG!\n";
}
}
// std::to_string() and std::stoi()
{
{
stopwatch myWatch("std::to_string()");
for ( size_t cycle = 0; cycle < CYCLE_NB; ++cycle )
{
for ( size_t elm = 0; elm < myIntVec.size(); ++elm )
{
myStrVec[elm] = std::to_string( myIntVec[elm] );
}
}
}
{
stopwatch myWatch("std::stoi()");
for ( size_t cycle = 0; cycle < CYCLE_NB; ++cycle )
{
for ( size_t elm = 0; elm < myIntVec.size(); ++elm )
{
myResVec[elm] = std::stoi( myStrVec[elm] );
}
}
}
// Make sure final output is the same as input
// i.e. myResVec == myIntVec for each element
if ( myIntVec != myResVec )
{
std::cout << "SOMETHING WENT WRONG!\n";
}
}
// std::sprintf() and std::atoi()
{
{
stopwatch myWatch("std::sprintf()");
for ( size_t cycle = 0; cycle < CYCLE_NB; ++cycle )
{
for ( size_t elm = 0; elm < myIntVec.size(); ++elm )
{
std::sprintf( myStrVec[elm].data(), "%d", myIntVec[elm] );
}
}
}
{
stopwatch myWatch("std::atoi()");
for ( size_t cycle = 0; cycle < CYCLE_NB; ++cycle )
{
for ( size_t elm = 0; elm < myIntVec.size(); ++elm )
{
myResVec[elm] = std::atoi( myStrVec[elm].c_str() );
}
}
}
// Make sure final output is the same as input
// i.e. myResVec == myIntVec for each element
if ( myIntVec != myResVec )
{
std::cout << "SOMETHING WENT WRONG!\n";
}
}
// std::ostringstream and std::stringstream
{
{
stopwatch myWatch("std::ostringstream()");
std::ostringstream ss;
for ( size_t cycle = 0; cycle < CYCLE_NB; ++cycle )
{
for ( size_t elm = 0; elm < myIntVec.size(); ++elm )
{
ss << myIntVec[elm];
myStrVec[elm] = ss.str();
ss.str(std::string());
}
}
}
{
stopwatch myWatch("std::stringstream()");
std::stringstream ss;
for ( size_t cycle = 0; cycle < CYCLE_NB; ++cycle )
{
for ( size_t elm = 0; elm < myIntVec.size(); ++elm )
{
ss << myStrVec[elm];
ss >> myResVec[elm];
ss.str("");
}
}
}
// Make sure final output is the same as input
// i.e. myResVec == myIntVec for each element
if (myIntVec != myResVec)
{
std::cout << "SOMETHING WENT WRONG!\n";
}
}
return EXIT_SUCCESS;
}
/*
--------------------------------------------------
PARAMETERS
Elements - 1000
Number of cycles - 1000
--------------------------------------------------
Computation using std::to_chars() performed in 23 ms
Computation using std::from_chars() performed in 12 ms
Computation using std::to_string() performed in 65 ms
Computation using std::stoi() performed in 68 ms
Computation using std::sprintf() performed in 54 ms
Computation using std::atoi() performed in 33 ms
Computation using std::ostringstream() performed in 64 ms
Computation using std::stringstream() performed in 18 ms
*/