Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) 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):

# wc baseline output for comparison: ~$ wc perl_wc.pl pyth_wc.py large.txt 19 58 492 perl_wc.pl 25 96 673 pyth_wc.py 208048 1675832 11021496 large.txt 208092 1675986 11022661 total ~$ cat perl_wc.pl pyth_wc.py large.txt | wc 208092 1675986 11022661 # python version 2.0.1 ~$ time ./pyth_wc.py perl_wc.pl pyth_wc.py large.txt 19 58 492 perl_wc.pl 25 96 673 pyth_wc.py 208048 1675832 11021496 large.txt 208092 1675986 11022661 total real 0m31.360s user 0m31.030s sys 0m0.090s :~$ cat perl_wc.pl pyth_wc.py |./pyth_wc.py 44 154 1165 <stdin> # perl version 5.6.1 ~$ time ./perl_wc.pl perl_wc.pl pyth_wc.py large.txt 19 58 492 perl_wc.pl 25 96 673 pyth_wc.py 208048 1675832 11021496 large.txt 208092 1675986 11022661 total real 0m7.450s user 0m7.240s sys 0m0.090s ~$ cat perl_wc.pl pyth_wc.py |./perl_wc.pl 44 154 1165 -

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.

#!/usr/bin/python import sys files = map( lambda f: open(f), sys.argv[1:]) or [sys.stdin] Twords, Tlines, Tchars = 0, 0, 0 for file in files: words, lines, chars = 0, 0, 0 while 1: line = file.readline() if line: lines = lines + 1 list = line.split() words = words + len(list) chars = chars + len(line) else: print "%7d %7d %7d %s" % (lines, words, chars, file.name) break file.close() Twords = Twords + words Tlines = Tlines + lines Tchars = Tchars + chars if len(sys.argv) > 2: print "%7d %7d %7d total" % (Tlines, Twords, Tchars) #!/usr/bin/perl -w use strict; my $total = @ARGV > 1; my($Tlines, $Twords, $Tbytes,$lines, $words, $bytes); while(<>){ my @words = split; $words += @words; $bytes += length; $lines++; if (eof) { printf "%7d %7d %7d %s\n",$lines,$words,$bytes,$ARGV; $Tlines += $lines; $Twords += $words; $Tbytes += $bytes; ($lines,$bytes,$words) = (0,0,0); close ARGV; } } printf "%7d %7d %7d total\n",$Tlines,$Twords,$Tbytes if $total;

In reply to Re: Perl vs. Python: Looking at the Code by danger
in thread Perl vs. Python: Looking at the Code by mothra

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-23 16:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found