Show Navigation
Conversation
Notices
-
TIL pre-crement is faster than post-crement but probably doesn't matter to the .Net compiler. Also, counting to zero for loops is slightly quicker than counting up.
-
@thatbrickster Yeah counting to zero you can use the "loop" assembly instruction, assuming the rcx register is available. I think the difference should be pretty marginal though, as I've almost never seen anyone tdo it.
Assuming dot net works like C++, pre vs post increment shouldn't matter for builtin types, only for non-builtin ones that need to first make a copy then increment then return the copy.
-
@ayy It's said that on a 2GHz computer a millisecond would be saved for every one million iterations. Not much but the code in question is a hot path.
-
@thatbrickster I wonder... Modern processors are pretty complex beasts and I think they would emit equivalent uops for the two cases.
Having an explicit loop may help the branch predictor for the first iterations, and the code size will be marginally smaller relieving cache pressure.