Re: Perl Quiz of the Week #23
Matthew Walton <[email protected]>
| Newsgroups | gmane.comp.lang.perl.qotw.discuss |
|---|---|
| Message-ID | <[email protected]> |
Xavier Noria wrote: > On Sep 3, 2004, at 14:24, colin.rafferty-/PgpppG8B+R7qynMiXIxWgC/[email protected] wrote: > >>> % time perl balanced_gen.pl 12 >>> perl balanced_gen.pl 12 4.38s user 0.03s system 83% cpu 5.255 total >> >> >> You are looking at the wrong number. In the one you're showing, I'm >> doing `head', so it's cut off after 10 results. You really should be >> looking at the results of the full run to see that we're in the same >> ballpark. > > > I knew head(1) just showed the first 10 lines by default, but I assumed > the rest were ignored but printed no matter what by the script, since > there's nothing about the pipeline in the code. I was timing the > execution without printing, so I thought the timing would be more or > less equivalent to yours in consequence. > > Now I see the assumption was wrong, although I don't understand why from > the man page. Looks like after those 10 lines the script itself is > halted somehow. Adding printing: I think you'll find that actually the output is still generated by the script, but it doesn't go anywhere. Never underestimate the speed impact of printing things. I'm working on a solution to this in Haskell at the moment, and on 10 pairs it takes 3 seconds with printing and 0.3 seconds without, yet it's calculating the same result set (in the latter case, it's telling me how long it is instead of what the results actually are). This is on a 1GHz G4 Powerbook on OS X, so it's entirely possible that the transparent terminal isn't helping printing speed. Of course, I still need to write a solution in Perl, and I've not even looked at the Haskell one's memory use (which is probably frighteningly high - the penalty of lazy evaluation).