Python vs Bash Benchmark

Share Embed Donate


Short Description

Download Python vs Bash Benchmark...

Description

Python vs. BASH Benchmark Test One reason why Python should be included in Puppy. Mr. Maxwell 3/17/2009

This is a short in-depth report that includes 5 different benchmarks tests with each analyzed.

Python vs. BASH Benchmark Test March 17, 2009 Because Python is byte-compiled one would expect that Python would be faster than BASH which is interpreted. But how much faster is Python than BASH and is there any area where BASH is faster than Python? On very short scripts BASH is faster but only because Python takes about 0.120 seconds to load. This benchmark test sets out to figure out exactly how much faster Python is than BASH and what areas Python truly destroys BASH in speed. Much of the Python start up time can be reduced by loading Python during the booting of the computer and setting Python up so that instead of reloading it every time a Python script is run that Python script just uses the already loaded Python to interpret it. A child process would only be spawned if multiple Python scripts needed to run at the same time, in this case a small execution time increase would be acceptable. The hardware that this benchmark was performed on and the Python version:     

HP Pavilion N3410 laptop AMD-K6-2+ mobile processer at 550MHz 64MB of SDRAM at 100MHz Puppy Linux 4.1.2 Python 2.5.4

It is important to note that the writer strongly believes that Python is superior to BASH so his writing is more Python oriented than BASH oriented. However, every benchmark is fair and the writer gave no advantage to Python over BASH in recording the data or doing the tests. Another important note is that Python was not running in the background at the time of the benchmarks; in an unmodified Python state if Python is already running in the background very little of the interpreter is loaded into RAM because it shares most of its resources with the parent Python program. This is to demonstrate that Python will need heavy modifications to get it suitable for Puppy.

Page 2

Python vs. BASH Benchmark Test March 17, 2009 The first test is just simple addition, looping, and variable assignment. Python test 1 #! /usr/bin/Python var = 0 for i in xrange(10000): var += i BASH test 1 #! /bin/BASH VAR=0 for i in `seq 0 9999`; do let VAR=VAR+i done Python results:

Python

Real User Sys

First Pass Second Pass Third Pass Fourth Pass Fifth Pass Average 1.013 0.165 0.139 0.136 0.136 0.144 0.112 0.120 0.112 0.116 0.124 0.118 0.080 0.028 0.024 0.020 0.012 0.021

BASH results:

BASH

Real User Sys

First Pass Second Pass Third Pass Fourth Pass Fifth Pass Average 1.464 0.816 1.053 0.815 0.783 0.867 0.760 0.744 0.816 0.736 0.728 0.756 0.032 0.072 0.024 0.052 0.056 0.051

Conclusion: The Python program in this example is about 6 times faster than the BASH program. It’s important to take note that almost all the time spent in the Python program is just loading it into RAM, by creating an optimized Python shell that loads at runtime and does not need to fork processes this time can be removed.

Page 3

Python vs. BASH Benchmark Test March 17, 2009 The second test is to demonstrate text printing speed. Python test 2 #! /usr/bin/Python for i in xrange(10000): print i BASH test 2 #! /bin/BASH for i in `seq 0 9999`; do echo $i done Python results:

Python

Real User Sys

First Pass Second Pass Third Pass Fourth Pass Fifth Pass Average 0.830 0.825 0.795 0.720 0.745 0.771 0.216 0.236 0.272 0.208 0.276 0.248 0.116 0.112 0.096 0.120 0.108 0.109

BASH results:

BASH

Real User Sys

First Pass Second Pass Third Pass Fourth Pass Fifth Pass Average 2.154 2.264 2.368 2.555 2.347 2.384 0.828 0.860 0.812 0.884 0.964 0.880 0.184 0.220 0.232 0.276 0.260 0.247

Conclusion: Python is a little slower in this example as it is only 3 times faster than BASH; the Python print function is notoriously slow however. But, with a little dirty work in C a much faster print function could be made that does the same thing as the existing one.

Page 4

Python vs. BASH Benchmark Test March 17, 2009 The third test shows string indexing and slicing speed. Python test 3 #! /usr/bin/Python s = “Python is faster than BASH” for num in xrange(100): for i in xrange(len(s)+1): print s[0:i] BASH test 3 #! /bin/BASH s=”Python is faster than BASH” let l=${#s} for num in `seq 0 99`; do for i in `seq 0 $1`; do echo ${s:0:$i} done done Python results:

Python

Real User Sys

First Pass Second Pass Third Pass Fourth Pass Fifth Pass Average 1.068 0.350 0.537 0.375 0.384 0.412 0.120 0.120 0.112 0.112 0.132 0.119 0.076 0.060 0.064 0.072 0.048 0.061

BASH results:

BASH

Real User Sys

First Pass Second Pass Third Pass Fourth Pass Fifth Pass Average 2.334 2.112 2.514 2.354 2.071 2.263 0.932 0.928 0.904 0.960 0.908 0.925 0.756 0.740 0.732 0.760 0.772 0.751

Conclusion: Python is roughly 5.5 times faster than BASH in this test. Since string indexing and slicing are such important utilities in any text (or file) manipulation program it is crucial that whatever programming language is used that it is reasonably fast. Of course if you wanted blistering speed you would use C/C++ but that is unacceptable for system admin scripts (because you have to have scripts not binaries).

Page 5

Python vs. BASH Benchmark Test March 17, 2009 Test #4 is designed to demonstrate array indexing and the results are quite shocking, however, since Python has a sequential array data type (list) and BASH uses a hash table this is like comparing apples to oranges. But, because most programs only need a simple sequential list this is a good demonstration. A Python benchmark using the Python hash table data type (dictionary) is also included to make a more fair comparison. Python test 4, list #! /usr/bin/Python l = [] for i in xrange(10000): l.append(i) for i in xrange(10000): print l[i] Python test 4, dictionary #! /usr/bin/Python d = {} for i in xrange(10000): d[i] = i for i in xrange(10000): print d[i] BASH test 4 #! /bin/BASH for i in `seq 0 9999`; do l[$i]=$i done for i in `seq 0 9999`; do echo ${1[$i]} done Python list results:

Python

Real User Sys

First Pass Second Pass Third Pass Fourth Pass Fifth Pass Average 0.851 0.951 0.834 0.873 0.793 0.863 0.292 0.284 0.288 0.284 0.304 0.290 0.132 0.108 0.112 0.132 0.080 0.108

Page 6

Python vs. BASH Benchmark Test March 17, 2009

Python dictionary results:

Python

Real User Sys BASH results:

BASH

Real User Sys

First Pass Second Pass Third Pass Fourth Pass Fifth Pass Average 0.938 0.908 1.112 1.246 0.943 1.052 0.360 0.328 0.296 0.372 0.356 0.338 0.092 0.124 0.136 0.128 0.132 0.130

First Pass Second Pass Third Pass Fourth Pass Fifth Pass Average 25.030 24.948 24.588 25.039 25.167 24.936 19.957 19.881 19.517 20.077 19.885 19.840 0.762 0.808 0.956 0.764 0.916 0.861

Conclusion: With Python lists being 29 times faster and Python dictionaries being 23.7 times faster using BASH for any kind of sequential data manipulation is unacceptable (unless it is very small amounts of data). Since arrays are critical in many programs this very slow speed is severely limiting, Python gets all of its speed from an extremely optimizing byte compiler. Unfortunately BASH does not support arrays with strings as keys, it would have been interesting to see the speed comparison.

Page 7

Python vs. BASH Benchmark Test March 17, 2009 Test 5 is a function speed test with two functions and prints a total of 10000 times. Python test 5 #! /usr/bin/Python def f1(num): print num def func(num): for i in xrange(10): f1(i) for i in xrange(1000): func(i) BASH test 5 #! /bin/BASH function f1 { echo $1 } function func { for i in `seq 0 9`; do f1 $i done } for i in `seq 0 999`; do func $i done Python results:

Python

Real User Sys

First Pass Second Pass Third Pass Fourth Pass Fifth Pass Average 0.991 1.400 1.207 0.965 1.021 1.148 0.308 0.316 0.328 0.336 0.364 0.336 0.114 0.132 0.124 0.120 0.144 0.130

BASH results:

BASH

Real User Sys

First Pass Second Pass Third Pass Fourth Pass Fifth Pass Average 13.413 13.501 13.580 13.428 13.550 13.515 4.800 4.688 4.704 4.880 4.780 4.763 6.896 6.860 6.916 6.672 7.024 6.868

Conclusion: Since in this simple function calling program Python is 11.77 times faster than BASH, BASH is therefore restricted to linear program whereas Python is only 48% (verses the 476% of BASH) slower than the non

Page 8

Python vs. BASH Benchmark Test March 17, 2009 function calling program and for that reason is a very good structured programming language (keep in mind that 10000 function calls were made in both programs).

Page 9

Python vs. BASH Benchmark Test March 17, 2009 I hope you have enjoyed reading this benchmark report and now share my view that Python is much faster than BASH. I know that I enjoyed writing this and learned a great deal about how much faster Python is then BASH and with further optimizations Python can be even faster. Python’s ‘print’ is rather slow but with a C compiler and a little elbow grease a much faster version can be created. However, BASH does not support this kind of add-on very well making it very difficult to write extension modules in C. Whereas Python has extensive C extension module support including the ability to embed Python in C/C++ programs, a very useful feature. The big tests were data sets and functions; in the data sets test a Python sequential list was 29 times faster than a BASH array and a Python dictionary (hash-table) was 24 times faster. For anyone who does significant amounts of programming this is bad as data sets are crucial for almost all programs. Another killer was the speed of BASH functions, in this test Python was 12 times faster than BASH, no small amount. There are many reasons why structured programming is better than linear programming so I’m not going to get into that here, the added bonus of Pythons very well designed class system blows BASH out of the water in structured programming, completely limiting BASH to linear scripts. File executions were not included because Python uses BASH to launch programs (on Linux). Also, if a Python shell system was created it would be much faster because it would go directly to a C function.

Page 10

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF