Optimizing Perl (et al.) code by converting it to C
- I took a look at the discussion I originally conducted in use.perl.org
regarding Perl as a beginners language, and remembered it eventually
diverted into a related topic.
Apparently, some people there claimed that I will usually not gain
speed by converting my perl code to C, while I maintained that for some
programs, this is indeed the case:
For the record, Mark Jason Dominus, a very dominant figure in the Perl
world, wrote an article just about it to perl.com, where he made the claim
that converting the program to C will usually make little difference:
I agree that there are many Perlish programs whose equivalent C code will
not run substiantially faster. However, I do think that in some cases, it
Such a case, can be a code that contains a lot of loops or otherwise
intensive computations. My "Freecell Solver" program is such an example,
because I sometimes nest loops and conditionals to very large extents.
I encountered a similar case with Matlab. In Matlab, Matrix operations are
hard-coded in C and are therefore fast, but loops are slow. Those, who
write their programs using loops find they run incredibly slow. Matlab
fails when one has to write an iterative process in which every result is
dependant on the results of the previous iterations. In that case, I have
not yet found a way to avoid loops, but then again, I'm not entirely
proficient in Matlab.
I think any properly designed language that is destined to be interpreted
should make some things fast to perform and some things should be left to
hard-coded extensions. While some interpereted languages now aim to
incorporate C extensions that will make them suitable for a wider variety
of tasks, I still think C will always have an edge.
If anybody can re-write my Freecell Solver program in Perl (without
cheating by having C code all over the place) so it will be equally fast,
I will be in awe of him. However, I personally don't think it is possible.
I'm not going to write it in Perl (or in Matlab, for that matter) myself.
As the title implies this also concerns other languages such as Python,
Ruby, PHP, Pike, whatever. And compiling or JIT compilation is cheating.
Shlomi Fish shlomif@...
Home Page: http://t2.technion.ac.il/~shlomif/
Home E-mail: shlomif@...
A more experienced programmer does not make less bugs. He just realizes
what went wrong more quickly.