Weird loop optimizations with C++ #643
DelinWorks
started this conversation in
General
Replies: 1 comment 4 replies
-
Does the test result is from release build? |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
One question about cpp is when I was working on improving the particle system, there were comments saying that separating loops to fill each data field was the best optimization, like:
![image](https://user-images.githubusercontent.com/45469625/170127949-5de77be5-4f3d-4f79-82dd-33796af7597d.png)
And then I was told that
![image](https://user-images.githubusercontent.com/45469625/170128021-8fdc90d0-4c5c-46df-8cfb-9a7b07b6599e.png)
std::fill_n
is even better for static values, like:Soo I've ran a test and the results were shocking:
Why is the method of combining the loops into a single one the fastest? even though people say that separating loops is faster because cache hit rates are better.
A single loop is something like this:
![image](https://user-images.githubusercontent.com/45469625/170128412-9cf2eacb-8562-4039-b4b4-bc726ffedb13.png)
Can someone explain to me why is this the case?
The code:
Beta Was this translation helpful? Give feedback.
All reactions