php php中的整数除法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2673798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:18:26  来源:igfitidea点击:

integer division in php

phpperformancemath

提问by oezi

i'm looking for the fastest way to do an integer division in php. for example, 5 / 2 schould be 2 and 6 / 2 should be 3 and so on. if i simply do this, php will return 2.5 in the first case, the only solution i could find was using intval($my_number/2)- wich isn't as fast as i want it to be (but gives the expected results).

我正在寻找在 php 中进行整数除法的最快方法。例如,5 / 2 应该是 2,6 / 2 应该是 3 等等。如果我只是这样做,在第一种情况下,php 将返回 2.5,我能找到的唯一解决方案是使用intval($my_number/2)- 它没有我想要的那么快(但给出了预期的结果)。

can anyone help me out with this?

谁能帮我解决这个问题?

EDIT:
thanks to all of you for your ideas, i used the script postet by rubber_boots to test some of them with 10000000 iterations, here you can see the results (MAMP on a 3 or 4 year old macbook with 2Ghz intel core 2 duo):

编辑:
感谢大家的想法,我使用了rubber_boots 的脚本 postet 用 10000000 次迭代测试了其中的一些,在这里你可以看到结果(MAMP 在 3 或 4 岁的 macbook 上使用 2Ghz intel core 2 duo) :

start (10000000)
(int)...: 2.26 sec
floor(): 4.36 sec
int_divide(): 2.86 sec
bit-shift: 1.45 sec //note: only works for divisions through powers of 2
intval(): 4.51 sec
round() with PHP_ROUND_HALF_DOWN: 5.48 sec

until now, bit-shift is the fastest way, but i'll leave this question open for a day to see if there are other possibilitys for this...

到目前为止,位移位是最快的方法,但我会将这个问题留待一天,看看是否还有其他可能性......

EDIT2:
updated the results, added round() with PHP_ROUND_HALF_DOWN (thanks to Col._Shrapnel)

EDIT2:
更新结果,用 PHP_ROUND_HALF_DOWN 添加 round() (感谢 Col._Shrapnel)

回答by cletus

Just cast it to an int:

只需将其转换为 int:

$result = (int)(6 / 2);

For whatever reason, it's much faster than intval().

无论出于何种原因,它都比intval().

Edit:I assumeyou are looking for a generalinteger division solution. Bit-shifting is a special case for dividing by (or multiplying by) powers of 2. If that interests you then:

编辑:假设您正在寻找通用整数除法解决方案。位移位是除以(或乘以)2 的幂的特殊情况。如果您对此感兴趣,那么:

a / b^n = a >> n where a, b, n are integers

so:

所以:

a / 2 = a / 2^1 = a >> 1

But two caveats:

但有两个警告:

  1. Many compilers/interpreters will do this for you automatically so there is no point second guessing it;

  2. Unless you're doing this division at least 100,000 times in a singlescript execution don't bother. It's a pointless micro-optimization.

  1. 许多编译器/解释器会自动为你做这件事,所以没有必要再猜测它;

  2. 除非您在单个脚本执行中至少进行 100,000 次这种划分,否则不要打扰。这是一个毫无意义的微优化。

To further elaborate on (2), yes (int)is faster than parseInt()but does it matter? Almost certainly not. Focus on readable code and a good algorithm. This sort of thing is an irrelevant distraction.

进一步详细说明(2),是(int)比快parseInt()但重要吗?几乎可以肯定不是。专注于可读的代码和好的算法。这种事情是无关紧要的分心。

回答by AlexanderMP

if it's division by 2, the fastest way to do it is bit shifting.

如果除以 2,最快的方法是位移。

5>>1 = 2
6>>1 = 3

and so on and so forth. What it does is just shift the bits to the right by 1 bit, thus dividing the number by 2 and losing the rest

等等等等。它所做的只是将位向右移动 1 位,从而将数字除以 2 并丢失其余部分

1110 >> 1 =  111
1011 >> 1 =  101
1011 >> 2 =   10 //division by 4
1011 << 1 =10110 

