Learning python

Part of learning a new programming language when you already know a couple is getting deep into the nitty gritty of it. With Python being the new BASIC I decided to start with something, well, basic.

I remember “back in the good old days” when there was a goofy standard of “web-safe colors”. Specifically there were 216 defined colors, so I decided to start by writing a program to take an image and covert it as if it was presentable in the late 1990s.

This exercise requires knowledge of storing a list of three pieces of information, performing mathematic computations to find a nearest color, and using libraries to load and save images.

PHP

source: web-safe-colors.php

PHP makes this easy with GD and faster with JIT built into 8.

Python

source: web-safe-colors.py

Python made this easy as well with PIL, the right Math libraries, and how easy tuples are to access.

Execution

I noticed a significant delay when running the Python version over the PHP version and decided to benchmark it over 10 executions:

for x in `seq 1 10`; do time php -f web-safe-colors.php src.jpg; done;
for x in `seq 1 10`; do time python web-safe-colors.py src.jpg; done; 

PHP: 1m8.973s, 1m9.619s, 1m8.960s, 1m8.981s, 1m9.235s, 1m8.967s, 1m9.447s, 1m8.888s, 1m8.985s, 1m9.472s. Average 1m9.1527
Python: 9m25.760s, 9m29.751s, 9m11.790s, 9m24.404s, 9m20.062s, 9m29.795s, 9m33.445s, 9m22.530s, 9m12.430s, 9m25.237s. Average 9m23.5204

Lesson

Learning python is the current norms “BASIC”, just like my middle schools’ Turtle Graphics, and it’s exactly what it needs to be: Easy to use, easy to maintain, easy to hire people. The performance, however, isn’t blowing my hair away.

Due to my googling and watching instructional videos a random short came my way which had an interesting comment that summarizes the performance point perfectly: “never be worried about performance in python” – because if you’re worried about performance, you shouldn’t be using python.

Leave a Reply