13

Is there a python built-in (or just optimized) function to get the floor division and remainder at the same time in two separate variables?

Example:

a, b = 10 divided by 4

Desired results:

a = 2
b = 2

I need this to be an optimized solution.

Performance results:

First piece of code:

for i in range(10000000):
    a, b = divmod(i, 5)
took 3.99 seconds to run

Second piece of code:

for i in range(10000000):
    a = i // 5
    b = i % 5
took 2.56 seconds to run

Remarks:

Read @casevh answer for a more detailed explanation.

tldr: divmod() works better if numbers are big.

joaoavf
  • 1,343
  • 1
  • 12
  • 25

3 Answers3

24

Use this. this will help you.

a,b = divmod(10,2)

it will return both value

Manoj Jadhav
  • 1,270
  • 1
  • 15
  • 23
  • Am I missing something? Because that just gives you `a=2` and `b=2`, not what OP asked for where `b=0.5`. – Karl Reid Nov 22 '17 at 12:33
  • 1
    Oh, he edited the question to ask for just plain `a % b`. Fair enough. – Karl Reid Nov 22 '17 at 12:35
  • This attends exactly what I need. I will test it for performance. Thank you very much. – joaoavf Nov 22 '17 at 12:35
  • @KarlReid, yes this works great as well. My question was not that good, sorry. – joaoavf Nov 22 '17 at 12:36
  • 1
    What did you write first in your answer? OP edited `a = 2; b = 2` at `2017-11-22 12:33:06Z` and you did answer this at `2017-11-22 12:31:01Z`. Do you use time travel or something? – Elis Byberi Nov 22 '17 at 12:45
10

There is a significant difference in performance if you use larger numbers.

Here is an example using both small and large numbers:

$ py27 -m timeit -s "a=123;b=7" "divmod(a,b)"
10000000 loops, best of 3: 0.0913 usec per loop
$ py27 -m timeit -s "a=123;b=7" "a//b;a%b"
10000000 loops, best of 3: 0.047 usec per loop
$ py27 -m timeit -s "a=123333333333333333333333333333333333333;b=7222222222222222222" "divmod(a,b)"
10000000 loops, best of 3: 0.165 usec per loop
$ py27 -m timeit -s "a=123333333333333333333333333333333333333;b=7222222222222222222" "a//b;a%b"
1000000 loops, best of 3: 0.232 usec per loop

Why the difference?

divmod() requires a function call while // and % are operators. There is additional overhead for a function call relative to operators. So when the cost of the computation is minimal, the overhead of calling a function is much greater than the actual cost of the calculation(s).

For larger numbers, divmod() is faster. divmod() calculates both the quotient and remainder at the same time and returns both of them. The // and % operators each calculate the quotient and remainder but only return one of the results.

divmod() has more overhead but only performs one division. // and % have less overhead but perform two divisions. As long as the overhead is large compared to time to perform division, divmod() will be slower. But once the cost of a division is greater than the overhead, divmod() will be faster.

casevh
  • 11,093
  • 1
  • 24
  • 35
6

I think you are looking for divmod() library function

Here is a link to its docs

Example

>>> a, b = divmod(10, 4)
>>> print a, b
>>> 2 2
Anuj
  • 994
  • 11
  • 21