Confusion between ++1 and 1++, speed diff

Hi all,

In a C# book, Microsoft Visual C# 2017, it mentions that having either – / ++ before a number in a loop is faster than ++/-- after it.
However, I see that there are many implementations throughout using --/++ after the number in loops.
Is the speed differential true? If so, why is not the norm throughout?

P.S. I am new to programming in general, so any info. is good info.

It might depend on the language you are using and/or the environment you are running it in. I suggest you create a small utility to test this and try it out for yourself. I can tell you that testing with JS through node v12.16.3 on a linux Fedora 32 box makes no difference whatsoever.

Interestingly, there was a slight difference when I counted up from 0 to N (N = 20 billion in this case) in the for loop than when I counted down from N to 0. Going from 0 to N consistently took about 17.6 seconds regardless if I used ++ as prefix or postfix. But counting down from N to 0 consistently took a little longer at 18. 2 seconds (again, regardless if I used -- as prefix or postfix). Is this minor difference something I’m going to worry about? Probably not unless I’m actually looping over a billion items.

Now these results could be different if I ran my test through the browser or on a different operating system. But who has time to do all of that? And if you do get slightly different results for different environments then which one are you going to choose? In summary, I doubt this distinction is anything to worry about except for the most critical time sensitive applications so I wouldn’t worry about it. Personally, I think people are much more comfortable using the postfix version of these operators and thus that’s why most code examples use postfix.

this might help

Honestly, I’d be highly suspicious of that book’s quality after reading something like that.