[erlang-questions] foldl vs foldr and ++

Tim Watson <>
Tue Jan 14 12:41:29 CET 2014


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 
>  <javascript:> 
> http://erlang.org/mailman/listinfo/erlang-questions 
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20140114/d6bcd933/attachment.html>


More information about the erlang-questions mailing list