[erlang-questions] foldl vs foldr and ++

Kelly Wong <>
Thu Jan 16 00:51:17 CET 2014


Did you make sure to run it with -mode(compile). ?  If you don't the script
will be interpreted, and could throw off performance tests.

Kelly Wong


On Tue, Jan 14, 2014 at 3:41 AM, Tim Watson <>wrote:

> I tweaked your module to run via escript (just adding a simple main/1 and
> printing out the results of timer:tc), and I see a bit gap between foldl
> and foldr, whilst the hand coded version based on lists:reverse/1 is plenty
> fast enough:
>
> :test $ escript lr.beam 10000000
> foldl: 679.103ms
> foldr: 43609.149ms
> reverse: 909.946ms
> :test $ evm info
> R16B03 compiled for x86_64-apple-darwin11.4.2, 64bit
>
> On Friday, 11 May 2012 10:27:29 UTC+1, Ulf Wiger wrote:
>>
>>
>> On 11 May 2012, at 04:02, Richard O'Keefe wrote:
>>
>> > This can be written more efficiently, but it's best of all
>> > just not to reverse the string in the first place.  Keep
>> > the code at a level where you can see what's going on well
>> > enough to spot the really _big_ efficiency gains.
>>
>> And this is of course the really important difference between
>> foldr() and foldl():
>>
>> - in which order do you want to process the list?
>> - if you build a list in the accumulator, in which order do you want it?
>>
>> For most practical purposes, one should assume that they
>> both both do their job efficiently.
>>
>> BR,
>> Ulf
>>
>> Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.
>> http://feuerlabs.com
>>
>>
>>
>> _______________________________________________
>> erlang-questions mailing list
>> 
>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20140115/8bf83195/attachment.html>


More information about the erlang-questions mailing list