Ad
  • Custom User Avatar

    Maybe (I'm not sure) because into_iter() actually copies i32, and iter() creates references. While references are also 32 bit on x32 arch, they are 64 bit on x64 arch. So it may cause unnecessary creation of muliple u64 pointers that slow down the whole thing.

  • Custom User Avatar

    using iter() instead of into_iter() will degrade performance

    This is really weird.

    Isn't into_iter giving you a copy, while iter giving you a reference? Why is making a copy faster?

  • Default User Avatar

    If it were only the sum() or only the fold() as on stackoverflow this would be true.
    But benchmarking shows that combining map() and sum() is about 10% slower.
    Also, using iter() instead of into_iter() will degrade performance by another 30-40%.

    Using vectors with 100 million elements on my machine:
    iter + map + sum = ~3.1-3.3s
    into_iter +map + sum = ~2.5-2.6s
    iter + fold = ~2.9-3.1s
    into_iter + fold = ~2.3-2.4s

  • Custom User Avatar

    Actually IntStream.of() calls Arrays.stream()
    From IntStream.java :

    <...>
    /**
     * Returns a sequential ordered stream whose elements are the specified values.
     *
     * @param values the elements of the new stream
     * @return the new stream
     */
    public static IntStream of(int... values) {
        return Arrays.stream(values);
    }
    <...>
    
  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    Is this solution better than solution with Arrays.stream()?