The performance of LengthWhile
has been improved in v11.1, now the lengthwhile
below is no longer faster.
A friend of mine showed me this example, it's a test comparing LengthWhile
to a self-made lengthwhile
written in a direct and conventional way:
lengthwhile[x_, t_] := Module[{i = 0, l = Length@x}, While[i < l && t@x[[i + 1]], i++]; i]
lst = RandomInteger[{-2, 2}, {10^4, 10}];
rst1 = LengthWhile[#, # >= 0 &] & /@ lst; // AbsoluteTiming
rst2 = lengthwhile[#, # >= 0 &] & /@ lst; // AbsoluteTiming
rst1 == rst2
{3.941000, Null} {0.474000, Null} True
LengthWhile
is much slower than the reinvented wheel! Why? Simply a bad performance of LengthWhile
? Or LengthWhile
isn't used in a proper way?