4 ms·
> Obviously if the function is on a hot path iterating and summing with a number is far more efficient versus filtering. I got curious and checked this in Rust
by veber-alex 3y ago
> Obviously if the function is on a hot path iterating and summing with a number is far more efficient versus filtering.
I got curious and checked this in Rust, the generated assembly is the same!
https://rust.godbolt.org/z/jhGWdYPz1 https://rust.godbolt.org/z/jhGWdYPz1
- proto_lambda 3y agoBecause you're not actually creating a filtered list, you're just filtering an iterator.
- nonameiguess 3y agoNeither of your examples is doing what the parent suggested as an alternative, which is simply looping and counting rather than creating an anonymous function to repeatedly call. I doubt this makes an enormous difference in practice, but you can see your generated assembly doing all the argument passing and stack frame setup, which is extra instructions compared to just straight looping and counting.