Greetings, Programs! Welcome to Flynn's Arcade! Here you will find my random musings on life and technology, as well as various interesting computer projects of mine. The name of my site comes from the 1982 sci-fi classic TRON. I am a huge fan of the movie and of classic video gaming and computing. As such, the Flynn's Arcade Radio stream plays various original and remixed music from those bygone days, as well as some of the best newer video game and chiptune music. I also host quite a few Armagetron Advanced servers to help carry on the spirit of classic gaming and serve the TRON community.
C10ud
Another Awesome way to Optimize Python
Written by C10ud
Tuesday, 27 October 2009
This semester I am building an 8086 microprocessor simulator as a project for one of my computer science classes. We were given the freedom to choose whatever language and other programming tools we wanted so I chose Python. By using Python I have been able to come up with some absolutely beautiful, dense code. For example, I have one method that is called after an arithmetic instruction to determine the status of the various flags. One of the flags that can be set is the parity flag. It is set if the lower 8 bits of the result contain an even number of one bits. This can be accomplished in one line of Python via a construct borrowed from functional programming known as a list comprehension:
[extract_bits(value, i, 1) for i in range(8)].count(1) % 2 == 0
While many features of Python such as this are almost as efficient as the equivalent code in C, others are not. Upon profiling my code, I discovered that a large amount of time was spent in a function I had written to simulate primative integer datatypes (so that things such as integer overflow could be properly simulated since Python uses arbitrary precision integers). The slowdown was unacceptable so I sought for alternative ways to speed things up while still writing the bulk of my code in Python. After many days of experimentation and searching, I found an excellent solution: Cython.
Cython allows you to keep almost all of your Python code (except for lambdas... grrr...) and optimize it using several techniques such as static typing, and then compile it to a C module. I was able to make most of my code use static types (such as C integers) where appropriate by adding Cython declarations. As a benchmark of performance, I used an object file that causes the simulator to run about 450,000 simple 8086 instructions. By the time I was done with my optimizations (and I'm sure there is still room for more), my simulator was able to finish in about 13 seconds on average. This is a tremendous improvement over the 34 seconds that it took the pure Python version, and my code didn't have to change very much at all. I highly recommend it if you are considering writing a Python C module and don't have a whole lot of time. It will not generate code that is as optimized as a human writing in C (at least not yet), but it sure saves a whole whackload of dev time.