What I learned from Python that makes me a better programmer when I use C

Here is my response to this call for posts on the raganwald blog. A response from Ryan Ginstrom can be found here

Readability counts

I think I realised this was important before I started learning Python, but I don’t think realised just how important it really is. I should probably start withe disclaimer that I am not a trained programmer. I started off learning Basic on my BBC micro back in the ‘80’s, learned a spot of Perl in my old job, and learned C to help with my PhD and my work as a researcher. None of these languages emphasise readability. That is not to say that you can’t write readable code in Perl or Basic or C, it’s just that the concept of readability doesn’t seem to be built into the language in the same way that it is with Python. In the about section on the official Python website, “very clear, readable syntax” is at the top of the list of key distinguishing features, and the need for clear, readable code is the most important lesson I have learned from writing Python.

Better algorithms

When writing in C, if I wanted a section of code to run more quickly, I used to optimise the code by bit twiddling. The code would get lower-and-lower level, and less-and-less readable until I achieved what I wanted. In Python, in some cases this might not be possible, and it is certainly not a ‘Pythonic’ thing to do. Firstly, in Python many of the built in operations: functional programming functions, list processing, dictionary storage and access, are already highly optimised. Secondly, quite often efficiency problems are caused the overall logic of an algorithm, rather than the implementation of the algorithm in code.

In C, it is possible to disguise a slow or flawed algorithm because the implementation runs fast enough for it not to be noticeable. However, I find that in Python, if I have a section of code that is not running fast enough, it forces me to look at the underlying algorithm more closely. By this, I mean that it forces me to look at the logical flow of the program, and see if it can be condensed/re-organised/changed to make it more efficient and/or elegant. Quite often this results in code that is faster and more readable. I think this approach of algorithmic optimisation rather than code optimisation is applicable to many languages. Maybe they teach this on Programming courses, but it’s not something I was overtly aware of until I met Python.

Related Entries

4 Comments

Nice ones. I've also noticed that the best speedups I get have been by improving the algorithm/reducing the search space, rather than micro-optimizations. Python makes it easy to experiment with various algorithms, and the built-in profiling tools let you get concrete measurements.

Ohhh man, you really need to check out Scheme too. Just last night it helped me out. Recursive solutions are tons of fun :D

Also, you'd be interested to know that the University of Toronto is teaching Python to 1st year students instead of Java!

Learning Lisp/Scheme has been on my to-do list forever. I'm sure it is the ultimate language to improve one's programming in general. I'd love to see a 'What I learned from C++ that made me better Lisp programmer' blog post ;-)

I like your homegrown approach. Optimizing your algorithms first is always the way to go. If you haven't already, I recommend you look up Big-O notation and learn a little about it. Some of the stuff you find will likely be confusing, but keep looking and you can find some explanations. I find that trying to come up with efficient algorithms is a great joy myself. You might pick up a book on algorithms (just search amazon). There are some very clever ones out there, and the more problems you know how to solve, the more programs you'll invent.

Leave a comment

About

I work at Birmingham Conservatoire as senior researcher and software development manager for the Integra Project. I live with my wife and three beautiful children in Birmingham, UK.» More...

Tag Cloud

Projects

-->
Close