预分配清单 [英] preallocate list

查看:74
本文介绍了预分配清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好


这是预分配整数列表的最佳方式吗?

listName = range(0,length)


非整数怎么样?


我刚刚在上面的新闻组声称预分配有帮助,但是我可能会b / b
与matlab混淆;)


如果我的每行都有一个浮点数的文件,那么将它们读入的最佳方式是什么?一个列表(或其他有序的结构)?


我正在使用readline迭代并附加到列表但它需要很长时间。


Jim

Hi all

Is this the best way to preallocate a list of integers?
listName = range(0,length)

What about non integers?

I''ve just claimed in the newsgroup above that pre-allocating helps but I
might be getting confused with matlab ;)

If I have a file with a floating point number on each line, what is the
best way of reading them into a list (or other ordered structure)?

I was iterating with readline and appending to a list but it is taking ages.

Jim

推荐答案

Jim写道:
Jim wrote:
如果我的每行都有一个浮点数的文件,什么是将它们读入列表(或其他有序结构)的最佳方式?

我正在使用readline进行迭代并附加到列表但是它已经过了
年龄。
If I have a file with a floating point number on each line, what is the
best way of reading them into a list (or other ordered structure)?

I was iterating with readline and appending to a list but it is taking
ages.




也许你应该使用r eadlines(注意s)而不是readline。



Perhaps you should use readlines (notice the s) instead of readline.


2005年4月13日,Jim< jb*@cannedham.ee.ed.ac.uk>写道:
On 4/13/05, Jim <jb*@cannedham.ee.ed.ac.uk> wrote:
大家好

这是预分配整数列表的最佳方法吗?
listName = range(0,length)


0是不必要的;范围(长度)做同样的事情。

非整数怎么样?


arr = [myobject()for i in range(length)]

我刚刚在上面的新闻组中声称预分配有帮助,但我可能会对matlab感到困惑;)

如果我有一个带有每行上的浮点数,将它们读入列表(或其他有序结构)的最佳方法是什么?

我正在使用readline迭代并附加到列表但它是花费很多时间。
Hi all

Is this the best way to preallocate a list of integers?
listName = range(0,length)

the 0 is unnecessary; range(length) does the same thing.
What about non integers?

arr = [myobject() for i in range(length)]
I''ve just claimed in the newsgroup above that pre-allocating helps but I
might be getting confused with matlab ;)

If I have a file with a floating point number on each line, what is the
best way of reading them into a list (or other ordered structure)?

I was iterating with readline and appending to a list but it is taking ages.




我会对你的应用进行分析,看看它是你的附加物,即

年龄,但预分配字符串列表如下:


["这是一个平均长度字符串对于我在范围内(大约长度)]


我的猜测是它没有帮助预先分配,但是时间让我们知道了b $ b。一个测试来支持我的猜测:


导入时间,数学

def test1():

lst = [0 for i in range(100000)]

for i in xrange(100000):

lst [i] = math.sin(i)* i


def test2():

lst = []
$ x $ b for x in xrange(100000):

lst.append(math.sin(i)* i)


t1 = timeit.Timer(''test1()'',''来自__main__ import test1'')

t2 = timeit.Timer(''test2()'',''来自__main__ import test2'')

print" time1:%f" %t1.timeit(100)

print" time2:%f" %t2.timeit(100)

09:09 AM~



I would profile your app to see that it''s your append which is taking
ages, but to preallocate a list of strings would look like:

["This is an average length string" for i in range(approx_length)]

My guess is that it won''t help to preallocate, but time it and let us
know. A test to back my guess:

import timeit, math

def test1():
lst = [0 for i in range(100000)]
for i in xrange(100000):
lst[i] = math.sin(i) * i

def test2():
lst = []
for i in xrange(100000):
lst.append(math.sin(i) * i)

t1 = timeit.Timer(''test1()'', ''from __main__ import test1'')
t2 = timeit.Timer(''test2()'', ''from __main__ import test2'')
print "time1: %f" % t1.timeit(100)
print "time2: %f" % t2.timeit(100)

09:09 AM ~


python test.py

time1:12.435000

time2:12.385000


Peace

Bill Mill

bill.mill at gmail.com
python test.py
time1: 12.435000
time2: 12.385000

Peace
Bill Mill
bill.mill at gmail.com


这篇关于预分配清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