python读取redis三种方式效率对比

横轴是读取redis的key的数量,纵轴是时间。绿色是pipline方式,蓝色是单次读取,红色是mget方式读取。此图在相同环境下运行时间对比,包含连接redis和读取时间,有比较意义。

{0: {1000: 0.05, 20000: 0.41, 40000: 0.74}, 1: {1000: 0.23, 20000: 3.93, 40000: 7.73}, 2: {1000: 0.03, 20000: 0.18, 40000: 0.31}}

0 是pipline方式;1是单次get方式;2是mget方式

1000/20000/40000是读取数量,对应value是消耗时间。

python读取redis三种方式效率对比_第1张图片

 

def getCourseShowAll(redis_0, courseIdDict, num):
    pipe = redis_0.pipeline()
    cnt = 0
    for course_id, courseInfo in courseIdDict.items():
        if cnt == num:
            break
        cnt += 1
        pipe.get("course_show_"+str(course_id))
    result = pipe.execute()
    cntResult = len(result)
    return result, cnt, cntResult

def getCourseMGet(redis_0, courseIdDict,num):
    cnt = 0
    listId = []
    for course_id, courseInfo in courseIdDict.items():
        if cnt == num:
            break
        listId.append("course_show_"+str(course_id))
        cnt += 1
    result = redis_0.mget(listId)
    return result, cnt

if __name__ == "__main__":

    start_time = time.clock()
    redis_0 = redis.Redis(host=OaDataSystem.Core.redisAddress['host'],port=OaDataSystem.Core.redisAddress['port'],db=0)
    if sig == 0:
        print sig
        result, allCnt, cntResult = getCourseShowAll(redis_0, courseIdDict,num)
     #   print result, allCnt
    elif sig == 1:
        print sig
        i = 0
        for course_id, courseInfo in courseIdDict.items():
            if i == num:
                break
            package_name = getCourseShow(redis_0, course_id)
            i += 1
        allCnt = i
    elif sig == 2:
        print sig
        result, allCnt = getCourseMGet(redis_0, courseIdDict, num)
      #  print result, allCnt
    end_time = time.clock()
    print 'using time: '+ str( end_time - start_time),  allCnt

 

你可能感兴趣的:(数据库)