Created
April 1, 2014 14:44
-
-
Save h2rd/9915545 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| [pow(2, n) for n in xrange(10000)] | |
| 0.108330011368 sec | |
| [p(2, n) for n in xrange(10000)] | |
| 0.289177894592 sec | |
| [bp(2, n) for n in xrange(10000)] | |
| 19.3486168385 sec | |
| """ | |
| def p(x, n): | |
| i, y = n, 1 | |
| while i > 0: | |
| if i % 2 == 1: | |
| y = y * x | |
| x = x * x | |
| i = i / 2 | |
| return y | |
| def bp(x, n): | |
| a = 1 | |
| for _ in xrange(n): | |
| a = a * x | |
| return a | |
| if __name__ == '__main__': | |
| import time | |
| start = time.time() | |
| [pow(2, n) for n in xrange(10000)] | |
| print("%s sec" % ( time.time() - start )) | |
| start = time.time() | |
| [p(2, n) for n in xrange(10000)] | |
| print("%s sec" % (time.time() - start)) | |
| start = time.time() | |
| [bp(2, n) for n in xrange(10000)] | |
| print("%s sec" % ( time.time() - start )) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment