Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

integer division in php

Tags:

I'm looking for the fastest way to do an integer division in php. for example, 5 / 2 should 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) - which isn't as fast as I want it to be (but gives the expected results).

How can I do this?

EDIT:
Thanks to all of you for your ideas, I used the script posted 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):

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 possibilities for this...

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

like image 271
oezi Avatar asked Apr 20 '10 08:04

oezi


People also ask

What is the division of integer?

Division of integers means equal grouping or dividing an integer into a specific number of groups. For example, -6 ÷ 2 means dividing -6 into 2 equal parts, which results in -3.

How do you convert int to division?

use round() function to get integer rounded value. @ByScripts -8/3 = -3 is just as valid a way for integer division to work as -8/3 = -2. The former is "truncation towards negative infinity", and the latter is "truncation towards zero". Both have advantages, and different languages choose different behaviours.

How does PHP calculate quotient?

PHP intdiv() Function. The intdiv() is PHP mathematical function of PHP, which is used to calculate integer division. It returns the integer quotient of the division of dividend by divisor.

How does PHP calculate remainder?

The fmod() function returns the remainder (modulo) of x/y.


2 Answers

Just cast it to an int:

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

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

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

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 single script execution don't bother. It's a pointless micro-optimization.

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.

like image 132
cletus Avatar answered Oct 15 '22 20:10

cletus


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

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

1110 >> 1 =  111 1011 >> 1 =  101 1011 >> 2 =   10 //division by 4 1011 << 1 =10110  
like image 33
Alex Avatar answered Oct 15 '22 20:10

Alex