|
| 1 | +__author__ = 'ericvergnaud' |
| 2 | + |
| 3 | +import unittest |
| 4 | +import time |
| 5 | +import uuid |
| 6 | + |
| 7 | +class TestAtomicPerformance(unittest.TestCase): |
| 8 | + |
| 9 | + # test to check that on the current platform, a < b < c is faster than b in xrange(a,c) |
| 10 | + # x in xrange is intensively used in Interval |
| 11 | + def test_in_xrange(self): |
| 12 | + xstart = time.time() |
| 13 | + # check with 20 various range sizes |
| 14 | + for max in xrange(0,1000,50): |
| 15 | + r = xrange(0,max) |
| 16 | + for i in r: |
| 17 | + ok = i in r |
| 18 | + xend = time.time() |
| 19 | + print str((xend-xstart)*1000000) |
| 20 | + ystart = time.time() |
| 21 | + # check with 20 various range sizes |
| 22 | + for max in xrange(0,1000,50): |
| 23 | + r = xrange(0,max) |
| 24 | + for i in r: |
| 25 | + ok = max > i >= 0 |
| 26 | + yend = time.time() |
| 27 | + print str((yend-ystart)*1000000) |
| 28 | + self.assertTrue((yend-ystart)<(xend-xstart)) |
| 29 | + |
| 30 | + # test to check that on the current platform, hashing string tuples is faster than hashing strings |
| 31 | + def test_tuple_hash(self): |
| 32 | + # create an array of random strings |
| 33 | + s = [] |
| 34 | + for i in xrange(0,10000): |
| 35 | + s.append(str(uuid.uuid4())) |
| 36 | + # hash then using string concat |
| 37 | + xstart = time.time() |
| 38 | + for i in xrange(0,9999): |
| 39 | + a = hash(s[i] + s[i+1]) |
| 40 | + for i in xrange(0,9998): |
| 41 | + a = hash(s[i] + s[i+1] + s[i+2]) |
| 42 | + for i in xrange(0,9997): |
| 43 | + a = hash(s[i] + s[i+1] + s[i+2] + s[i+3]) |
| 44 | + xend = time.time() |
| 45 | + print str((xend-xstart)*1000000) |
| 46 | + ystart = time.time() |
| 47 | + # hash then using string tuple |
| 48 | + for i in xrange(0,9999): |
| 49 | + a = hash((s[i],s[i+1])) |
| 50 | + for i in xrange(0,9998): |
| 51 | + a = hash((s[i],s[i+1],s[i+2])) |
| 52 | + for i in xrange(0,9997): |
| 53 | + a = hash((s[i],s[i+1],s[i+2],s[i+3])) |
| 54 | + yend = time.time() |
| 55 | + print str((yend-ystart)*1000000) |
| 56 | + self.assertTrue((yend-ystart)<(xend-xstart)) |
| 57 | + |
| 58 | + |
| 59 | +if __name__ == '__main__': |
| 60 | + unittest.main() |
0 commit comments