码迷,mamicode.com
首页 > 编程语言 > 详细

python list 线性访问和随机访问比较

时间:2014-10-01 03:07:30      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:style   for   sp   c   on   r   时间   bs   python   

线性访问速度是随机访问的2倍左右

一下是数组长度和两种访问的时间,单位是秒

数组长度 线性 随机
100 0.01 0.01
1000 0.01 0.01
10000 0.015 0.015
100000 0.02 0.03
1000000 0.1 0.2
10000000 1 2

 

比较的时候把产生随机数的时间减掉了,长度是一亿的时候程序直接挂了

线性访问

import random

a = range(10000)

b = 0

for i in xrange(10000):

b = random.randint(0,999)

a[i] = 0

随机访问

import random

a = range(10000)

b = 0

for i in xrange(10000):

b = random.randint(0,999)

a[b] = 0

python list 线性访问和随机访问比较

标签:style   for   sp   c   on   r   时间   bs   python   

原文地址:http://www.cnblogs.com/zy001/p/4002991.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!