4 Feb 2004
(updated 4 Feb 2004 at 20:09 UTC) »
Efficiency of Python vs. C++
Obviously I am not going to give the final answer to such a question like which one is faster. It depends on what you are programming and their requisites, and if you need maximun speed you may need to use C++, C or even assembler. But I want to share with you my little experiment.
I wrote a small C++ library for handling tables of data of diverse types. I did it aiming to clarity and usability, and so I used many inheritance and virtual methods. I recognize efficiency was not my priority.
Then I needed to convert ascii files of tabbed data with different formats to mine. I needed to write too much code in C++ (and I wanted to learn python I must confess) so I wrote a python script to do that (with a Gtk interface thanks to lgs).
Fascinated by the simplicity of Python code (as I told you on 29th) I decided to wrote in Python a feature selection algorithm I had previously wrote in C++. Today I have decided to test their speed and these are the results:
- Python: 29 minutes
- C++: 33 minutes
Well, it is not a very scientific experiment, and I cheated C++ because it is loading the table 32 times (it takes just a few seconds). But, hey! I expected C++ to win by large.
Good (coding is simpler now) and bad news (it took long time to wrote C++ lib) for me. But definitely a BIG surprise!