Created
November 30, 2012 20:36
-
-
Save mbrezu/4178424 to your computer and use it in GitHub Desktop.
A dumb microbenchmark
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
| def breakNumber(n): | |
| return [(n / 1000) % 10, | |
| (n / 100) % 10, | |
| (n / 10) % 10, | |
| n % 10] | |
| def allDifferent(bn): | |
| def iter(num, idx): | |
| if idx == 4: return False | |
| else: return num == bn[idx] or iter(num, idx + 1) | |
| return not (iter(bn[0], 1) or iter(bn[1], 2) or iter(bn[2], 3)) | |
| def allDifferent2(bn): | |
| return bn[0] != bn[1] and bn[0] != bn[2] and bn[0] != bn[3] \ | |
| and bn[1] != bn[2] and bn[1] != bn[3] \ | |
| and bn[2] != bn[3] | |
| def generateOptions(): | |
| result = [] | |
| for n in xrange(10000): | |
| bn = breakNumber(n) | |
| if allDifferent2(bn): result.append(bn) | |
| return result | |
| import time | |
| def timeit(): | |
| t1 = time.time() | |
| opts = generateOptions() | |
| t2 = time.time() | |
| print opts[:10] | |
| print t2 - t1 | |
| timeit() | |
| timeit() | |
| timeit() | |
| timeit() | |
| timeit() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment