divmod

Is divmod() faster than using the % and // operators?

狂风中的少年 提交于 2019-12-17 23:09:58
问题 I remember from assembly that integer division instructions yield both the quotient and remainder. So, in python will the built-in divmod() function be better performance-wise than using the % and // operators (suppose of course one needs both the quotient and the remainder)? q, r = divmod(n, d) q, r = (n // d, n % d) 回答1: To measure is to know (all timings on a Macbook Pro 2.8Ghz i7): >>> import sys, timeit >>> sys.version_info sys.version_info(major=2, minor=7, micro=12, releaselevel='final

Is divmod() faster than using the % and // operators?

旧城冷巷雨未停 提交于 2019-11-30 03:01:54
I remember from assembly that integer division instructions yield both the quotient and remainder. So, in python will the built-in divmod() function be better performance-wise than using the % and // operators (suppose of course one needs both the quotient and the remainder)? q, r = divmod(n, d) q, r = (n // d, n % d) To measure is to know (all timings on a Macbook Pro 2.8Ghz i7): >>> import sys, timeit >>> sys.version_info sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0) >>> timeit.timeit('divmod(n, d)', 'n, d = 42, 7') 0.1473848819732666 >>> timeit.timeit('n // d,