简单的问题,是有可能简化(或以较低费用运作取代司或组合)
(k/m)%n
在变数为单位的情况下,操作者为C风格分工和模块操作者。
请允许我略微回答问题,除非变数是基数2,在什么条件下(例如某些变数可能保持不变)可以简化表述(或部分使用基2操作)以删除分部分或模块?
这对我来说是学习数字理论,特别是基础2的trick计,而不是在优化业绩方面进行。
谢谢。
简单的问题,是有可能简化(或以较低费用运作取代司或组合)
(k/m)%n
在变数为单位的情况下,操作者为C风格分工和模块操作者。
请允许我略微回答问题,除非变数是基数2,在什么条件下(例如某些变数可能保持不变)可以简化表述(或部分使用基2操作)以删除分部分或模块?
这对我来说是学习数字理论,特别是基础2的trick计,而不是在优化业绩方面进行。
谢谢。
各位可以采用这样的方法,对持小不变的提名。
k/m=k*(1/m)
x=(1<<16)/m
k/m=(k*x)>>16
答案可能并不准确,取决于投入。
关于与小奇常分母的划分,您可使用
<代码>% 2^32 当然在32英寸内无。 采用这种数字,甚至数字因素,然后适用。 Hackers Delight有一章涉及分类。 简单模块和权力分工。3 2863311531 11 3123612579
5 3435973837 13 3303820997
7 3067833783 15 4008636143
9 954437177 17 4042322161
x/11 == x*3123612579 % 2^32
x/44 == (x*3123612579 % 2^32) >> 2
x%m == x&(m-1)
x/m == x>>log2(m) // assumes log2(m) is known, not calculated
明显的优化:
k % m
.0
.(k >> 2) % n
;(k / m) & (n - 1)
;第1号和2号的检查是微不足道的。
检查两种权力时,采用:
void isPowerOfTwo(unsigned int x)
{
return x & (x - 1) == 0;
}
加上Peter Alexander的答复
页: 1 页: 1 页: 1
(1) k; 答案总是0。
2)k = m: 回答总是1(除非是1,见5)。
3)k / m < n: 答案为k/m。
4) k < ( m * n ): The answer is always k / m. This particular condition is not very conducive to optimization since m*n would not be reused and it should not be much faster than modulo unless m and/or n are powers of 2 in which case you d still be better using 7. and/or 8.
参考:
页: 1
页: 1
7) m 功率为2:例如,如果面积为4,你可以使用(k >> 2)n;
8)n 是2 :表达(k /m)和(n - 1)
I suppose that it depends. A right arithmetic shift would give you division by 2 and with some additional arithmetic you could make that into a division by any number. Similarly I would think that you could do the same with the modulo operator. In reality, unless your processor is lacking the necessary hardware, I wouldn t have thought that you would gain anything.
edit Actually thinking about it, more thought is needed here as strictly speaking, for a negative number a signed shift wouldn t work as a divide by 2.
如果我正确地记住,在C标准中,算术转变权的行为没有界定(关于权力的会谈2),汇编者也依赖。
如果我们只想一下数字理论的逻辑,那就是一个不同的问题。 让我思考一下。
If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...
I m using Electro in Lua for some 3D simulations, and I m running in to something of a mathematical/algorithmic/physics snag. I m trying to figure out how I would find the "spin" of a sphere of a ...
There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...
As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...
Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...
I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...
Given an array of integers arr = [5, 6, 1]. When we construct a BST with this input in the same order, we will have "5" as root, "6" as the right child and "1" as left child. Now if our input is ...
I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...