6 ms·
The console compilers (especially Nintendo's) do tend to be somewhat old & somewhat crappy. I haven't done game code in a couple of years, but for the decade th
by dahart 10y ago
The console compilers (especially Nintendo's) do tend to be somewhat old & somewhat crappy. I haven't done game code in a couple of years, but for the decade that I did, the compilers on all platforms were 5-10 years behind current.
That said, there are reasons that compilers often make slower code than humans, reasons that may always exist. The primary reason to use a compiler isn't to get faster code, it's to use a programming language abstraction and get "fast enough" code. The abstractions are (generally speaking) what add layers that are hard for a compiler to optimize against a person who's not using abstractions. There's a long list of commonly used features that compilers have a hard time optimizing: virtual functions, the threat of aliasing, floating point pipelining, etc. etc.. Compilers are required to make difference choices than humans writing assembly would, because of the abstractions that we choose to use in higher level languages.
Lots of people agree that it's not hard to make assembly faster than an optimizing C compiler.
http://stackoverflow.com/questions/577554/when-is-assembler-faster-than-c http://stackoverflow.com/questions/577554/when-is-assembler-...
- JoeAltmaier 10y agoYet, my very first C++ program was to declare an object that contained a byte, had a constructor to initialize that byte, and a method to return it. My 'main' declared an instance initialized with 'true' and the code simply returned the accessor method's value. The generated code? "mov ax, 255; ret" That was pretty good optimization, seeing completely through the abstraction and flattening it to the simplest possible implementation. I was impressed. Anyway there're lots of further abstractions these days, harder to see through. I imagine a clever human, with domain knowledge, can write a loop to do what a high-level library used to do.
- dahart 10y agoThis is a great example of something compilers definitely do better than humans writing assembly. Inlining code, especially with multiple passes, is something humans don't do. I still modularize in assembly and write functions, factor code used in multiple places into a single spot. The compiler is free to expand the same function inline a thousand places, and then use the expanded code to collapse to fewer instructions. The flattening can definitely be cool to see. So in your example, what would you have done if you'd written the assembly yourself? Do you feel like the compiler did something you wouldn't have?
- JoeAltmaier 10y agoMany programmers would have declared the object, set its members, and written main to fetch and return the data member. Not unreasonable, since humans tend to write abstractions pretty much as they're stated.