Skip to content

Instantly share code, notes, and snippets.

@adrian17 adrian17/ub14.txt
Last active Nov 10, 2016

Embed
What would you like to do?
import timeit
import time
import subprocess
lines = [
"a = ''",
"a = ' '",
"a = ' '",
"a = 'a'",
"a = 'a b a b '*100",
"a = 'a b a b '*10000",
"a = 'a1 b2 a3 b4 '*100",
"a = 'a1 b2 a3 b4 '*10000",
"a = 'value value'",
"a = 'value value '*100",
"a = 'value value '*10000",
"a = 'valuevalue valuevalue '",
"a = 'valuevalue valuevalue '*100",
"a = 'valuevalue valuevalue '*10000",
"a = 'value'*100 + ' 1 2 3'",
"a = 'value'*1000 + ' 1 2 3'",
"a = 'value'*10000 + ' 1 2 3'",
"a = 'value'*100000 + ' 1 2 3'",
"a = (('x'*100)+' ')*1000",
"a = (('x'*1000)+' ')*100",
"a = (('x'*10000)+' ')*10",
]
for line in lines:
outputs = {}
times = {}
for name, python in [("new", "./python"), ("old", "./python2")]:
out = subprocess.check_output([python, "-c", line + "; print(a.split(' '))"])
outputs[name] = out
out = subprocess.check_output([python, "-m", "timeit", "-s", line, "a.split(' ')"])
time = out.decode().split(":")[1].strip()
times[name] = time
vals = {"nsec": 1, "usec": 1000, "msec": 1000*1000}
get = lambda ts: float(ts[0]) * vals[ts[1]]
oldtime = get(times["old"].split())
newtime = get(times["new"].split())
mult = oldtime / newtime
if mult < 1:
mult = -(newtime / oldtime)
assert outputs["new"] == outputs["old"]
print '{:<35} | {:<20} | {:<20} | {:.3}x'.format(line, times["old"], times["new"], mult)
#if STRINGLIB_SIZEOF_CHAR == 1
const STRINGLIB_CHAR* loc = str;
while (maxcount-- > 0) {
loc = memchr(loc, ch, str_len-j);
if (loc == NULL)
break;
j = loc - str;
SPLIT_ADD(str, i, j);
i = j = j + 1;
loc++;
}
#else
while ((j < str_len) && (maxcount-- > 0)) {
for(; j < str_len; j++) {
/* I found that using memchr makes no difference */
if (str[j] == ch) {
SPLIT_ADD(str, i, j);
i = j = j + 1;
break;
}
}
}
#endif
a = '' | 149 nsec per loop | 150 nsec per loop | -1.01x
a = ' ' | 177 nsec per loop | 180 nsec per loop | -1.02x
a = ' ' | 322 nsec per loop | 355 nsec per loop | -1.1x
a = 'a' | 160 nsec per loop | 163 nsec per loop | -1.02x
a = 'a b a b '*100 | 4.3 usec per loop | 5.3 usec per loop | -1.23x
a = 'a b a b '*10000 | 359 usec per loop | 446 usec per loop | -1.24x
a = 'a1 b2 a3 b4 '*100 | 9.76 usec per loop | 10.6 usec per loop | -1.09x
a = 'a1 b2 a3 b4 '*10000 | 1.41 msec per loop | 1.47 msec per loop | -1.04x
a = 'value value' | 206 nsec per loop | 209 nsec per loop | -1.01x
a = 'value value '*100 | 5.42 usec per loop | 5.57 usec per loop | -1.03x
a = 'value value '*10000 | 688 usec per loop | 722 usec per loop | -1.05x
a = 'valuevalue valuevalue ' | 221 nsec per loop | 220 nsec per loop | 1.0x
a = 'valuevalue valuevalue '*100 | 5.7 usec per loop | 5.8 usec per loop | -1.02x
a = 'valuevalue valuevalue '*10000 | 760 usec per loop | 769 usec per loop | -1.01x
a = 'value'*100 + ' 1 2 3' | 410 nsec per loop | 272 nsec per loop | 1.51x
a = 'value'*1000 + ' 1 2 3' | 1.84 usec per loop | 460 nsec per loop | 4.0x
a = 'value'*10000 + ' 1 2 3' | 16.9 usec per loop | 3.2 usec per loop | 5.28x
a = 'value'*100000 + ' 1 2 3' | 179 usec per loop | 45.7 usec per loop | 3.92x
a = (('x'*100)+' ')*1000 | 73.5 usec per loop | 43.1 usec per loop | 1.71x
a = (('x'*1000)+' ')*100 | 40.8 usec per loop | 12 usec per loop | 3.4x
a = (('x'*10000)+' ')*10 | 34.7 usec per loop | 7.53 usec per loop | 4.61x
a.split(' ') | old | new | improvement
a = '' | 175 nsec per loop | 178 nsec per loop | 0.983x
a = ' ' | 211 nsec per loop | 222 nsec per loop | 0.95x
a = ' ' | 325 nsec per loop | 356 nsec per loop | 0.913x
a = 'a' | 204 nsec per loop | 196 nsec per loop | 1.04x
a = 'value value' | 253 nsec per loop | 260 nsec per loop | 0.973x
a = 'value value ' | 267 nsec per loop | 274 nsec per loop | 0.974x
a = 'value value '*100 | 7.86 usec per loop | 7.82 usec per loop | 1.01x
a = 'value value '*10000 | 963 usec per loop | 960 usec per loop | 1.0x
a = 'valuevalue valuevalue ' | 273 nsec per loop | 277 nsec per loop | 0.986x
a = 'valuevalue valuevalue '*100 | 8.43 usec per loop | 7.91 usec per loop | 1.07x
a = 'valuevalue valuevalue '*10000 | 1.04 msec per loop | 1.01 msec per loop | 1.03x
a = 'value'*100 + ' 1 2 3' | 634 nsec per loop | 329 nsec per loop | 1.93x
a = 'value'*1000 + ' 1 2 3' | 3.65 usec per loop | 544 nsec per loop | 6.71x
a = 'value'*10000 + ' 1 2 3' | 35.1 usec per loop | 3.83 usec per loop | 9.16x
a = 'value'*100000 + ' 1 2 3' | 360 usec per loop | 54.4 usec per loop | 6.62x
a = (('x'*100)+' ')*1000 | 115 usec per loop | 51.7 usec per loop | 2.22x
a = (('x'*1000)+' ')*100 | 77.9 usec per loop | 15.4 usec per loop | 5.06x
a = (('x'*10000)+' ')*10 | 69.8 usec per loop | 8.51 usec per loop | 8.2x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.