Optimisation

Andy Armstrong andy at hexten.net
Mon Mar 2 22:40:30 GMT 2009


On 2 Mar 2009, at 22:02, Nigel Peck wrote:

> Andy Armstrong wrote:
>> $ perl bm-foolish-idea.pl
>>                       Rate    empty_foreach empty_if_foreach
>> empty_foreach     5242880/s               --             -74%
>> empty_if_foreach 20309304/s             287%               --
>>                          Rate non_empty_if_foreach     
>> non_empty_foreach
>> non_empty_if_foreach 1203020/s                   --                  
>> -17%
>> non_empty_foreach    1456355/s                   
>> 21%                   --
>
> Thanks for this, I've not done any benchmarking before. So if I read  
> this right, the "if" version is a lot faster for empty lists, but  
> slows it down a bit for non-empty lists?

Yeah, that's right. But bear in mind - per Simon's test - that the  
difference is quickly swamped by the overhead of whatever else you're  
doing. In all but extremely exceptional cases it's not worth worrying  
about.

> Sorry for the noob questions, but if these are being compared  
> against each other, why are the percentages different?

Cos that's how percentages work :)

100 + 287% = 100 * 3.87 = 387
74% of 387 = 387 * 0.74 = 287 (not quite - blame rounding)
387 - 287 = 100

> Feel free to tell me to go and RTFM! :)


Well, you should do that too :)

-- 
Andy Armstrong, Hexten



More information about the london.pm mailing list