Python在不同对象中使用 in 操作符的查找效率

在Python中 in 操作符可以用于判断某个元素是否存在于当前对象中,而对于不同的Python对象,使用 in 操作符的处理效率是不一样的。

今天我们主要针对 4 种不同的Python数据类型进行学习:list列表、tuple元组、set集合、dict字典。

测试过程

我们用于测试的 4 种Python数据类型,分别为 tmp_list 、tmp_tuple、tmp_set、tmp_dict,测试过程中,它们所包含的元素都是相同的,均通过 random.randint(0, num) 随机生成,但它们的长度均为 num - 3 ,也就是说在 [0, num] 范围内,将有3个整数不在上面的对象中,我们需要把这3个整数找出来。

测试代码如下:

import time
import random


def demo(target, num):
    time1 = time.time()
    res = []
    for i in range(num):
        if i not in target:
            res.append(i)
    time2 = time.time()
    print("结果:{},当前类型:{},耗时:{}".format(res, type(target), time2 - time1))

#学习中遇到问题没人解答?小编创建了一个Python学习交流群:531509025

num = 500
tmp_set = set()
while len(tmp_set) <= num - 3:
    tmp_set.add(random.randint(0, num))
tmp_list = list(tmp_set)
tmp_tuple = tuple(tmp_set)
tmp_dict = {key: "" for key in tmp_set}

demo(tmp_list, num)
demo(tmp_tuple, num)
demo(tmp_set, num)
demo(tmp_dict, num)

当 num = 50 时,得到如下结果:

不包含的整数:[25, 31, 36],当前类型:<class 'list'>,耗时:0.0
不包含的整数:[25, 31, 36],当前类型:<class 'tuple'>,耗时:0.0
不包含的整数:[25, 31, 36],当前类型:<class 'set'>,耗时:0.0
不包含的整数:[25, 31, 36],当前类型:<class 'dict'>,耗时:0.0

当 num = 500 时,得到如下结果:

不包含的整数:[114, 329, 355],当前类型:<class 'list'>,耗时:0.0059354305267333984
不包含的整数:[114, 329, 355],当前类型:<class 'tuple'>,耗时:0.0052182674407958984
不包含的整数:[114, 329, 355],当前类型:<class 'set'>,耗时:0.0
不包含的整数:[114, 329, 355],当前类型:<class 'dict'>,耗时:0.0

当 num = 5000 时,得到如下结果:

不包含的整数:[445, 850, 3547],当前类型:<class 'list'>,耗时:0.3342933654785156
不包含的整数:[445, 850, 3547],当前类型:<class 'tuple'>,耗时:0.39918947219848633
不包含的整数:[445, 850, 3547],当前类型:<class 'set'>,耗时:0.00099945068359375
不包含的整数:[445, 850, 3547],当前类型:<class 'dict'>,耗时:0.0

当 num = 50000 时,得到如下结果:

不包含的整数:[9296, 18652, 32281],当前类型:<class 'list'>,耗时:26.92029118537903
不包含的整数:[9296, 18652, 32281],当前类型:<class 'tuple'>,耗时:25.956974506378174
不包含的整数:[9296, 18652, 32281],当前类型:<class 'set'>,耗时:0.009968996047973633
不包含的整数:[9296, 18652, 32281],当前类型:<class 'dict'>,耗时:0.009973287582397461

当 num = 55000 时,得到如下结果:

不包含的整数:[16086, 33891, 46161],当前类型:<class 'list'>,耗时:52.91718029975891
不包含的整数:[16086, 33891, 46161],当前类型:<class 'tuple'>,耗时:52.84810948371887
不包含的整数:[16086, 33891, 46161],当前类型:<class 'set'>,耗时:0.009554624557495117
不包含的整数:[16086, 33891, 46161],当前类型:<class 'dict'>,耗时:0.007979393005371094

当 num = 75000 时,得到如下结果:

不包含的整数:[23057, 35827, 69232],当前类型:<class 'list'>,耗时:75.57932734489441
不包含的整数:[23057, 35827, 69232],当前类型:<class 'tuple'>,耗时:64.49729013442993
不包含的整数:[23057, 35827, 69232],当前类型:<class 'set'>,耗时:0.005983591079711914
不包含的整数:[23057, 35827, 69232],当前类型:<class 'dict'>,耗时:0.005979776382446289

当 num = 100000 时,得到如下结果:

不包含的整数:[22499, 22800, 29652],当前类型:<class 'list'>,耗时:110.19707798957825
不包含的整数:[22499, 22800, 29652],当前类型:<class 'tuple'>,耗时:109.08251285552979
不包含的整数:[22499, 22800, 29652],当前类型:<class 'set'>,耗时:0.011965036392211914
不包含的整数:[22499, 22800, 29652],当前类型:<class 'dict'>,耗时:0.009937524795532227

结论

通过上面的测试,我们可以看到,总体来说,list、tuple它们使用 in 操作符的查找效率相差不多,set、dict它们使用 in 操作符的查找效率相差不多,但随着查找数据量的增大,list、tuple的处理效率变得越来越慢,而set、dict的处理效率,将远远优于list及tuple。

list列表、tuple元组、set集合、dict字典,使用 in 操作符查找的平均时间复杂度如下:

数据类型使用 in 操作符查找的平均时间复杂度
listO(n)
tupleO(n)
setO(1)
dIctO(1)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值