哪个更有效? [英] Which is More Efficient?
问题描述
我有一个程序占用了大量的CPU,并希望尽可能高效地使用它来实现它。那么
中的哪一个会更有效率,知道l是一个列表并且大小是
a数?
l = l [:size]
del l [size:]
如果它有所不同,列表中的所有内容都是可变的。
I have a program that uses up a lot of CPU and want to make it is
efficient as possible with what I have to work with it. So which of the
following would be more efficient, knowing that l is a list and size is
a number?
l=l[:size]
del l[size:]
If it makes a difference, everything in the list is mutable.
推荐答案
测量并找出答案。听起来像是对你的时间的一点投资
学习如何衡量表现可能会给你带来好处。
Measure it and find out. Sounds like a little investment in your time
learning how to measure performance may pay dividends for you.
" Dustan" <都********** @ gmail.com>写道:
"Dustan" <Du**********@gmail.com> writes:
我有一个耗尽了大量CPU的程序,并希望尽可能高效地利用它来处理它。
描述您的程序并找到最慢的精确部件。
最慢。在此之前尝试优化是浪费你的时间。
因此,知道l是列表并且大小是数字,以下哪项会更有效?
l = l [:size]
del l [size:]
I have a program that uses up a lot of CPU and want to make it is
efficient as possible with what I have to work with it.
Profile your program and find the precise parts that are the
slowest. Attempting to optimise before that is a waste of your time.
So which of the following would be more efficient, knowing that l is
a list and size is a number?
l=l[:size]
del l[size:]
当你描述它时,哪一个在你的程序中效率更高
表现?
< URL:http://docs.python.org/lib/profile.html>
-
\制定社会政治,你可以信任谁,为什么?
` \,非常字面意思,非常真实我们大脑的很大一部分已经演变成了。(b)b _o__)。 - 道格拉斯亚当斯|
Ben Finney
Which one is more efficient in your program, when you profile its
performance?
<URL:http://docs.python.org/lib/profile.html>
--
\ "Working out the social politics of who you can trust and why |
`\ is, quite literally, what a very large part of our brain has |
_o__) evolved to do." -- Douglas Adams |
Ben Finney
Dustan写道:
Dustan wrote:
I有一个程序,耗尽了大量的CPU,并希望尽可能高效,我必须使用它。那么
以下哪一个会更有效率,知道l是一个列表并且大小是一个数字?
l = l [:size]
del l [size:]
既然你有这个程序,那么测试
两个替代品应该不难吗?
(理论上,del在大多数情况下应该更快,因为它避免了创建另一个对象的
。但唯一可以确定的方法是
来试试吧。
如果它有所不同,列表中的所有内容都是可变的。
I have a program that uses up a lot of CPU and want to make it is
efficient as possible with what I have to work with it. So which of the
following would be more efficient, knowing that l is a list and size is
a number?
l=l[:size]
del l[size:]
since you have the program, it shouldn''t that hard to test the
two alternatives, should it ?
(in theory, del should be faster in most cases, since it avoids
creating another object. but the only way to tell for sure is
to try it out).
If it makes a difference, everything in the list is mutable.
唯一的区别是Python中的可变和不可变对象
是可变对象具有允许您修改对象的方法
内容,而不可变对象没有这样的方法。
< / F>
the only difference between mutable and immutable objects in Python
is that mutable objects have methods that let you modify the object
contents, while immutable objects don''t have such methods.
</F>
这篇关于哪个更有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!