[erlang-questions] Can I do the same with a fold easily
Mon Oct 19 23:41:21 CEST 2015
by doing some testing, strangely using (X rem 2 ) =:= 1 is almost
always better than X band 1 =:= 1. On my machine at least.
go() -> List = lists:seq(1, 1000) ,
T1 = erlang:timestamp(),
lists:partition(fun (X) -> (X rem 2 ) =:= 1 end, List),
T3 = erlang:timestamp(),
lists:partition(fun (X) -> X band 1 =:= 1 end, List),
Le 19/10/2015 22:53, Éric Pailleau a écrit :
> 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).
> 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
> erlang-questions mailing list
More information about the erlang-questions