[erlang-questions] Can I do the same with a fold easily

PAILLEAU Eric <>
Mon Oct 19 23:41:21 CEST 2015


Hi,
by doing some testing, strangely using (X rem 2 ) =:= 1  is almost 
always better than X band 1 =:= 1. On my machine at least.

______________________________________________________________________
-module(odd_even).

-export([go/0]).


go() ->   List = lists:seq(1, 1000) ,
           T1 = erlang:timestamp(),
           lists:partition(fun (X) -> (X rem 2 ) =:= 1 end, List),
           io:format("~p 
microseconds~n",[timer:now_diff(erlang:timestamp(), T1)]),

           T3 = erlang:timestamp(),
           lists:partition(fun (X) -> X band 1 =:= 1 end, List),
           io:format("~p 
microseconds~n",[timer:now_diff(erlang:timestamp(), T3)]).
______________________________________________________________________


Le 19/10/2015 22:53, Éric Pailleau a écrit :
> Hi,
> I agree but like Joe says: let it work,  then optimize if needed.
> My main remark was: Splitting odd and even does not need to check odd then even, but check it is odd Else it is even (or contrary).
> Regards
>
> Le 19 oct. 2015 4:54 AM,  a écrit :
>>
>> "Éric Pailleau" <>
>>
>>> A number cannot be both even and odd, so simply take odd numbers in a
>>> variable O from list L , even numbers will be L -- O.
>>
>> Here we run into the question "what does BEST mean?"
>>
>> Now Xs -- Ys cannot assume anything about the contents of Xs or Ys,
>> other than them both being well formed lists.  So while it *could*
>> be implemented in O(|Xs|+|Ys|+|Xs|lg|Ys|) time by building some kind
>> of balanced search tree from Ys, or even better by building some
>> kind of hashed set, the cost of building the supporting data structure
>> would likely be worse than the cost of traversing L twice or using
>> lists:partition/2.
>>
>>
>>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list