假设我的列表是关于1,000,000
条目的。要访问一个项目,时间将是O(500,000)
,这对我来说似乎很长。
当我将列表拆分为多个列表时会发生什么?让我们看一个例子:
将列表分成 10 个部分,我将有一个列表如下:
splitted_list = [
[list with 100,000 entries],
[list with 100,000 entries],
[list with 100,000 entries],
[list with 100,000 entries],
[list with 100,000 entries],
[list with 100,000 entries],
[list with 100,000 entries],
[list with 100,000 entries],
[list with 100,000 entries],
[list with 100,000 entries]
]
访问一个项目的时间将是O(5) + O(50,000) = O(50,005)
大约 1000% 的加速!
当拆分关于它的根的原始列表时,1000
在这种情况下,这将给我们一个包含 1000 个列表的列表,其中包含另外 1000 个条目。
splitted_list = [
[list with 1000 entries],
[list with 1000 entries],
[list with 1000 entries],
[list with 1000 entries],
...
]
现在看一下访问项目的时间:
O(500) + O(500) = O(1000)
O(1000) < O(50,005) < O(500,000)
这是1000倍左右的最佳加速!难以置信,我想,所以我的问题是: