python - 为什么 python 字典中的字符串键的写入/读取速度比元组慢?

标签 python dictionary key

在尝试优化模仿树结构的程序的速度时(“树”存储在 DICT 中,笛卡尔坐标 x,y 坐标对作为键)我发现将它们的唯一地址存储在字典中作为元组,而不是字符串,导致运行时间大大加快。

我的问题是,如果 Python 针对字典和散列中的字符串键进行了优化,为什么在此示例中使用元组要快得多?执行完全相同的任务时,字符串键似乎要多花 60%。我是否忽略了示例中的一些简单内容?

我引用这个线程作为我的问题的基础(以及其他同样断言字符串更快的线程):Is it always faster to use string as key in a dict?

下面是我用来测试方法和计时的代码:

import time

def writeTuples():
    k = {}
    for x in range(0,500):
        for y in range(0,x):
            k[(x,y)] = "%s,%s"%(x,y)
    return k

def readTuples(k):
    failures = 0
    for x in range(0,500):
        for y in range(0,x):
            if k.get((x,y)) is not None: pass
            else: failures += 1
    return failures

def writeStrings():
    k = {}
    for x in range(0,500):
        for y in range(0,x):
            k["%s,%s"%(x,y)] = "%s,%s"%(x,y)
    return k

def readStrings(k):
    failures = 0
    for x in range(0,500):
        for y in range(0,x):
            if k.get("%s,%s"%(x,y)) is not None: pass
            else: failures += 1
    return failures

def calcTuples():
    clockTimesWrite = []
    clockTimesRead = []
    failCounter = 0
    trials = 100

    st = time.clock()
    for x in range(0,trials):
        startLoop = time.clock()
        k = writeTuples()
        writeTime = time.clock()
        failCounter += readTuples(k)
        readTime = time.clock()
        clockTimesWrite.append(writeTime-startLoop)
        clockTimesRead.append(readTime-writeTime)

    et = time.clock()

    print("The average time to loop with tuple keys is %f, and had %i total failed records"%((et-st)/trials,failCounter))
    print("The average write time is %f, and average read time is %f"%(sum(clockTimesWrite)/trials,sum(clockTimesRead)/trials))
    return None

def calcStrings():
    clockTimesWrite = []
    clockTimesRead = []
    failCounter = 0
    trials = 100

    st = time.clock()
    for x in range(0,trials):
        startLoop = time.clock()
        k = writeStrings()
        writeTime = time.clock()
        failCounter += readStrings(k)
        readTime = time.clock()
        clockTimesWrite.append(writeTime-startLoop)
        clockTimesRead.append(readTime-writeTime)

    et = time.clock()
    print("The average time to loop with string keys is %f, and had %i total failed records"%((et-st)/trials,failCounter))
    print("The average write time is %f, and average read time is %f"%(sum(clockTimesWrite)/trials,sum(clockTimesRead)/trials))
    return None

calcTuples()
calcStrings()

谢谢!

最佳答案

测试没有公平加权(因此存在时间差异)。您在 writeStrings 循环中对 format 的调用次数是在 writeTuples 循环中的两倍,并且在 读取字符串。为了进行更公平的测试,您需要确保:

  • 两个写循环每个内循环只调用一次%
  • readStringsreadTuples 都会在每个内部循环中调用一次或零次调用 %

关于python - 为什么 python 字典中的字符串键的写入/读取速度比元组慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15602107/

相关文章:

python - 将浮点值舍入到区间限制/网格

python - SQLAlchemy-Migrate 或 Alembic 重命名模型时删除数据

python - 为什么pyqt无法识别Qt Designer css样式表的背景颜色?

python - 删除具有最低值的字典条目

java - 有效地迭代多个 Java Map 键集的联合

xaml - 向 ListPickerItem 添加一个键

python - 询问长度为 2 的数组列表是否包含给定长度为 1 的数组时出现 ValueError

C# 结构如 std::set 支持 lower_bound

ruby 使用 array.map(& :methodname) for hash key strings rather than methodname 中的 "&:methodname"快捷方式

MySQL 错误: `query':Ruby 文件上的键 3 (Mysql2::Error) 重复条目 ''