Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is taking the mod of a number in python faster with exponents?
    text
    copied!<p>I was trying to optimize a program I'm tinkering with, when I noticed that doing <code>value = i % 65536</code> appeared to be running slower then doing <code>value = i % (2**16)</code>. </p> <p>To test this, I ran the following program:</p> <pre><code>import cProfile import pstats AMOUNT = 100000000 def test1(): for i in xrange(AMOUNT): value = i % 65536 return def test2(): for i in xrange(AMOUNT): value = i % (256**2) return def test3(): for i in xrange(AMOUNT): value = i % (16**4) return def test4(): for i in xrange(AMOUNT): value = i % (4**8) return def test5(): for i in xrange(AMOUNT): value = i % (2**16) return def run_tests(): test1() test2() test3() test4() test5() return if __name__ == '__main__': cProfile.run('run_tests()', 'results') stats = pstats.Stats('results') stats.sort_stats('calls', 'nfl') stats.print_stats() </code></pre> <p>...which produced the following output:</p> <pre><code>Fri May 11 15:11:59 2012 results 8 function calls in 40.473 seconds Ordered by: call count, name/file/line ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} 1 0.000 0.000 40.473 40.473 &lt;string&gt;:1(&lt;module&gt;) 1 0.000 0.000 40.473 40.473 test.py:31(run_tests) 1 10.466 10.466 10.466 10.466 test.py:6(test1) 1 7.475 7.475 7.475 7.475 test.py:11(test2) 1 7.485 7.485 7.485 7.485 test.py:16(test3) 1 7.539 7.539 7.539 7.539 test.py:21(test4) 1 7.508 7.508 7.508 7.508 test.py:26(test5) </code></pre> <p>Using <code>65536</code> was the slowest at 10.466 seconds, while doing <code>256**2</code> was the fastest at 7.475 seconds (with the other possible exponent values falling in between). Granted, this difference in speed is only noticeable given high amounts of repetition, but I'm still curious as to why this occurs.</p> <p>Why is taking the mod of a number by <code>65536</code> slower then taking the mod using exponents? They should evaluate to the same number, and I would have thought that it would take longer for the python interpreter to fully evaluate exponents before taking the mod.</p> <p>By extension, is it generally more efficient to use powers of two in python expressions rather then fully typing the number out? And does this pattern hold true for operations besides modulus or for other numbers besides <code>2</code>?</p> <p>(btw, I'm using Python 2.7.2 (32 bit), and I ran the above on a 64 bit Windows 7 laptop).</p> <p><strong>EDIT:</strong><br> So I tried reversing the order of the functions I call, and now the opposite is true. It looks like whatever the first function is in <code>run_tests</code> will always run a bit slower when using cProfile, which is <em>weird</em>. So, lesson learned, I guess -- profilers are weird :D</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload