python in列表list和in集合set耗时对比

# -*- coding: utf-8 -*-
import random
import time


def count_time(fun):
    def warpper(*args):
        s_time = time.time()
        fun(*args)
        e_time = time.time()
        t_time = e_time - s_time
        print('%s耗时:%s' % (fun.__name__, t_time))

    return warpper



@count_time
def in_list(times,size):
    larget_list = list(range(size))
    count = 0
    for i in range(times):
        num = random.randint(0, size * 2)
        if num in larget_list:
            count += 1
    print count


@count_time
def in_set(times,size):
    larget_set = set(range(size))
    count = 0
    for i in range(times):
        num = random.randint(0, size * 2)
        if num in larget_set:
            count += 1
    print count


if __name__ == '__main__':
    times = 10000
    size = 10000
    in_set(times,size)
    in_list(times,size)

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值