问题描述
在我的程序中,我使用很多整数除以10 ^ x和幂10的整数mod函数。
例如:
unsigned __int64 a = 12345;
a = a / 100;
....
或:
$ b b
unsigned __int64 a = 12345;
a = a%1000;
....
如果我要使用右移位>>
,那么我会得到 2 ^ x
的模式,这不是我想要的。
长答案:
NO。
说明:
编译器已经为您优化了这样的语句。
如果有一种实现这种方法的技术比整数除法更快,那么编译器已经知道它了,将会应用它(假设你打开优化)。
如果你提供适当的架构标志,编译器甚至可能知道特定的快速架构特定的汇编,将提供
总之,编译器会击败人类99.9999999%的时间在任何优化技巧(尝试它记得添加优化标志和架构标志)。所以你通常可以做的最好的就是等于编译器。
如果通过一些奇迹,你发现一个尚未找到的方法,大会boffins与后端编译器团队。然后,请让他们知道,下一个版本的流行的编译器将更新为未知(谷歌)的10优化技巧。
In my program I use a lot of integer division by 10^x and integer mod function of power 10.
For example:
unsigned __int64 a = 12345;
a = a / 100;
....
or:
unsigned __int64 a = 12345;
a = a % 1000;
....
If I am going to use the right bit shift >>
, then I will get mode of 2^x
, which is not what I want.
Is there any way I can speed up my program in integer division and mod functions?
Short Answer: NO
Long Answer:NO.
Explanation:
The compiler is already optimizing statements like this for you.
If there is a technique for implementing this quicker than an integer division then the compiler already knows about it and will apply it (assuming you turn on optimizations).
If you provide the appropriate architecture flags as well then the compiler may even know about specific fast architecture specific assembles that will provide a nice trick for doing the operation otherwise it will apply the best trick for the generic architecture it was compiled for.
In short the compiler will beat the human 99.9999999% of the time in any optimization trick (try it remember to add the optimization flag and architecture flags). So the best you can normally do is equal the compiler.
If by some miracle you discover a method that has not already been found by the Assembly boffins that work closely with the backend compiler team. Then please let them know and the next version of the popular compilers will be updated with the 'unknown (google)' division by 10 optimization trick.
这篇关于C ++快速除法/ mod由10 ^ x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!