【问题标题】:why python process a sorted list cost more time than a unsorted list为什么python处理排序列表比未排序列表花费更多时间
【发布时间】:2018-08-30 04:37:37
【问题描述】:

例子:

import cProfile, random, copy
def foo(lIn): return [i*i for i in lIn]
lIn = [random.random() for i in range(1000000)]
lIn1 = copy.copy(lIn)
lIn2 = sorted(lIn1)
cProfile.run('foo(lIn)')
cProfile.run('foo(lIn2)')

结果:

0.075 秒内调用 3 个函数

排序者:标准名称

ncalls tottime percall cumtime percall filename:lineno(function) 1 0.005 0.005 0.075 0.075 :1() 1 0.070 0.070 0.070 0.070 测试.py:716(foo) 1 0.000 0.000 0.000 0.000 {方法'禁用''_lsprof.Profiler'对象}

3 个函数调用在 0.143 秒内

排序者:标准名称

ncalls tottime percall cumtime percall filename:lineno(function) 1 0.006 0.006 0.143 0.143 :1() 1 0.137 0.137 0.137 0.137 测试.py:716(foo) 1 0.000 0.000 0.000 0.000 {方法'禁用''_lsprof.Profiler'对象}

【问题讨论】:

  • 这没有意义
  • 它似乎与排序没有任何关系。你可以用random.shuffle(lIn1) 代替 sort 和cProfile.run('foo(lIn1)'),你会得到同样的结果。
  • 是什么导致处理时间加倍?
  • 也许第一个列表还在缓存中?而且您在第一次测试调用中使用的是lIn,而不是lIn1

标签: python


【解决方案1】:

还没有真的答案,但评论边距对于这个来说有点太小了。

由于random.shuffle() 会产生相同的结果,我决定实现我自己的shuffle 函数并改变我洗牌的次数。 (在下面的例子中,它是xrange300000的参数。

def my_shuffle(array):
    for _ in xrange(300000):
        rand1 = random.randint(0, 999999)
        rand2 = random.randint(0, 999999)
        array[rand1], array[rand2] = array[rand2], array[rand1]

其他代码几乎没有修改:

import cProfile, random, copy
def foo(lIn): return [i*i for i in lIn]
lIn = [random.random()*100000 for i in range(1000000)]
lIn1 = copy.copy(lIn)
my_shuffle(lIn1)
cProfile.run('foo(lIn)')
cProfile.run('foo(lIn1)')

第二次cProfile 得到的结果取决于我洗牌的次数:

10000 0.062
100000 0.082
200000 0.099
400000 0.122
800000 0.137
8000000 0.141
10000000 0.141
100000000 0.248

看起来你把数组弄得越乱,操作就越长,直到某个点。 (我不知道最后的结果。花了这么长时间,我在后台做了一些简单的其他事情,并不想重试。)

【讨论】:

    猜你喜欢
    • 2015-09-13
    • 2017-03-11
    • 2018-08-18
    • 2014-02-25
    • 2012-08-23
    • 2012-06-28
    相关资源
    最近更新 更多