import timeit
import redis
def clock(func):
def clocked(*args, **kwargs):
t0 = timeit.default_timer()
result = func(*args, **kwargs)
elapsed = timeit.default_timer() - t0
name = func.__name__
arg_str = ', '.join(repr(arg) for arg in args)
print('[%0.8fs] %s(%s) -> %r' % (elapsed, name, arg_str, result))
return result
return clocked
@clock
def add_to_redis(x):
conn = redis.ConnectionPool(host='127.0.0.1', port='6379')
db = redis.Redis(connection_pool=conn)
for i in range(x):
db.zadd('test1', {i: 100})
if __name__ == '__main__':
add_to_redis(100000)
执行5次,分别为18,15,14,15,14