Fastest way to loop through an array X E

DO NOT RUN IN DEBUG MODE. Rust Playground, I have tried with i2 count down in all spots and it consistently performs best. What I mean by spot is which of first to last test run it is. It is almost a dead heat. In debug mode it is no contest, same one is fastest by a long shot. I challenge everyone to find one way that is consistently better enough.

If we had a reverse for loop or iterator that elides bounds checks might be even faster. I could even optimize further some by making things outside loops only. Why it is all in there is for fairness. I also tried with array.iter().enumerate(), such a fail in performance I took it out. It all measured in nanoseconds for release, in debug it was microseconds before I thousand multiplied counts and array size. Release is like a billion times faster than debug, not exaggerating much. It is consistently for first two trials warming up.

Anyway, yes, I can now prove that this i2-=1 way is fastest to iterate over an array. Prove me wrong with a few trials of anything possibly faster. Hope you enjoyed this and learned something. I am a performance nut so I do these things. It is a megabyte of storage sure but small price to pay to teach everyone that this is fastest as of now. Please use a local machine if you want more data. Feel free to adjust stuff. Maybe try on Vec if you want, or not. Unless there is some great thing I am missing, i2 loop is as fast as it gets.

Note that if you separate it into a loop {} with if i2 == 0 then break it is slower. This pre-decrement rules. X E.

Rust Playground, again, same deal, but I tried iter_mut. X E.

Rust Playground, this one is much better, like before but this time actually allocating something. X E.

Rust Playground, this one removed a thing not actually setting. X E.

Rust Playground, Inconclusive but definitely close between first and 3rd now, I say 3rd, it is basically reverse iter. Only code review and benchmark with this one. I also noticed Rust is randomly skipping. X E.

All of them compile to a call to memset Compiler Explorer

2 Likes

Sorry, all but that last one is faulty I think. X E.

As far as I can see, they all still compile to the same assembly (though with some interesting-looking unrolling): Compiler Explorer

1 Like

I am seeing differences but they are close. X E.

But are those differences meaningful? If you're not using statistical techniques, you're probably just measuring noise, especially if they compile to the same thing.

Obligatory link to https://youtu.be/r-TLSBdHe1A, since you might just be measuring the size of your environment variables, or something.

1 Like

Well, if you can do it better, try. If not, kind of sorry I made this thread, feel free to delete. X E.