4 ms·
I was sort of surprised in example 4 that the compiler couldn't optimize {a[0]++; a[0]++;} to {a[0]+=2;}, given that the allocation was right there in the same
by Symmetry 8y ago
I was sort of surprised in example 4 that the compiler couldn't optimize {a[0]++; a[0]++;} to {a[0]+=2;}, given that the allocation was right there in the same function as the usage.
EDIT: Should have kept reading, other people apparently had this same comment and it was addressed. This was for C# rather than C++ so no wonder my intuition was off.
- rocky1138 8y agoIt was addressed that it didn't optimize it this way, but no one has clarified why it didn't. I'm curious!
- ygra 8y agoA Just-in-time compiler has to find a balance between compilation time (which slows down your program or makes it slower to start) and optimization quality. So a lot of optimizations that are OK in C++ where you have lots of time because of AOT compilation are not feasible for a JIT. Things could be different by now, though, with RyuJIT and tiered compilation. That article is quite old (although processors still work the same, just with larger caches).
- monocasa 8y agoConstant propagation is pretty cheap though and is a common JIT compiler technique.