1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
#!/usr/bin/env python
# encoding: utf-8
"""
cache.py
Created by David Farrar on 2012-12-27.
Copyright (c) 2009-2015 Exa Networks. All rights reserved.
License: 3-clause BSD. (See the COPYRIGHT file)
"""
import unittest
import time
from exabgp.util.cache import Cache
from exabgp.vendoring import six
from exabgp.vendoring.six.moves import xrange
class TestCache(unittest.TestCase):
def test_speed(self):
class klass1:
def __init__(self, data):
pass
class klass2(object):
def __init__(self, data):
pass
class klass3:
def __init__(self, data):
self.a = data[0]
self.b = data[1]
self.c = data[2]
self.d = data[3]
self.e = data[4]
class klass4:
def __init__(self, data):
self.a = data[0]
self.b = data[1]
self.c = data[2]
self.d = data[3]
self.e = data[4]
class _kparent1:
def __init__(self, data):
self.a = data[0]
self.b = data[1]
class _kparent2(object):
def __init__(self, data):
self.a = data[0]
self.b = data[1]
class klass5(_kparent1):
def __init__(self, data):
_kparent1.__init__(self, data)
self.c = data[2]
self.d = data[3]
self.e = data[4]
class klass6(_kparent2):
def __init__(self, data):
_kparent2.__init__(self, data)
self.c = data[2]
self.d = data[3]
self.e = data[4]
class klass7(klass6):
pass
class klass8(klass6):
def __init__(self, data):
klass6.__init__(self, data)
self.s = self.a + self.b + self.c + self.d + self.e
class klass9(klass6):
def __init__(self, data):
klass6.__init__(self, data)
self.s1 = self.a + self.b + self.c + self.d + self.e
self.s2 = self.b + self.c + self.d + self.e
self.s3 = self.c + self.d + self.e
self.s4 = self.d + self.e
self.s5 = self.a + self.b + self.c + self.d
self.s6 = self.a + self.b + self.c
self.s7 = self.a + self.b
COUNT = 100000
UNIQUE = 5000
samples = set()
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:"|;<>?,./[]{}-=_+!@£$%^&*()'
from random import choice
while len(samples) != UNIQUE:
samples.add(choice(chars) + choice(chars) + choice(chars) + choice(chars) + choice(chars))
samples = list(samples)
for klass in [klass1, klass2, klass3, klass4, klass5, klass6, klass7, klass8, klass9]:
cache = {}
start = time.time()
for val in xrange(COUNT):
val %= UNIQUE
_ = klass(samples[val])
end = time.time()
time1 = end - start
print(COUNT, 'iterations of', klass.__name__, 'with', UNIQUE, 'uniques classes')
print("time instance %d" % time1)
cache = Cache()
start = time.time()
for val in xrange(COUNT):
val %= UNIQUE
if val in cache:
_ = cache.retrieve(val)
else:
_ = cache.cache(val, klass(samples[val]))
end = time.time()
time2 = end - start
print("time cached %d" % time2)
print("speedup %.3f" % (time1 / time2))
print()
|