3

So what if we tell a programming language to calculate this: X/1, where X is any number. Do they actually calculate the output or check/ignore 1 and return X?

Furthermore, when coding something like the above is it worth checking if the divider is 1 or is it faster to just let it compute the division anyway?

Thank you


In order to elaborate on this question: Which is faster?

$result = $number / $divisor;

or

$result = $divisor > 1 ? $number / $divisor : $number;
Dan Soap
  • 10,114
  • 1
  • 40
  • 49
czioutas
  • 1,032
  • 2
  • 11
  • 37
  • one is a power of 2, so it should be translated to a right-shift (of zero), which in turn is a no-op. i would guess. – sp2danny Mar 17 '15 at 14:14
  • The compiler / optimizer will get rid of it. If it is a runtime parameter, you still don't want to check, because usually you have seldom 1 as a divisor. Btw, division is usually done by some iterative algorithm. It will converge very fast when using 1. Thus it is not that much of an issue. – usr1234567 Mar 17 '15 at 14:16
  • @sp2danny: Only true when the compiler knows about the value. For user input you'd need a JIT to find that out. – usr1234567 Mar 17 '15 at 14:16
  • 1
    Division is usally done using the DIV CPU instruction, I'd wager – neuhaus Mar 17 '15 at 14:16
  • @neuhaus: And how is the DIV DBPU instruction working internally...? – usr1234567 Mar 17 '15 at 14:18
  • Does it matter as long as it's fast? – neuhaus Mar 17 '15 at 14:20
  • I love that my question got voted down but people comment on it :) apart from that, do we know about PHP/C++ compilers what they will do on that case? in my application its 50/50 that it will hit a divider of 1. – czioutas Mar 17 '15 at 14:23
  • I wouldn't bother optimizing it unless it's a huge performance issue. – neuhaus Mar 17 '15 at 14:33
  • @neuhaus while everything on the desktop has had single-cycle integer divide for fifteen or twenty years, ARMv7 does not necessarily have it. So there are still quite a few hundreds of millions of devices in use with a soft integer divide. But, no, I wouldn't bother hand optimising it either. That's crazy. – Tommy Mar 17 '15 at 14:34
  • Have a look at this http://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array – QuentinUK Mar 17 '15 at 14:52

3 Answers3

2

Most programming languages will probably not check for special divisors (with the exception of 0).

CPUs are very fast at division nowadays so it's not worth checking beforehand. The checks could take longer than the actual division.

neuhaus
  • 3,886
  • 1
  • 10
  • 27
2

Comparing the examples (at least for PHP):

<?php
$divisor = 1;
for ($i=0; $i<100000000; $i ++ ) {
if ($divisor != 1)  
    $a = $i / $divisor;
else
    $a = $i;
}

takes 6.9s on my machine. The other one:

<?php
$divisor = 1;
for ($i=0; $i<100000000; $i ++ ) {

    $a = $i / $divisor;
}

takes only 5.2s

So to answer the final question, which one is faster: just let it compute the division :-)

Dan Soap
  • 10,114
  • 1
  • 40
  • 49
0

Since a check and a jump + the actual division would be more cpu instructions than just doing the division, I'm pretty certain that it will just be done.

If your code is variable / 1 (the 1 being static) the compiler will most likely optimize it away.

Schneeez
  • 101
  • 4