3 ms·
Actually, on x86 at least, I'm not sure there'd be much difference even from a naive compiler - the modern version would compile to a LEA instruction followed b
by jlangenauer 15y ago
Actually, on x86 at least, I'm not sure there'd be much difference even from a naive compiler - the modern version would compile to a LEA instruction followed by a MOV, whereas the old version would be an INC instruction followed by the MOV. From memory, both the INC and LEA instructions execute in one clock tick.
- kator 15y agoTrue. I imagine modern compilers optimize this sort of stuff very quickly. Back in the time that code was written it was better to just optimize directly in the code and not wonder if the compiler would do it for you. I'm not advocating the code, more I wonder about how much the general population of developers has lost connection with the underlying performance of things. The systems we work with today are so powerful that they forgive errors that just could not be ignored on older systems. I recently re-wrote a streaming MapReduce job from Perl to C code and improved it literally 10x in speed. I resisted doing it because most of my team doesn't understand C well enough to slice and dice a bunch of pointers and buffers etc. I wanted maintainable code that the rest of my team could maintain. But in the end I needed performance so short of writing it in assembly C was the next best thing.
- masklinn 15y ago> True. I imagine modern compilers optimize this sort of stuff very quickly. Just checked with Clang (and otool), in Os and O2 the disassembly is exactly the same whether one uses struct accesses or raw pointer assignments. There are differences in O0 though.