美文网首页Python
Python在不同对象中使用 in 操作符的查找效率

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

作者: wintests | 来源:发表于2021-11-20 11:49 被阅读0次

前言

在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))


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 操作符查找的平均时间复杂度
list O(n)
tuple O(n)
set O(1)
dIct O(1)

当我们在处理数据量大且需频繁查找元素时,最好使用 set、dict ,这样将会大幅度提升处理速度。

相关文章

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

    前言 在Python中 in 操作符可以用于判断某个元素是否存在于当前对象中,而对于不同的Python对象,使用 ...

  • Python中属性查找的顺序

    1.继承关系的查找 python2中是深度查找python3中是广度查找 2.属性的查找 对象的查找先从对象属性字...

  • in 和 for in

    in 使用in操作符来检查属性在对象中是否存在时,同样会查找对象的整条原型链(无论属性是否可枚举) for in ...

  • Java中的clone

    Java中对象的创建 使用new操作符创建一个对象 使用clone方法复制一个对象 那么这两种方式有什么相同和不同...

  • Python高阶(持续更新中...)

    对象的比较 == 操作符比较对象之间的值是否相等在 Python 中,每个对象的身份标识,都能通过函数 id(ob...

  • PHP经典算法题

    PHP学习之路---算法题 1.使用PHP描述顺序查找和二分查找(也叫做折半查找)算法,顺序查找必须考虑效率,对象...

  • 十六讲 单元格的查找

    入库单的输入,查找,删除与修改 一 查找功能 在VBA中查找主要有三种方法,第一是使用循环查找(在单元格中查找效率...

  • python 深入变量和引用对象

    变量和对象 在《learning python》中的一个观点:变量无类型,对象有类型 在python中,如果要使用...

  • 你想了解Python中的 == 和IS 其他?

    前言 比较对象值时使用 == 等逻辑操作符,比较的是对象的值;比较对象身份时使用 is 和 is not 操作符,...

  • PHP算法

    PHP算法 使用PHP描述顺序查找和二分查找(也叫做折半查找)算法,顺序查找必须考虑效率,对象可以是一个有序数组二...

网友评论

    本文标题:Python在不同对象中使用 in 操作符的查找效率

    本文链接:https://www.haomeiwen.com/subject/qyzttrtx.html