|Do you know where your variables are?|
Re: Perl vs. Python: Looking at the Codeby danger (Priest)
|on Apr 03, 2002 at 22:41 UTC||Need Help??|
Hey mothra, I guess I know why you haven't been to the local Perl monger meetings in a while ... :-)
Picking out individual bits of syntax as a basis for comparison is simply fraught with problems. All the more so when your comparison boils down to counting characters typed in each fragment. If you like Python better than Perl, that's fine, I think Python is a fine language too. If you think it is easier to learn, easier to maintain, and/or simply easier to think in, that's just fine too. But I cannot fathom choosing to base so much of your debate on a few tiny instances of being able to type a few less characters in one or the other.
However, if you are really interested in make that a comparison issue, please do so with something other than isolated fragments. Anyone can play that game --- no one wins, and what's more, no one learns.
So, though I haven't touched Python in quite some time, and I never had more than what I would call passing acquaintance with it (I implemented a few things to see how I liked it), I decided to do a simple, but non-trivial program in each for comparison. I chose to implement a wc like program: read from either STDIN, or from files passed in on the command line, print out the line-count, word-count, and byte-count of the input --- if multiple files on the command line, print out each separately, and a total at the end. The output should be similar to the output of wc. Each program should process the inputs one line at a time (no slurping in whole files, we don't know how big they'll be or how much memory the user has). Below is a base run of wc on my python version, my perl version, and a roughly 10Mb text file (I concatenated the jargon file a handful of times), followed by timing runs of the python and perl version with the same input (stdin tests not timed):
Before I post the code for both, I will state that I simply thought of a rough and ready algorithm first, then coded each one --- not trying to use tricks or shortcuts (though I did remove blank lines from each when finished). Well, that's not entirely true --- I also inlined the variable initializations in the python version, and I normally wouldn't do that in python code ... it just kind of freaks me out without parentheses :-) Also, to be fair, here's the byte count for each one with all whitespace stripped out entirely (I mean, extra indentation in the python version doesn't really equate to extra typing, auto-indent handles much of that): python stripped: 437; perl stripped: 362, difference = 75 characters. And I'll certainly grant that there are likely common idioms in python I am unaware of that would shrink that difference further --- the little map-lambda thing was just what sprang to mind for dealing with either STDIN or command line args, perhaps there's something more obviously magical like Perl's <> operator.
Frankly, I'm unconcerned about the difference in typing. Both versions were easy to code, and seem to me to be easy to read. But the difference in speed of basic I/O and text handling does seem significant to me (I know, if I was *really* concerned about speed I'd use C ... but I'm also concerned about ease of programming and development time, so if Perl and Python are on relative equal standing there, the 4-fold speed difference is definitely a factor ... well, that and CPAN of course). Of course, this is but a tiny fragment of the functionality in both languages as well ... so, make of it what you will.
With that in mind, please feel free to enlighten me on using better and/or more efficient python constructs. I'm always interested in learning something new.