Testing string conversion performance in python
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
Wed Jul 30 02:58:53 2014 /tmp/python.string.stats | |
2100004 function calls in 1.436 seconds | |
Ordered by: internal time | |
ncalls tottime percall cumtime percall filename:lineno(function) | |
300000 0.500 0.000 0.529 0.000 random.py:173(randrange) | |
1 0.278 0.278 1.436 1.436 foo.py:24(main) | |
300000 0.154 0.000 0.683 0.000 random.py:237(randint) | |
400000 0.103 0.000 0.103 0.000 {_struct.pack} | |
100000 0.094 0.000 0.156 0.000 foo.py:21(four) | |
100000 0.088 0.000 0.142 0.000 foo.py:15(two) | |
100000 0.077 0.000 0.077 0.000 foo.py:12(one) | |
300000 0.054 0.000 0.054 0.000 {chr} | |
100000 0.045 0.000 0.085 0.000 foo.py:18(three) | |
300000 0.029 0.000 0.029 0.000 {method 'random' of '_random.Random' objects} | |
100000 0.014 0.000 0.014 0.000 foo.py:8(zero) | |
1 0.002 0.002 0.002 0.002 {range} | |
1 0.000 0.000 1.436 1.436 <string>:1(<module>) | |
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} | |
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
#!/usr/bin/python | |
import struct | |
import random | |
import cProfile | |
import pstats | |
def zero(a,b,c): | |
# This is just to check the call overhead | |
return '' | |
def one(a,b,c): | |
return '%c%c%c' % (a,b,c) | |
def two(a,b,c): | |
return chr(a) + chr(b) + chr(c) | |
def three(a,b,c): | |
return struct.pack("BBB", a,b,c) | |
def four(a,b,c): | |
return struct.pack("B", a) + struct.pack('B',b) + struct.pack("B",c) | |
def main(): | |
for x in range(100000): | |
a=random.randint(0,255) | |
b=random.randint(0,255) | |
c=random.randint(0,255) | |
zero(a,b,c) | |
one(a,b,c) | |
two(a,b,c) | |
three(a,b,c) | |
four(a,b,c) | |
tmpfile = "/tmp/python.string.stats" | |
cProfile.run('main()', tmpfile) | |
p = pstats.Stats(tmpfile) | |
p.strip_dirs().sort_stats('time').print_stats() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment