<p>Which compiles to faster code: "ans = n * 3" or "ans = n+(n*2)"?</p>

<p>Assuming that n is either an int or a long, and it is is running on a modern Win32 Intel box.</p>

<p>Would this be different if there was some dereferencing involved, that is, which of these would be faster?</p>

<pre>

long    a;
long    *pn;
long     ans;

...
*pn = some_number;
ans = *pn * 3;
</pre>

<p>Or</p>

<pre>
ans = *pn+(*pn*2);
</pre>

<p>Or, is it something one need not worry about as optimizing compilers are likely to account for this in any case?</p>

[{'answer_id': 53759, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 7, 'selected': True, 'text': '<p>IMO such micro-optimization is not necessary unless you work with some exotic compiler. I would put readability on the first place.</p>\n'}, {'answer_id': 53761, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': 1, 'selected': False, 'text': '<p>It does depend on the compiler you are actually using, but very probably they translate to the same code. </p>\n\n<p>You can check it by yourself by creating a small test program and checking its disassembly.</p>\n'}, {'answer_id': 53762, 'author': 'Paul Tomblin', 'author_id': 3333, 'author_profile': 'https://Stackoverflow.com/users/3333', 'pm_score': 1, 'selected': False, 'text': "<p>Most compilers are smart enough to decompose an integer multiplication into a series of bit shifts and adds.  I don't know about Windows compilers, but at least with gcc you can get it to spit out the assembler, and if you look at that you can probably see identical assembler for both ways of writing it.</p>\n"}, {'answer_id': 53763, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 0, 'selected': False, 'text': '<p>Compilers are good at optimising code such as yours. Any modern compiler would produce the same code for both cases and additionally replace <code>* 2</code> by a left shift.</p>\n'}, {'answer_id': 53765, 'author': 'Will Dean', 'author_id': 987, 'author_profile': 'https://Stackoverflow.com/users/987', 'pm_score': 2, 'selected': False, 'text': "<p>This would depend on the compiler, its configuration and the surrounding code.</p>\n\n<p>You should not try and guess whether things are 'faster' without taking measurements.   </p>\n\n<p><em>In general</em> you should not worry about this kind of nanoscale optimisation stuff nowadays - it's almost always a complete irrelevance, and if you were genuinely working in a domain where it mattered, you would already be using a profiler and looking at the assembly language output of the compiler.</p>\n"}, {'answer_id': 53781, 'author': 'Antti Kissaniemi', 'author_id': 2948, 'author_profile': 'https://Stackoverflow.com/users/2948', 'pm_score': 3, 'selected': False, 'text': "<p>As it's easy to measure it yourself, why don't do that? (Using <code>gcc</code> and <code>time</code> from cygwin)</p>\n\n<pre><code>/* test1.c */\nint main()\n{\n    int result = 0;\n    int times = 1000000000;\n    while (--times)\n        result = result * 3;\n    return result;\n}\n\nmachine:~$ gcc -O2 test1.c -o test1\nmachine:~$ time ./test1.exe\n\nreal    0m0.673s\nuser    0m0.608s\nsys     0m0.000s\n</code></pre>\n\n<p>Do the test for a couple of times and repeat for the other case.</p>\n\n<p>If you want to peek at the assembly code, <code>gcc -S -O2 test1.c</code></p>\n"}, {'answer_id': 53797, 'author': 'Ferruccio', 'author_id': 4086, 'author_profile': 'https://Stackoverflow.com/users/4086', 'pm_score': 4, 'selected': False, 'text': "<p>It doesn't matter. Modern processors can execute an integer MUL instruction in one clock cycle or less, unlike older processers which needed to perform a series of shifts and adds internally in order to perform the MUL, thereby using multiple cycles. I would bet that</p>\n\n<pre><code>MUL EAX,3\n</code></pre>\n\n<p>executes faster than</p>\n\n<pre><code>MOV EBX,EAX\nSHL EAX,1\nADD EAX,EBX\n</code></pre>\n\n<p>The last processor where this sort of optimization might have been useful was probably the 486. (yes, this is biased to intel processors, but is probably representative of other architectures as well).</p>\n\n<p>In any event, any reasonable compiler should be able to generate the smallest/fastest code. So always go with readability first.</p>\n"}, {'answer_id': 53815, 'author': 'Skizz', 'author_id': 1898, 'author_profile': 'https://Stackoverflow.com/users/1898', 'pm_score': 2, 'selected': False, 'text': '<p>It\'s not difficult to find out what the compiler is doing with your code (I\'m using DevStudio 2005 here). Write a simple program with the following code:</p>\n\n<pre><code>int i = 45, j, k;\nj = i * 3;\nk = i + (i * 2);\n</code></pre>\n\n<p>Place a breakpoint on the middle line and run the code using the debugger. When the breakpoint is triggered, right click on the source file and select "Go To Disassembly". You will now have a window with the code the CPU is executing. You will notice in this case that the last two lines produce exactly the same instructions, namely, "lea         eax,[ebx+ebx*2]" (not bit shifting and adding in this particular case). On a modern IA32 CPU, it\'s probably more efficient to do a straight MUL rather than bit shifting due to pipelineing nature of the CPU which incurs a penalty when using a modified value too soon.</p>\n\n<p>This demonstrates what aku is talking about, namely, compilers are clever enough to pick the best instructions for your code.</p>\n'}, {'answer_id': 63220, 'author': 'Mark D', 'author_id': 7452, 'author_profile': 'https://Stackoverflow.com/users/7452', 'pm_score': 0, 'selected': False, 'text': '<p>Trust your compiler to optimize little pieces of code like that. Readability is much more important at the code level. True optimization should come at a higher level.</p>\n'}, {'answer_id': 304032, 'author': 'Angel', 'author_id': 23285, 'author_profile': 'https://Stackoverflow.com/users/23285', 'pm_score': 1, 'selected': False, 'text': "<p>It doesn't care. I think that there are more important things to optimize. How much time have you invested thinking and writing that question instead of coding and testing by yourself? </p>\n\n<p>:-)</p>\n"}, {'answer_id': 304053, 'author': 'Artelius', 'author_id': 31945, 'author_profile': 'https://Stackoverflow.com/users/31945', 'pm_score': 1, 'selected': False, 'text': '<p>As long as you\'re using a decent optimising compiler, just <em>write code that\'s easy for the compiler to understand</em>. This makes it easier for the compiler to perform clever optimisations.</p>\n\n<p>You asking this question indicates that an optimising compiler knows more about optimisation than you do. So trust the compiler. Use <code>n * 3</code>.</p>\n\n<p>Have a look at <a href="https://stackoverflow.com/questions/288942/how-to-convert-last-3-digits-of-number-into-0#289034">this answer</a> as well.</p>\n'}]
['https://Stackoverflow.com/questions/53757', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3137/']