-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestHardcodedText.cpp
More file actions
141 lines (128 loc) · 5.29 KB
/
testHardcodedText.cpp
File metadata and controls
141 lines (128 loc) · 5.29 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
#include <string>
#include <iostream>
#include <sstream>
#include <string_view>
#include <chrono>
static void escape(void *p){
asm volatile("" : : "g"(p) : "memory");
}
static void clobber(){
asm volatile("" : : : "memory");
}
void smallStringReplace(std::string& str,
const std::string& oldStr,
const std::string& newStr)
{
std::string::size_type pos = 0u;
while((pos = str.find(oldStr, pos)) != std::string::npos){
str.replace(pos, oldStr.length(), newStr);// O(X * L)
pos += newStr.length();
}
}
void stringStreamReplace(
std::string& s,
std::string const& oldStr,
std::string const& newStr
) {
std::ostringstream oss;
std::size_t pos = 0;
std::size_t prevPos;
while (true) {
prevPos = pos;
pos = s.find(oldStr, pos);
if (pos == std::string::npos)
break;
oss << s.substr(prevPos, pos - prevPos); // O(L)
oss << newStr;
pos += oldStr.size();
}
oss << s.substr(prevPos);
s = oss.str();
}
std::string insertReplacingChars(const std::string input, const std::string toInsert,
const int start ,const int interval){
size_t insertPos = start;
std::string copy = input;
while(insertPos < copy.size()){
copy.insert(insertPos, toInsert);
// interval + added special
insertPos += interval+toInsert.size();
}
return copy;
}
int main(){
// https://stackoverflow.com/a/21710033/4123703
// small str on 64bit platform. 22chars
// Larger one
const char* smallStr = "someone@somehewhereothersmaller.com";
// Use Gutenberg project txt, https://www.gutenberg.org/cache/epub/2600/pg2600.txt
// Insert 4\r
const char* longStr = "“Heavens! what a virulent attack!” replied the prince, not in theleast disconcerted by this reception. He had just entered, wearing anembroidered court uniform, knee breeches, and shoes, and had stars onhis breast and a serene expression on his flat face. He spoke in thatrefined French in which our grandfathers not only spoke but thought, andwith the gentle, patronizing intonation natural to a man of importancewho had grown old in society and at court. He went up to Anna Pávlovna,kissed her hand, presenting to her his bald, scented, and shining head,and complacently seated himself on the sofa.";
static std::string output;
std::string insertChar = "\r";
int insertInterval = 1;
int iteration = 2;
int insertStart = rand()*5;
{
using unit = std::chrono::nanoseconds;
auto totalDuration = 0;
for(int i = 0; i < iteration; i++){
std::string lstr = insertReplacingChars(longStr, insertChar, insertStart + i, insertInterval);
auto start = std::chrono::high_resolution_clock::now();
smallStringReplace(lstr, "\r", "\\r");
auto finish = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<unit>(finish - start).count();
output += lstr;
std::cout << "smallStringReplace() long " << duration << "\n";
totalDuration += duration;
}
std::cout << "smallStringReplace() long string took total "
<< totalDuration
<< " nanoseconds\n";
totalDuration = 0;
for(int i = 0; i < iteration; i++){
std::string sstr = insertReplacingChars(smallStr, insertChar, insertStart + i , insertInterval);
auto start = std::chrono::high_resolution_clock::now();
smallStringReplace(sstr, "\r", "\\r");
auto finish = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<unit>(finish - start).count();
output += sstr;
std::cout << "smallStringReplace() small " << duration << "\n";
totalDuration += duration;
}
std::cout << "smallStringReplace() small string took total "
<< totalDuration
<< " nanoseconds\n";
}
{
using unit = std::chrono::nanoseconds;
auto totalDuration = 0;
for(int i = 0; i < iteration; i++){
std::string lstr = insertReplacingChars(longStr, insertChar, insertStart + i, insertInterval);
auto start = std::chrono::high_resolution_clock::now();
stringStreamReplace(lstr, "\r", "\\r");
auto finish = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<unit>(finish - start).count();
output += lstr;
std::cout << "stringStreamReplace() long " << duration << "\n";
totalDuration += duration;
}
std::cout << "stringStreamReplace() long string took total "
<< totalDuration
<< " nanoseconds\n";
totalDuration = 0;
for(int i = 0; i < iteration; i++){
std::string sstr = insertReplacingChars(smallStr, insertChar, insertStart + i, insertInterval);
auto start = std::chrono::high_resolution_clock::now();
stringStreamReplace(sstr, "\r", "\\r");
auto finish = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<unit>(finish - start).count();
output += sstr;
std::cout << "stringStreamReplace() small " << duration << "\n";
totalDuration += duration;
}
std::cout << "stringStreamReplace() small string took total "
<< totalDuration
<< " nanoseconds\n";
}
}