Timeline for When I test out the difference in time between shifting and multiplying in C, there is no difference. Why?
Current License: CC BY-SA 3.0
6 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| May 2, 2018 at 14:48 | comment | added | derobert | @AkshayLAradhya I can't say what your compiler is doing, but I confirmed again that gcc -O3 (now with 7.3) still removes the loop entirely. (Make sure to switch to a long instead of int if required, otherwise it optimizes it into an infinite loop due to overflow). | |
| May 2, 2018 at 14:37 | comment | added | DollarAkshay | I get what you are saying, but I don't think the compiler is removing the entire loop. You can easily test out this theory by simply increasing the number of iterations. You will see that increasing the iterations does make the program take longer. If the loop was entirely removed this wouldn't be the case. | |
| Jul 22, 2014 at 22:08 | comment | added | derobert | @MichaelT Yep. That's what I meant by "Note that a sufficiently determined optimizer could still optimize away the loop (it relies entirely on constants known at compile time)." | |
| Jul 22, 2014 at 22:07 | review | First posts | |||
| Jul 23, 2014 at 0:45 | |||||
| Jul 22, 2014 at 22:07 | comment | added | user40980 | Note also that the optimizer can (and will) optimize out operations on constants (even in a loop) as shown in sqrt c# vs sqrt c++ where the optimizer was able to replace a loop summing a value with the actual sum. To defeat that optimization you need to use something determined at runtime (such as a command line argument). | |
| Jul 22, 2014 at 21:58 | history | answered | derobert | CC BY-SA 3.0 |