Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reduce/3 has clauses specialized for various enumerable types, so should be much faster than the general Enumerable.reduce/3 case, especially that we don't need additional facilities like terminating the reduction early. Comparing 3 implementations: * map based elements |> Enum.map(&(if check(&1), do: 1, else: 0)) |> Enum.sum * count/2 based elements |> Enum.count(&check/1) * filter based elements |> Enum.filter(&check/1) |> Enum.count We get following results before and after the change: len | map | count before | count after | filter ------|-----------|--------------|-------------|------------ 10 | 2.1706 μs | 7.0754 μs | 1.6304 μs | 1.9765 μs 100 | 19.921 μs | 27.579 μs | 13.057 μs | 14.591 μs 1000 | 168.93 μs | 178.93 μs | 130.76 μs | 163.43 μs 10000 | 2128.9 μs | 1822.1 μs | 1298.2 μs | 1664.6 μs The most idiomatic code is now also the fastest.
- Loading branch information