7 ms·
A related question is: why is Perl so fast? Quite a few years ago I wrote a little Runge-Kutta solver in Perl for some simulation work. It seemed like a good i
by tjradcliffe 12y ago
A related question is: why is Perl so fast?
Quite a few years ago I wrote a little Runge-Kutta solver in Perl for some simulation work. It seemed like a good idea at the time. The equations of motion had to be integrated over a very long time, and it could take hours for a single run (still much faster than the Monte Carlo it was being used to do a sanity-check on). I re-wrote everything in C++, and picked up less than a factor of two in speed.
So it isn't that "Python is interpreted" that is the problem, because "Perl is interpreted" in exactly the same way. It really does seem to come down to the Python object model. Perl's scalar types have vastly less overhead, so much so that you can actually do reasonably efficient numerical computation in it.
I abandoned Perl for Python shortly thereafter because once I got over the "oh my god it's full of whitespace" thing Python was just more fun to code in, but the speed that Perl provided is something I've definitely missed, and it was a real awakening to the notion that interpreted languages don't have to be slow. The striking thing was that unlike Java (say) where it can be fast but you generally have to think about it, I was getting fast Perl without even really trying.
- tenfingers 12y agoAlso had the same experiences. Perl is relatively fast and memory compact compared to other modern scripting languages. Perl also has PDL (http://pdl.perl.org/ http://pdl.perl.org/), which is very similar in scope to numpy, but predates it by several years.
- dagw 12y agopredates it by several years If you want to get technical, Numpy is a continuation of Numeric which in turn pre-dates pdl.
- haberman 12y agoI'm not sure your results are typical. In microbenchmarks, Perl is 2-125x slower than C++: http://benchmarksgame.alioth.debian.org/u32/benchmark.php?test=all&lang=perl&lang2=gpp&data=u32 http://benchmarksgame.alioth.debian.org/u32/benchmark.php?te... And Java is quite a bit faster than Perl too: http://benchmarksgame.alioth.debian.org/u32/benchmark.php?test=all&lang=perl&lang2=java&data=u32 http://benchmarksgame.alioth.debian.org/u32/benchmark.php?te... Perl isn't really that fast. It's faster than Python in most cases, but gets beat by Lua pretty consistently: http://benchmarksgame.alioth.debian.org/u32/benchmark.php?test=all&lang=perl&lang2=lua&data=u32 http://benchmarksgame.alioth.debian.org/u32/benchmark.php?te...
- hyperbovine 12y agoI'm not sure microbenchmarks are typical.
- haberman 12y agoI think they are a pretty good representation of the performance of doing something directly in the language (as opposed to just calling into lower-level libraries written in a different language).
- cleaver 12y agoFor a general-purpose problem, Perl won't be particularly fast. For a Perl-type problem (scanning and parsing big files), Perl is very fast. Doing a Perl-type problem in a general-purpose language would be considerably slower. However, Python or others will perform much better in the "can I read my own code six months later" benchmark.
- csirac2 12y agoI don't know why I always have to chip in on "perl is unreadable lol" comments, but over the last 8 years apart from a steady trickle of C coding here and there the bulk of my dayjob has moved around from C/Verilog, then I discovered Ruby, then Perl/R/Python, to full-time Python now. It's true that unsupervised, weak coders using perl turn out worse code than in other languages. But it really doesn't take much to produce good code if you have a tiny bit of supervision/discipline (which stems mostly from 80% of perl tutorials on the web are teaching 1997-era perl anti-patterns). And/or if you happen to be a stronger programmer in something else, hopefully you stumble across perlcritic and modern perl patterns more quickly. Well-written Moose code involves less boilerplate, the declarative nature and composability of classes, types and data members with free type/value validation is a delight to maintain, and results in more robust code with a lot fewer silent failures (or objects happily chugging along silently with invalid state) than typical Python classes. Of course, now that I can't use Moose and the Python community actively seems to discourage the very thought of relying on any superset of core Python OO features like enthoughts' traits package - I really want to revisit static/stronger typed programming languages for large projects. So it feels like I've come full-circle in my programming career...
- Igglyboo 12y agoIf you're doing numerical computation Numpy will be extremely fast. Most of it is a thin layer over C code so you get the best of both worlds(unless you're the Numpy maintainers).
- debacle 12y agoBecause it was written to do (certain) things very fast.
- vacri 12y ago"oh my god it's full of whitespace" When I write in python, this always triggers my mental slogan "python, the language which makes 'nothing' important". This is said with a trace of irony, as my most fluent language is bash scripting :)
- coldtea 12y ago>related question is: why is Perl so fast? Easy answer: it's not. On most benchmarks it's on par with Python, PHP, Ruby, etc, some are faster on one, others on some other test. >once I got over the "oh my god it's full of whitespace" thing What full of whitespace? Python has as much (or as little) whitespace as normally intended Perl, Ruby, C, etc. What it has that they don't have is significant whitespace.
- jules 12y agoIf you only got a 2x speedup there is something wrong. A more usual number for Perl is about 100x slower than C++ for numerical code. Perl isn't very different than Python in this regard. e.g. on the shootout on the n-body benchmark, Perl is about as fast as Python, and 125x slower than C++.
- baudehlo 12y agoIt's sad that this question hasn't actually even been answered yet. Some things perl does under the hood to be fast are that integers are (mostly) actually integers under the hood. Arrays are actually arrays under the hood (a fact that makes perl's DBI very fast). And more importantly it has had decades of people trying to make it faster without changing the (sometimes crazy) semantics of the language. But of course languages with JITs a have massively overtaken it in the performance stakes (PyPy, LuaJIT, and JavaScript). Mostly I think this is a result of lack of funding and huge company spending. Just look at what Facebook have managed to do with PHP.
- Htsthbjig 12y ago"why is Perl so fast?" Is not. We tested our own code in Perl for doing some text processing in our company. The result was 89 times slower than our c code, which is not so slow compared with other options, but is slow. We use a lot of c++ and python, but always for managing encapsulated low level code. With anything that has to compute intensely, like audio, video or 3d work, the differences are way over 3000 times slower. That is: the computer working for a second or for an hour. Java is not fast, it has never been. It is not its "forte". You could decide to use java or any high level languages based on its merits, but being fast is not one of them. It is a good idea to learn assembly and disassembly with the debugger before using high level languages. It gives you the knowledge of what computers are really doing under the hood.
- stinos 12y agoI re-wrote everything in C++, and picked up less than a factor of two in speed. Don't think it has been mentioned in the other comments, but this could also be a performance problem with your C++ code - possibly in combination with doing something Perl is good at