If your starting assumption is that you are smarter than the compiler, then your code should run the fastest as-is, and any compiler optimization flags could only make it slower.
-
-
Replying to @stefan_3d @dgmdavid and
That's not at all how compilers work. If you do not turn optimizations on, it will store and load every value to and from the stack on every line. There is no way to write performance oriented code without turning optimizations on unless you use inline ASM.
2 replies 0 retweets 18 likes -
Replying to @cmuratori @dgmdavid and
Then how would you compile code that you think can't be optimized no more? -O0 with a few select flags at best. -O1 (at least on gcc) already implies things like -freorder-blocks, which is letting the compiler make decisions for you.
1 reply 0 retweets 0 likes -
Replying to @stefan_3d @dgmdavid and
Does this help?https://godbolt.org/z/6DAVpR
1 reply 0 retweets 2 likes -
Replying to @cmuratori @dgmdavid and
I know godbolt, yes. My question is though, how would you compile hand-optimized C if you wanted the compiler to not touch any bits of it other than faithfully translating it line by line to assembly?
1 reply 0 retweets 0 likes -
Replying to @stefan_3d @dgmdavid and
Did you read the actual specific godbolt I sent?
1 reply 0 retweets 0 likes -
-
Replying to @stefan_3d @cmuratori and
But -O2 already does tons of things like automatic inlining, loop invariants, code reodering, switch conversion, etc. It's not a faithful translation of C code any more. At this point, you're relying on the compiler being smart.
1 reply 0 retweets 0 likes -
Replying to @stefan_3d @dgmdavid and
(Is this actually a reply to my godbolt example, or... ? Because it doesn't seem responsive.)
1 reply 0 retweets 0 likes -
Replying to @cmuratori @dgmdavid and
In your simple case, none of the points I mentioned applies, no. But on any real world code base, -O2 will optimize your code beyond just finding the closest ASM equivalent, and you are basing your performance on the compiler making smart decisions.
2 replies 0 retweets 0 likes
No I'm not? I mean I don't do that when I'm programming, anyway. I know exactly what I want the compiler to produce, and usually I can get -O2 to produce _something_ like it, but often times it fucks up and doesn't quite. -O0 is not at all what you claim it is. It's debug mode.
-
-
Replying to @cmuratori @dgmdavid and
I was wrong about -O0, yes. In my eyes though, -finline-small-functions or -freorder-* and -ftree*, which are implied with -O2, do reshape the code in ways I wouldn't say I had in mind when I wrote it.
1 reply 0 retweets 0 likes -
Replying to @stefan_3d @cmuratori and
I'm not saying "Screw optimized code, let the compiler figure it out". I'm not saying "I know better than the compiler". All I'm saying is that I don't expect myself to have the right intuition, so I profile and benchmark before making any assumptions about bottlenecks.
0 replies 0 retweets 0 likes
End of conversation
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.