Is shift still worth it?

Started by
27 comments, last by iMalc 19 years, 2 months ago
Quote:Original post by Nice Coder
An imul is 4 clocks, a Shift is one clock, and an add/subt is one clock.

This sort of reasoning is absolutely pointless due to modern processor design, with 20 stage pipelines and the vast disconnect between processor, cache, and memory latency. Due to the complexity of the operation of modern processors, it is almost impossible to predict speeds down to the clock cycle level. Furthermore, it is counter-productive, since it is highly likely that this bit of code will never be a bottleneck in a program.

It takes some experience to be able to predict in a general fashion the relative performance of your program. Even with that experience, one should look for profile-guided optimization. And that optimization should initially be at the algorithm and data structure level. You are much more likely to get a noticeable speed boost by re-organizing your data and data access to be cache friendly, than you are by optimizing integer multiplication.

Finally, as has been pointed out, your compiler is generally smart enough to handle a good deal of optimization. Worry about good design - I would hazard a guess that most projects here aren't suffering for lack of performance, but lack of features.
Advertisement
Premature optimization is the root of all evil - Edsger W. Dijkstra

The key is to write your code, ensure that it is correct, profile it, and then, only if it is not adequately performant, optimize. Discussions of "optimization" without context are meaningless, and tend to promote Bad Code™ - habits such as writing everything as cryptically as possible in the mistaken notion that "it's faster."

Code exists primarily to communicate to other programmers; that it is interpreted (directly or in a converted form) by a machine is a secondary benefit. If machines were all we targeted, then we might as well keep writing in binary. Your code should be readable and express concepts as clearly as possible first. It should be well commented, and additional remarks should be in place whenever there is a need for cryptic optimizations.

Happy hacking.
Great answers all around, thanks.

iMalc, I made this post because of a question "Give me a fast way of multiplying a number by 7" in a "Frequently Asked Questions in Interviews" Doc.

From the answers here, I'm guessing that question is a bit old, from a time when processors were simpler and the optimizations made by compilers didn't cover shifting.
Quote:Original post by Demosthenes
Great answers all around, thanks.

iMalc, I made this post because of a question "Give me a fast way of multiplying a number by 7" in a "Frequently Asked Questions in Interviews" Doc.

From the answers here, I'm guessing that question is a bit old, from a time when processors were simpler and the optimizations made by compilers didn't cover shifting.


Indeed. I had some of this optimization code in one of my old game programming books, which was for developing DOS games (I mean real DOS, not your pansy ass fake stuff like MS-DOS :P). But as repeatedly said here, it dosn't help much (if at all) these days :).
CPUs are in the 3 GHz range now. Saving a cycle means saving one three-billionth (~0.000000000033) of a second. When computers's speed were measured in the KHz or <100 MHz, saving a few cycles might help in your innermost loop (which in those days was called 64k times per frame to put something interesting in each pixel on the screen, maybe alpha blend using a palette, etc). These days, don't count on it because processors are faster, code is run less often, and the GPU takes care of pixel processing. Not only that, but processors are more complicated these days and can optimize the machine code as the execute it (pipelining and instruction reordering etc). Unless you're an expert, the compiler knows more than you about how to make your code run faster in relation to microoptimizations. The only real code-level optimizations you can make these days are converting things to vector instructions, and even that is already tackled to some degree by certain compilers.

The kind of optimizations you should worry about are which algorithm fits your needs, which data structure has characteristics that match your use of the data structure, etc.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Quote:Original post by Oluseyi
Premature optimization is the root of all evil - Edsger W. Dijkstra


Knuth said that, not Dijkstra.
n * 7; is faster to write and faster to read. As long as you want the product of seven and n, what else could matter?
Quote:Original post by NorthWoodsman
Quote:Original post by Oluseyi
Premature optimization is the root of all evil - Edsger W. Dijkstra


Knuth said that, not Dijkstra.
In "The Errors of TEX", Knuth was quoting C.A.R. Hoare. I tried to find it online, but I only found old versions of the text that didn't say what a log of other sites say is in the July 1989 version of the paper which is only published in Literate Programming (1992):
"But I also knew, and forgot, Hoare's dictum that premature        optimization is the root of all evil in programming."
Quote:Original post by Demosthenes
Great answers all around, thanks.

iMalc, I made this post because of a question "Give me a fast way of multiplying a number by 7" in a "Frequently Asked Questions in Interviews" Doc.

From the answers here, I'm guessing that question is a bit old, from a time when processors were simpler and the optimizations made by compilers didn't cover shifting.
Oh, that makes sense then. Yeah it's probably the answer they want, but I would first suggest "Profile to see it there is any point in performing any optimisation on it. Then perhaps remove the need to perform the multiplication at all, or hoist it out of a loop if possible." Or maybe ask what processor and what data type first as it depends upon the processor. If it were a verbal test, they'd probably be blown away with this kind of answer.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement