Re: [SPOILER] Solution to Perl 'Expert' Quiz-of-the-Week #22
Rod Adams <[email protected]>
| Newsgroups | gmane.comp.lang.perl.qotw.discuss |
|---|---|
| Message-ID | <[email protected]> |
Randy W. Sims wrote:
> On 8/29/2004 5:08 AM, Randy W. Sims wrote:
>
>> This is a common optimization and applies to all languages.
>> Generically, the goal is to perform each calculation only once.
>> Although it's not always as obvious as the code above. In particular
>> this implies reducing the number of temporaries which are created
>> behind the scenes, especially in languages like perl. This is why
>> complex one-line statements are often less efficent than the longer
>> equivelant: the shorter code creates multiple and expensive temporaries.
>
>
> Benchmark: timing 10 iterations of compound, simple...
> compound: 866 wallclock secs (726.63 usr + 0.94 sys = 727.57 CPU) @
> 0.01/s (n=10)
> simple: 4 wallclock secs ( 3.52 usr + 0.05 sys = 3.58 CPU) @
> 2.80/s (n=10)
> s/iter compound simple
> compound 72.8 -- -100%
> simple 0.358 20246% --
>
You must have been using the web2 list, not words. (names relative to
this list)
The interesting thing is that with the 'words' list, compound is faster
than simple:
D:\Code\QotW\22e>bench.pl words
Rate compound simple
compound 5.07/s -- -63%
simple 13.9/s 174% --
where as with 'web2', compound is slower:
D:\Code\QotW\22e>bench.pl web2
s/iter compound simple
compound 3.99 -- -45%
simple 2.18 83% --
For reference:
D:\Code\QotW\22e>wc -l words web2
25143 words
234936 web2
260079 total
My is is same as posted, I just added support for changing the dict on
the command line.
Running on perl v5.8.4 built for MSWin32-x86-multi-thread (ActiveState
build 810)
-- Rod Adams