【问题标题】:I still don't understand the point of memoryview我还是不明白memoryview的观点
【发布时间】:2018-09-29 22:27:09
【问题描述】:

我通读了问题和答案或What exactly is the point of memoryview in Python。我还是不明白。

答案中的示例起初似乎合乎逻辑,但是当我构建第三个测试用例时,我按索引扫描bytesobject,它与memoryview一样快。

import time


# Scan through a bytes object by slicing
for n in (100000, 200000, 300000, 400000):
    data = b'x' * n
    start = time.time()
    b = data
    while b:
        b = b[1:]
    print('bytes sliced  ', n, time.time() - start)

# Scan through a bytes object with memoryview
for n in (100000, 200000, 300000, 400000):
    data = b'x' * n
    start = time.time()
    b = memoryview(data)
    while b:
        b = b[1:]
    print('memoryview    ', n, time.time() - start)

# Scan through a bytes object by index
for n in (100000, 200000, 300000, 400000):
    data = b'x' * n
    start = time.time()
    b = data
    for i in range(n):
        b = b[i+1:]
    print('bytes indexed ', n, time.time() - start)

输出:

bytes sliced   100000 0.16396498680114746
bytes sliced   200000 0.6180000305175781
bytes sliced   300000 1.541727066040039
bytes sliced   400000 2.8526365756988525
memoryview     100000 0.02300119400024414
memoryview     200000 0.04699897766113281
memoryview     300000 0.0709981918334961
memoryview     400000 0.0950019359588623
bytes indexed  100000 0.027998924255371094
bytes indexed  200000 0.05700063705444336
bytes indexed  300000 0.08800172805786133
bytes indexed  400000 0.1179966926574707

其中一个论点是,您可以简单地将 memoryview 对象传递给struct.unpack。但是你绝对可以对字节对象做同样的事情。据我了解,归结为 memoryview 最终也必须复制切片。

如果你不做愚蠢的事情,仅仅坚持字节似乎要简单得多。

【问题讨论】:

    标签: python python-3.x memoryview


    【解决方案1】:

    你的前两个基准基本上从左边蚕食一个字节,直到什么都没有。

    对于bytes的例子,这会做N个拷贝,对于memoryview来说从来没有拷贝,只是调整视图的长度

    你的最后一个例子完全不相似,你不是蚕食一个字节,而是蚕食越来越多的字节(b[1:]b[2:]b[3:])——最终字符串用完了,你正在切割一个空字符串(更准确地说是i * (i + 1) / 2 > n)。例如,对于 100,000 字节序列,您在 446 次迭代后执行 noops。

    【讨论】:

    • 是的,关键是b = b[i+1:]bi都发生了变化。
    猜你喜欢
    • 2011-06-26
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    • 2014-08-19
    相关资源
    最近更新 更多