Created
April 8, 2017 14:33
-
-
Save anonymous/18e372e8d0173e77b5c405920d4d3080 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Seeing if decode is faster. It is: | |
f1 0.062 | |
f2 0.1 | |
f3 0.049 | |
f4 0.05 | |
f5 0.067 | |
f6 0.034 | |
f7 0.018 | |
f8 0.006 | |
======the script, with new function f8 added=== | |
import time | |
def timing(f, n, a): | |
print f.__name__, | |
r = range(n) | |
t1 = time.clock() | |
for i in r: | |
f(a); f(a); f(a); f(a); f(a); f(a); f(a); f(a); f(a); f(a) | |
t2 = time.clock() | |
print round(t2-t1, 3) | |
def f1(list): | |
string = "" | |
for item in list: | |
string = string + chr(item) | |
return string | |
def f2(list): | |
return reduce(lambda string, item: string + chr(item), list, "") | |
def f3(list): | |
string = "" | |
for character in map(chr, list): | |
string = string + character | |
return string | |
def f4(list): | |
string = "" | |
lchr = chr | |
for item in list: | |
string = string + lchr(item) | |
return string | |
def f5(list): | |
string = "" | |
for i in range(0, 256, 16): # 0, 16, 32, 48, 64, ... | |
s = "" | |
for character in map(chr, list[i:i+16]): | |
s = s + character | |
string = string + s | |
return string | |
import string | |
def f6(list): | |
return string.joinfields(map(chr, list), "") | |
import array | |
def f7(list): | |
return array.array('B', list).tostring() | |
def f8(list): | |
return bytearray(list).decode('latin-1') | |
testdata = range(256) | |
print `testdata` | |
testfuncs = f1, f2, f3, f4, f5, f6, f7, f8 | |
for f in testfuncs: print f.func_name, f(testdata) | |
for f in testfuncs: timing(f, 100, testdata) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment