zoukankan      html  css  js  c++  java
  • 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))
    
    
    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 ,这样将会大幅度提升处理速度。

    作者:wintest
    本文版权归作者和博客园共有,欢迎转载,但必须在文章页面明显位置给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
  • 相关阅读:
    Compoer的应用
    memcache的简单使用示例
    windows下安装Memcached服务器,PHP的memcache扩展
    jQuery跨域
    git常用基本命令
    Linux常用命令总结
    修改MySQL数据库密码
    Linux配置LNMP环境(三)配置MySQL
    django之多表查询与创建
    django之单表操作
  • 原文地址:https://www.cnblogs.com/wintest/p/15580617.html
Copyright © 2011-2022 走看看