小编Ima*_*axd的帖子

Python3与Python2列表/生成器范围性能

我有这个简单的函数,它分区列表并返回列表中的索引i,使索引小于i的元素小于list [i],索引大于i的元素更大.

def partition(arr):
    first_high = 0
    pivot = len(arr) - 1
    for i in range(len(arr)):
        if arr[i] < arr[pivot]:
            arr[first_high], arr[i] = arr[i], arr[first_high]
            first_high = first_high + 1

    arr[first_high], arr[pivot] = arr[pivot], arr[first_high]
    return first_high


if __name__ == "__main__":
    arr = [1, 5, 4, 6, 0, 3]
    pivot = partition(arr)
    print(pivot)
Run Code Online (Sandbox Code Playgroud)

OS X上的python 2.7.6的python 3.4运行时间要大得多:

time python3 partition.py
real 0m0.040s
user 0m0.027s
sys  0m0.010s

time python partition.py
real 0m0.031s
user 0m0.018s
sys  0m0.011s
Run Code Online (Sandbox Code Playgroud)

在ubuntu 14.04 /虚拟机上也是如此

python3:

real …
Run Code Online (Sandbox Code Playgroud)

python performance generator python-2.7 python-3.x

5
推荐指数
1
解决办法
7605
查看次数

标签 统计

generator ×1

performance ×1

python ×1

python-2.7 ×1

python-3.x ×1