If-else Vs Ternary
If else vs Ternary comparison in c++.
The actual code for If-else is very simple but it could be lengthy however for Teranary it can be complicated but short.
Example if_else.cpp gist
https://gist.github.com/bhaumikmistry/cffd44f80b7beca4860cb1876adb8e33
https://gist.github.com/bhaumikmistry/cffd44f80b7beca4860cb1876adb8e33/#file-if_else-cpp
Details on assambly
Using gcc -S file_name
you can create assambly code alos available as gist. The assambly code comes out to be equal for both files as you'll see the int initialization can be seen different, and that is the only difference in the file with gcc --version
which is gcc.exe (i686-win32-dwarf-rev1, Built by MinGW-W64 project) 7.2.0
.
Graphs
After running in loop n=41000000
I have collected some data and the chart shows simple answer as the iteration goes up the difference in time execution also grows.

More discussion and stackOverFlow answer links
Last updated
Was this helpful?