【问题标题】:Timing how long it takes to do the in operator计时执行 in 运算符需要多长时间
【发布时间】:2015-06-06 07:15:55
【问题描述】:

这里,del 运算符是定时的:

from timeit import Timer

def build_list(n):
    return list(range(n))  # create list of 1 to n


def build_dict(n): # build dict = { 0:"0", 1:"1", 2:"2", ... n:"n" }
    return {i: str(i) for i in range(n)}  # from last listing in this chapter


def inx(x,n): # do in front, middle, end, and not found
    str(0) in x
    str(n//2) in x
    str(n-1) in x
    str("a") in x # not in it

timeList = Timer(
    "inx(x,n)",
    "from __main__ import n,build_list,inx; x = build_list(n)")

timeDict = Timer(
    "inx(x,n)",
    "from __main__ import n,build_dict,inx; x = build_dict(n)")

# get min of 5 runs of 5
print("N", "\t", "List", "\t", "Dict")
for size in range(1000, 100000+1, 5000):  # sizes to graph for n:
    n = size
    list_secs = timeList.repeat(5,5)
    dict_sect = timeDict.repeat(5,5)
    print(n, "\t", min(list_secs), "\t", min(dict_sect))

这一次,是计算执行 in 运算符而不是 del 运算符所需的时间。哪些代码需要更改和添加?

【问题讨论】:

  • dict 的那些in x 计时有点误导,因为与执行 dict 查找相比,构建每个测试字符串所需的时间很重要。要看到这一点,请从inx(x,n) 的每一行中删除in x。仅调用 inx() 函数和构造字符串的时间大约是进行完整测试的时间的 50%。
  • 嗯,我可以看到,但这与 in 运算符的计时有关吗?
  • 是和不是。 :) 上面的代码当然表明in 在字典上比在相同大小的列表上要快得多。但它报告的时间并不仅仅是只是in 操作计时——这些时间还包括调用inx() 函数本身所花费的时间以及构建4 个字符串所花费的时间。而且这些东西所花费的时间与在字典上执行实际的in 操作所花费的时间大致相同。因此,代码打印的数字不应被视为在列表上执行 in 与在字典上执行之间的相对速度差异的准确指示。
  • 那么你建议我做什么来获得我想要的结果?
  • 没有完美的解决方案,但一般原则是尽量减少您正在计时的代码内完成的任何额外工作,以便完成这些额外工作所花费的时间不会淹没您真正想要衡量的操作。因此,在设置代码中或在调用计时器方法之前,请尽可能多地进行工作。如果您怀疑与要测试的实际操作相比开销很大(例如在这种情况下),您可以创建一个函数来完成额外的工作并对其计时,以便您了解这些东西需要多少时间。

标签: python operator-keyword timing


【解决方案1】:

你需要为 list 和 dict 分别实现 del ,因为 list 需要整数来索引,而你的字典有字符串键。由于我们无法删除列表或字典中不存在的元素,因此我省略了该部分。

from timeit import Timer

def build_list(n):
    return list(range(n))  # create list of 1 to n


def build_dict(n): # build dict = { 0:"0", 1:"1", 2:"2", ... n:"n" }
    return {i: str(i) for i in range(n)}  # from last listing in this chapter


def delx(x, n):  # do in front, middle, end, and not found
    if x is list:
        del x[0]  # deleting first element in list
        del x[(n-1)//2]  # middle element delete n-1 because 0-th element is already deleted
        del x[-1]  # last element delete
    if x is dict:
        del x[str(0)]  # deleting first element in dict
        del x[str((n-1)//2)]  # deleting middle element from dict
        del x[str((n-2)-1)]  # last element delete n-2 because 0-th and middle element is already deleted
        # str("a") in x # not in it

timeList = Timer(
    "delx(x,n)",
    "from __main__ import n,build_list,delx; x = build_list(n)")

timeDict = Timer(
    "delx(x,n)",
    "from __main__ import n,build_dict,delx; x = build_dict(n)")

# get min of 5 runs of 5
print("N", "\t", "List", "\t", "Dict")
for size in range(1000, 100000+1, 5000):  # sizes to graph for n:
    n = size
    list_secs = timeList.repeat(5,5)
    dict_sect = timeDict.repeat(5,5)
    print(n, "\t", min(list_secs), "\t", min(dict_sect))

【讨论】:

  • 似乎运行正常。我认为它与del x有关。虽然不是特别 100% 要做什么。非常感谢你帮助我,k4vin。
猜你喜欢
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 2017-03-06
  • 1970-01-01
  • 2016-07-13
  • 2013-06-17
  • 1970-01-01
相关资源
最近更新 更多