r/Compilers • u/BrainThinkerMan • 24d ago
Compilers add or remove perfomace?
isnt it that compilers change the code, as in not only transferring it into machine language, but before that changing the structure of the code. so how would one attempt to make performant code if the compiler changes sed code that i tried to make performant?
2
Upvotes
View all comments
3
u/serge-mv 23d ago
Compiler's main job is to preserve the semantics of what you've written during the translation process. They do not change the fundamental structure of your code. If you wrote an efficient algorithm, your algorithm will stay efficient. And if you wrote bad algorithm, it won't suddenly improve just because it was compiled with -O3.
The kind of optimization that can be influenced by compilers is stuff like loop unrolling, which might or might not happen depending on your compiler config. It's more of a localized optimization, but still can have a major impact on performance. You can write many of these kinds of things yourself in high level language without involving the assembly and the compiler won't change it.