5 ms·
> For example I try to use ++x instead of x++ ++x can be slower, depending on the context -- it introduces a sequence point.
by santaclaus 9y ago
> For example I try to use ++x instead of x++
++x can be slower, depending on the context -- it introduces a sequence point.
- mark-r 9y agoReally? If you have the time, I'd love an explanation. Or if you can point to some other resource that would be great too.
- santaclaus 9y agoI remember seeing a blog post from Andrei Alexandrescu that I can't seem to dig up, but this SO post seems to be a nice summary [1]. In short, in 99.9999999% of usages post increment is probably better. [1] https://stackoverflow.com/a/38947878 https://stackoverflow.com/a/38947878
- mark-r 9y agoThanks. I guess I'm applying this rule only when the result of the increment isn't being used directly, so there is no dependency on the operation. When the result is used, the semantic difference between ++x and x++ obviously determines the choice.
- deleted 9y ago[deleted]
- flamedoge 9y agoJust look at the liveness of the expression x+1. post-increment means expression has to be alive before whatever uses '++x' whereas pre-increment can delay until next usage of x.