回答by XzKto

Heh, I don't know how I got into this question as it seems to be from 2010 and this is not really an answer, but as the author seems to collect all ways to divide inegers fast it may help someone here.

嘿,我不知道我是如何进入这个问题的,因为它似乎是从 2010 年开始的,这并不是一个真正的答案,但由于作者似乎收集了所有快速划分 inegers 的方法,它可能对这里的某人有所帮助。

I usually use 0| instead of (int) when I write fast code for myself, because "|" operator have lover precedence then most other operators, so you don't need extra parentheses. Even

我通常使用 0| 当我为自己编写快速代码时,而不是 (int),因为“|” 运算符的优先级高于大多数其他运算符,因此您不需要额外的括号。甚至

$x=0| 0.3+0.7;

will work as expected and it is easily found when you look at code(at least for me) as I just think of "=0|" as special operator "set and cast to int".

将按预期工作,当您查看代码时(至少对我而言)很容易找到,因为我只是想到“=0|” 作为特殊运算符“设置并转换为 int”。

So, to add to your collection (these are simply just other ways to cast to int):

因此,要添加到您的集合中(这些只是转换为 int 的其他方法):

$c=0| $x/$y;

and

$c=$x/$y % PHP_INT_MAX;

回答by rubber boots

Just Test it:

测试一下:

Result (Win32, Core2/E6600):

结果(Win32、Core2/E6600):

 generic division (3000000)
 (int)DIV:       1.74 sec
 intval(DIV):    6.90 sec
 floor(DIV):     6.92 sec
 int_divide():   1.85 sec

 division by 2 (3000000)
 (int)(VAL/2):   1.75 sec
 VAL >> 2:       1.63 sec
 (int)(VAL*0.5): 1.72 sec

Code:

代码:

 ...
 echo "generic division ($N)\n";
 $start = getTime(); for($i=1; $i<$N; $i++) { $c = (int)(($i+1) / $i); }
 printf("(int)DIV:\t %.2f sec\n", getTime()-$start);

 $start = getTime(); for($i=1; $i<$N; $i++) { $c = intval(($i+1) / $i); }
 printf("intval(DIV):\t %.2f sec\n", getTime()-$start);

 $start = getTime(); for($i=1; $i<$N; $i++) { $c = floor(($i+1) / $i); }
 printf("floor(DIV):\t %.2f sec\n", getTime()-$start);

 $start = getTime(); for($i=1; $i<$N; $i++) { $c = ($i - ($i % ($i+1))) / ($i+1); }
 printf("int_divide():\t %.2f sec\n", getTime()-$start);

 echo "division by 2 ($N)\n";
 $start = getTime(); for($i=1; $i<$N; $i++) { $c = (int)(($i+1) / 2.0); }
 printf("(int)(VAL/2):\t %.2f sec\n", getTime()-$start);

 $start = getTime(); for($i=1; $i<$N; $i++) { $c = ($i+1) >> 2; }
 printf("VAL >> 2:\t %.2f sec\n", getTime()-$start);

 $start = getTime(); for($i=1; $i<$N; $i++) { $c = (int)(($i+1)*0.5); }
 printf("(int)(VAL*0.5):\t %.2f sec\n", getTime()-$start);
 ...

Regards

问候

rbo

红包

回答by ghoppe

Only works if $x and $y are integers

仅当 $x 和 $y 是整数时才有效

function int_divide($x, $y) {
    return ($x - ($x % $y)) / $y;
}

回答by Karthik

use round() or ceil() or floor() functions otherwise declare the type before like int()

使用 round() 或 ceil() 或 floor() 函数,否则像 int() 之前声明类型

回答by Your Common Sense

the round() usually used in such a purpose. But I have no idea of it's speed. I have never had millions of calculations in my code. Merely few tenths max.

round() 通常用于这种目的。但我不知道它的速度。我的代码中从未有过数百万次计算。最多只有十分之几。