Re: Perl Quiz of the Week #23
Xavier Noria <[email protected]>
| Newsgroups | gmane.comp.lang.perl.qotw.discuss |
|---|---|
| Message-ID | <[email protected]> |
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: % time perl balanced_gen.pl 12 | head (((((((((((()))))))))))) ((((((((((()())))))))))) ((((((((((())()))))))))) ((((((((((()))())))))))) ((((((((((())))()))))))) ((((((((((()))))())))))) ((((((((((())))))()))))) ((((((((((()))))))())))) ((((((((((())))))))()))) ((((((((((()))))))))())) perl balanced_gen.pl 12 0.08s user 0.03s system 91% cpu 0.120 total head 0.00s user 0.01s system 15% cpu 0.063 total Since the script prints as it finds solutions, that might be not very informative though. The timings reported by Rod Adams are amazing. My iterative version does not use memory (less than 1.5 MB for any N I've tried, opposed to the recursive version that swaps quickly), but I cannot see what else to improve by now. -- fxn