论写代码的合理性

只是举出一个小例子,不是为了说明redis怎么用节约时间,而是说明一种编程意识

 

from redis import Redis
from decorator_libs import TimerContextManager

# redis://:yMxsueZD9yx0AkfR@192.168.199.202:6543/7

with TimerContextManager():
    for i in range(10000):  # 180s
        r = Redis(host='192.168.199.202', port=6543, password='yMxsueZD9yx0AkfR', db=8)
        r.lpush('a1', 1)

with TimerContextManager():  # 42秒
    r = Redis(host='192.168.199.202', port=6543, password='yMxsueZD9yx0AkfR', db=8)
    for i in range(10000):
        r.lpush('a2', 1)

with TimerContextManager():  # 0.26秒
    r = Redis(host='192.168.199.202', port=6543, password='yMxsueZD9yx0AkfR', db=8)
    with r.pipeline() as p:
        for i in range(10000):
            p.lpush('a3', 1)
        p.execute()

 

同样是操作1万次,时间差距很大。一定要有性能意识,特别是io的类,怎么用合理,mysql/mongo/es使用同理。

posted @ 2020-08-04 10:00  北风之神0509  阅读(231)  评论(0编辑  收藏  举报