Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(269)

Side by Side Diff: benchmarks/write-utf8-benchmark.cc

Issue 9689065: Benchmarkify pointer swapping string encoder
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | benchmarks/write-utf8-benchmark.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 // This links against libv8. If you don't have that handy, but you have Python and Scons,
29 // the following commands will link against a directory with the V8 source.
30
31 // #!/bin/sh
32 // rm -f obj/release/*
33 // /path/to/v8/tools/test.py --mode=release --arch=x64 -j18 --build-only
34 // rm obj/release/d8*.o
35 // rm obj/release/preparser-api.o
36 // g++ -I /home/erikcorry/OpenV82/v8/include -o writeutf8 write-utf8-benchmark.c c obj/release/*.o obj/release/x64/*.o obj/release/extensions/externalize-string- extension.o obj/release/extensions/gc-extension.o -lpthread
37
38
39 #include <assert.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 #include "v8.h"
44
45 #ifdef _WIN32
46 #include <Windows.h>
47 #else
48 #include <sys/time.h>
49 #endif
50
51 using namespace v8;
52
53 const char* ascii[] = {
54 "a", // 1
55 "aa", // 2
56 "aaaa", // 4
57 "aaaaaaaa", // 8
58 "aaaaaaaaaaaaaaaa", // 16
59 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", // 32
60 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", // 64
61 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
62 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", // 128
63 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
64 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
65 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
66 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" // 256
67 };
68
69 const char* utf[] = {
70 "\304\233", // 1
71 "\304\233\304\233", // 2
72 "\304\233\304\233\304\233\304\233", // 4
73 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233", // 8
74 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
75 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233", // 16
76 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
77 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
78 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
79 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233", // 32
80 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
81 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
82 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
83 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
84 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
85 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
86 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
87 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233", // 64
88 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
89 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
90 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
91 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
92 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
93 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
94 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
95 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
96 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
97 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
98 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
99 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
100 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
101 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
102 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
103 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233", // 128
104 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
105 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
106 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
107 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
108 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
109 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
110 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
111 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
112 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
113 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
114 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
115 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
116 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
117 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
118 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
119 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
120 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
121 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
122 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
123 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
124 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
125 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
126 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
127 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
128 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
129 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
130 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
131 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
132 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
133 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
134 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
135 "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233" // 256
136 };
137
138
139 #ifdef _WIN32
140 typedef LARGE_INTEGER timestamp;
141 static void GetTime(timestamp* time) {
142 QueryPerformanceCounter(time);
143 }
144 static void PrintTime(timestamp* start, const char* name, bool just_enough) {
145 timestamp end;
146 static LARGE_INTEGER freq = {0};
147 QueryPerformanceCounter(&end);
148 if (freq.QuadPart == 0) {
149 QueryPerformanceFrequency(&freq);
150 }
151 printf("%s (%s): %ldms\n", name, just_enough ? "just enough" : "plenty", (end. QuadPart - start->QuadPart) / (freq.QuadPart / 1000));
152 }
153
154 #else //UNIX
155 typedef struct timeval timestamp;
156 static void GetTime(timestamp* time) {
157 gettimeofday(time, NULL);
158 }
159 static void PrintTime(timestamp* start, const char* name, bool just_enough) {
160 timestamp end;
161 gettimeofday(&end, NULL);
162 printf("%s (%s): %ldms\n", name, just_enough ? "just enough" : "plenty", (end. tv_sec - start->tv_sec) * 1000 + (end.tv_usec - start->tv_usec) / 1000);
163 }
164 #endif
165
166
167 static char output[1000000];
168
169 static void Test(Local<String>* one, Local<String>* two, Local<String> prepend, const char* name, bool just_enough) {
170 timestamp start;
171 GetTime(&start);
172 for (int k = 0; k < 1000; k++) {
173 HandleScope handle_scope;
174 for (int i = 0; i < 8; i++) {
175 for (int j = 0; j < 8; j++) {
176 Local<String> str = String::Concat(one[i], two[j]);
177 str = String::Concat(str, one[j]);
178 str = String::Concat(prepend, str);
179 int len;
180 str->WriteUtf8(output, just_enough ? str->Utf8Length() + 1 : -1, &len);
181 }
182 }
183 }
184 PrintTime(&start, name, just_enough);
185 }
186
187
188 static void Unbalanced(Local<String> one, Local<String> two, bool left, Local<St ring> prepend, const char* name, bool just_enough, int flag = 0) {
189 timestamp start;
190 GetTime(&start);
191 for (int k = 0; k < 100; k++) {
192 HandleScope handle_scope;
193 Local<String> str = String::Concat(one, two);
194 for (int i = 0; i < 2000; i++) {
195 if (left) {
196 str = String::Concat(one, str);
197 } else {
198 str = String::Concat(str, two);
199 }
200 }
201 int len;
202 str = String::Concat(prepend, str);
203 str->WriteUtf8(output, just_enough ? str->Utf8Length() + 1 : -1, &len, flag) ;
204 }
205 PrintTime(&start, name, just_enough);
206 }
207
208
209 int main(int argc, char* argv[]) {
210 V8::SetFlagsFromCommandLine(&argc, argv, false);
211
212 HandleScope handle_scope;
213
214 Local<String> ascii_strings[8];
215 Local<String> unicode_strings[8];
216 for (int i = 0; i < 8; i++) {
217 ascii_strings[i] = v8::String::New(ascii[i]);
218 unicode_strings[i] = v8::String::New(utf[i]);
219 }
220
221
222 Persistent<Context> context = Context::New();
223 Context::Scope context_scope(context);
224
225
226 for (int i = 0; i < 2; i++) {
227 Test(&ascii_strings[0], &ascii_strings[0], ascii_strings[0], "Short Ascii", i != 0);
228 Test(&unicode_strings[0], &unicode_strings[0], ascii_strings[0], "Short Unic ode", i != 0);
229 Test(&ascii_strings[0], &ascii_strings[0], unicode_strings[0], "Prepend Unic ode", i != 0);
230
231 Unbalanced(ascii_strings[0], ascii_strings[0], true, ascii_strings[0], "Unba lanced left", i != 0);
232 Unbalanced(ascii_strings[0], ascii_strings[0], false, ascii_strings[0], "Unb alanced right", i != 0);
233 Unbalanced(unicode_strings[0], unicode_strings[0], true, unicode_strings[0], "Unbalanced Unicode left", i != 0);
234 Unbalanced(unicode_strings[0], unicode_strings[0], false, unicode_strings[0] , "Unbalanced Unicode right", i != 0);
235
236 Unbalanced(ascii_strings[7], ascii_strings[7], true, ascii_strings[7], "Unba lanced long left", i != 0);
237 Unbalanced(ascii_strings[7], ascii_strings[7], false, ascii_strings[7], "Unb alanced long right", i != 0);
238 Unbalanced(unicode_strings[7], unicode_strings[7], true, unicode_strings[7], "Unbalanced Unicode long left", i != 0);
239 Unbalanced(unicode_strings[7], unicode_strings[7], false, unicode_strings[7] , "Unbalanced Unicode long right", i != 0);
240
241 Unbalanced(ascii_strings[7], ascii_strings[7], true, unicode_strings[7], "Pr epend Unbalanced long left", i != 0);
242 Unbalanced(ascii_strings[7], ascii_strings[7], false, unicode_strings[7], "P repend Unbalanced long right", i != 0);
243
244 Unbalanced(unicode_strings[7], unicode_strings[7], true, unicode_strings[7], "Unbalanced Unicode long left hint-many", i != 0, 1);
245 Unbalanced(unicode_strings[7], unicode_strings[7], false, unicode_strings[7] , "Unbalanced Unicode long right hint-many", i != 0, 1);
246 }
247
248 context.Dispose();
249 return 0;
250 }
OLDNEW
« no previous file with comments | « no previous file | benchmarks/write-utf8-benchmark.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698