7高效的方式乘 [英] Multiply by 7 in efficient way
问题描述
最近,我遇到了以下面试问题:
I recently encountered the following interview question:
你怎么能由7以高效,优化的方式繁衍多少?
How can you multiply a number by 7 in an efficient and optimized way?
我知道,我可以通过8(或左移三个位)相乘,然后减去原始值:
I know that I can multiply by 8 (or left-shift by three bits) and then subtract the original value:
num = (num << 3) - num;
但是否有任何其他的解决方案。
but are there any other solutions.
推荐答案
要获得7的倍数以有效的方式:
To get a multiple of 7 in an efficient way:
7
7的7,回答你问的问题多,但我敢肯定它不会回答你的意思是问的问题。
7 is a multiple of 7. That answers the question you asked, but I'm sure it doesn't answer the question you mean to ask.
的编辑:以上是根据问题的原题,我刚刚修正的
要乘的按的7高效,只写,例如:
To multiply by 7 efficiently, just write, for example:
x * 7
和调用与优化的编译器。让编译器弄清楚是否一个MUL指令或类似的东西(X&LT;&LT; 3) - X
更有效的当前计算机的。
and invoke your compiler with optimization. Let the compiler figure out whether a single MUL instruction or something like (x<<3) - x
is more efficient for the current machine.
还有另一个隐含的问题在这里:什么答案面试官想要?我的希望的说:让这件事编译器无忧将是一个可以接受的答案。 (X&LT;&LT; 3) - X
可能是最明显的微型优化 - 但它可以产生不正确的答案,如果 X&LT; 3;
溢出,并根据系统的不同,可能会比一MUL指令慢。
There's yet another implicit question here: what answer was the interviewer looking for? I hope that "let the compiler worry about it" would be an acceptable answer. (x<<3) - x
is probably the most obvious micro-optimization -- but it can yield incorrect answers if x<<3
overflows, and depending on the system it might be slower than a MUL instruction.
(如果我是面试官,我会用的问题比任何具体的答案一个很好的解释和理解pssed更多IM $ P $)。
(If I were the interviewer, I'd be more impressed by a good explanation and understanding of the issues than by any specific answer.)
修改
在进一步的思考,已经在这里讨论的各种微型优化的可能的,如果你知道更多关于 X
比编译器。如果你知道,因为你的程序逻辑的性质,即 X
将永远范围0..10,然后查找表可以很容易地比乘快操作。或者,如果你知道 X
中的时间范围的99%,也可能降低为一个实际的乘法查找表可能只是事情。
On further thought, the kinds of micro-optimizations that have been discussed here might be useful if you know more about the possible values of x
than the compiler does. If you know, because of the nature of your program's logic, that x
will always be in the range 0..10, then a lookup table could easily be faster than a multiply operation. Or if you know that x
is in that range 99% of the time, a lookup table with a fallback to an actual multiplication might be just the thing.
但是,如果你的程序流程的编译器的分析并没有允许它的证明的是 X
总是在这个范围内,那么它可以'T执行这种优化。
But if the compiler's analysis of your program flow doesn't allow it to prove that x
is always in that range, then it can't perform this kind of optimization.
不过,这样的情况下是非常罕见的。当你的code在一个新的环境中运行,其中 X
可以是11(也许这是一个更大的显示设备上运行)的 KABOOM 。和性能的改善很可能是不首先显著
But such circumstances are very rare. And when your code runs in a new environment where x
can be 11 (perhaps it's running on a device with a larger display), kaboom. And the performance improvement very likely wasn't significant in the first place.
有时候微优化是合适的,但在开发和测试时间大幅成本。做到这一点只有在实际的测量的指示,这是值得的。
There are times when micro-optimization is appropriate, but there is a substantial cost in development and testing time. Do it only if actual measurements indicate that it's worth it.
这篇关于7高效的方式乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!