找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 4214|回复: 0

python的内置排序究竟有多快

11

主题

35

帖子

35

积分

贫民

积分
35
zy1 发表于 2014-9-11 00:04:44 | 显示全部楼层 |阅读模式
本帖最后由 zy1 于 2014-9-12 01:11 编辑

我比较了一下sort和一个自己写的o(n)的程序的运行时间。
惊奇发现sort的速度几乎和直接用python写的o(n)的程序运行时间接近
先上代码
sort的测试代码
import random
import sys
len_test_arr = int(sys.argv[1])
test_arr = [random.random() for i in xrange(len_test_arr)]
len_test_arr.sort()
测试时间的运行指令 time python test.py 10,10表示数组长度,用到了time,只取了user time。


python写的o(n)的程序
import random
import sys


def counting_sort(a):
count = [0]*100
for i in a:
count += 1
return count


len_test_arr = int(sys.argv[1])
test_arr = [random.randint(0, 99) for i in xrange(len_test_arr)]
counting_sort(test_arr)
测试时间的运行指令相同 time python test.py 10,10表示数组长度,只取了user time。


结果
内置sort
数据大小 | 运行时间(s)
10 0.01
100 0.014
1000 0.01
10000 0.013
100000 0.065
1000000 0.769
10000000 10.991


python写的o(n)的程序
数据大小 | 运行时间(s)
10 0.01
100 0.01
1000 0.01
10000 0.02
100000 0.13
1000000 1.1
10000000 12
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表