[erlang-questions] Generators?

jla415 jla415@REDACTED
Tue Jul 31 05:58:31 CEST 2007




Bryan Burgers wrote:
> 
> I think the point tsuraan is making here (correct me if I'm wrong,
> tsuraan) is that the combination of prebuilt abstractions (zipwith and
> sum (or more generally, fold)) is more desirable than a hand-coded
> explicitly recursive function because the abstractions are more
> 'functional style' (something I would agree with--in other communities
> (Haskell coming to mind), it is strongly encouraged to use common
> abstractions, even to the point where using explicit recursion is
> sometimes discouraged). But the problem is that these abstractions, or
> at least the combination of them, are slower than a hand-coded
> function explicit recursion function. So, is there any way to make the
> natural abstractions work as fast as the hand-coded version?
> 

I was curious about the speed aspects and whipped up a quick generator
version


fold_gen(F,Acc,G) ->
    case G() of
	{I,N} when is_function(N) ->
	    fold_gen(F, F(I,Acc), N);
	{I,eos} ->
	    F(I,Acc)
    end.

sum_gen(G) ->  
    fold_gen(fun(Val,Acc) -> Val + Acc end, 0, G).

zip_cont([],_) ->  
    eos;
zip_cont(L1, L2) -> 
    fun() -> zip_gen(L1, L2) end.

zip_gen([H1|T1], [H2|T2]) ->
    {H1 * H2, zip_cont(T1, T2)}.

dot_prod_gen(L1, L2) -> 
    sum_gen(zip_cont(L1, L2)).



In my micro benchmarks it looks like the original recursive hand coded one
starts out about 2x as fast with small lists and scales to around 10x as
fast toward 10 million elements from one based on
lists:sum(lists:zipwith(...)).  

The surprising part is that the the zipwith version beats this generator
version by around 2x until you get up toward tens of millions of elements
and creating the large temporary array starts to slow things down.

Anyway, unless the data sets are really huge the difference between these
all are measured in microseconds so whichever is most readable and
maintainable is probably the best.  Save the optimizing for when it really
matters.




-- 
View this message in context: http://www.nabble.com/Generators--tf4170850.html#a11917859
Sent from the Erlang Questions mailing list archive at Nabble.com.




More information about the erlang-questions mailing list