琐碎的表演问题 [英] Trivial performance questions

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

问题描述

我在书中注意到,如果没有这样的属性,可以通过调用getattr

来引发异常。如果我在任何情况下都需要值

,我最好自己在try语句中使用getattr,或者

是否有一些聪明的实现增强,这使得这个想法不错?


即我应该更喜欢:

if hasattr(self,datum):

datum = getattr(" ; datum")

else:

datum =无

self.datum =无


结束:

尝试:

datum = self.getattr(" datum")

除了:

self .datum =无

datum =无


故意引发错误的概念对于这个蟒蛇来说仍然是陌生的

新手,但我真的很喜欢诱捕设施。我只是担心这些东西的性能影响和内存使用情况,特别是因为

我正在为Zope写作。


而我在这里:检查时性能是否有差异:

数据为无

结束:

datum = =没有


和类似:

如果x是None或y是None:

或:

如果(x,y)中没有:


我很欣赏这些都是极端的琐碎,但我好像在写

几十个,我也可以使用正确的一个,并且可以挤出我能做的表现。


非常感谢,

Christopher Boomer 。

I have noticed in the book of words that hasattr works by calling getattr
and raising an exception if no such attribute exists. If I need the value
in any case, am I better off using getattr within a try statement myself, or
is there some clever implementation enhancement which makes this a bad idea?

i.e. should I prefer:
if hasattr(self,"datum"):
datum=getattr("datum")
else:
datum=None
self.datum=None

over:
try:
datum=self.getattr("datum")
except:
self.datum=None
datum=None

The concept of deliberately raising an error is still foreign to this python
newbie, but I really like the trapping facilities. I just worry about the
performance implications and memory usage of such things, especially since
I''m writing for Zope.

And while I''m here: Is there a difference in performance when checking:
datum is None
over:
datum == None

and similarly:
if x is None or y is None:
or:
if None in (x,y):

I appreciate that these are trivial in the extreme, but I seem to be writing
dozens of them, and I may as well use the right one and squeeze what
performance I can.

Many thanks,
Christopher Boomer.

推荐答案

" Brian Patterson" < bp@computastore.com>写道:
"Brian Patterson" <bp@computastore.com> writes:
我在书中注意到,如果没有这样的属性,那么通过调用
getattr并引发异常来运行。如果我在任何情况下都需要这个值,我最好自己在一个
尝试声明中使用getattr,还是有一些聪明的实现增强使这个想法变得糟糕?

也许我更喜欢:
如果hasattr(self,datum):
datum = getattr(" datum")
else:
datum =没有
self.datum =无

结束:
尝试:
datum = self.getattr(" datum")
除了:


不要这样做,做除了AttributeError:相反。

self.datum =无
datum =无

故意提出错误的概念对于这个python新手来说仍然很陌生,但是我真的很喜欢诱捕设施。我只是担心这些事情的性能影响和内存使用情况,特别是因为我正在为Zope写作。


这些运行速度最快的答案取决于你b $ b期望该属性存在的频率。如果不经常这样,第一种形式

可能会更快,如果不是第二种形式。


然而,还有第三种形式:


datum = getattr(self," datum",None)


这就是你想要的。

虽然我在这里:检查时性能是否有差异:
基准是无
结束:
基准==无


是(我认为...),但你不应该太在意它。

和类似的:
如果x是None或y是None:
或:
如果(x,y)中没有:


通过。如果你真的在乎的时候(我对前者的赌注是更快的话)。

我很欣赏这些在极端情况下是微不足道的,但我似乎是
写了几十个,我也可以使用正确的一个,然后压缩我的表现。
I have noticed in the book of words that hasattr works by calling
getattr and raising an exception if no such attribute exists. If I
need the value in any case, am I better off using getattr within a
try statement myself, or is there some clever implementation
enhancement which makes this a bad idea?

i.e. should I prefer:
if hasattr(self,"datum"):
datum=getattr("datum")
else:
datum=None
self.datum=None

over:
try:
datum=self.getattr("datum")
except:
Don''t do that, do "except AttributeError:" instead.
self.datum=None
datum=None

The concept of deliberately raising an error is still foreign to
this python newbie, but I really like the trapping facilities. I
just worry about the performance implications and memory usage of
such things, especially since I''m writing for Zope.
The answer to which of these runs the fastest depends on how often you
expect the attribute to exist. If it''s not that often, the first form
will probably be quicker, if not the second.

However, there''s a third form:

datum = getattr(self, "datum", None)

which is what you want here.
And while I''m here: Is there a difference in performance when checking:
datum is None
over:
datum == None
Yes (I think...), but you shouldn''t care much about it.
and similarly:
if x is None or y is None:
or:
if None in (x,y):
Pass. Time it if you really care (my bet''s on the former being
quicker).
I appreciate that these are trivial in the extreme, but I seem to be
writing dozens of them, and I may as well use the right one and
squeeze what performance I can.




这是一种无益的态度。毕竟你是用Python编写的!


如果个人资料显示某些代码是热点,那么*然后*只有

那么它是适当的时间担心这种琐碎的表现

涨幅。


干杯,

mwh


-

微软唯一的问题就是他们没有品味。

- 史蒂夫·乔布斯,(来自Nerds_ PBS特别版的_Triumph)

并由Aahz在comp.lang.python上引用



This is an unhelpful attitude. You''re writing in Python after all!

If profile shows some of this code to be a hotspot, *then* and only
then is it an appropriate time to worry about such trivial performance
gains.

Cheers,
mwh

--
The only problem with Microsoft is they just have no taste.
-- Steve Jobs, (From _Triumph of the Nerds_ PBS special)
and quoted by Aahz on comp.lang.python


Michael Hudson写道:
Michael Hudson wrote:

如果个人资料显示这些代码中的一部分是一个热点,*然后*只有
那么它是一个适当的时间来担心这种微不足道的性能上升。

If profile shows some of this code to be a hotspot, *then* and only
then is it an appropriate time to worry about such trivial performance
gains.




并且永远不要忘记包含第二个困扰的标准

担心性能:代码不符合其性能

要求。


即使分析显示你是一个热点(因为它几乎是al如果你实际上*需要*

代码更快,你还在浪费时间。削减程序运行时间的几秒钟

需要一分钟才能运行可能会浪费你的时间

从长远来看,尤其是当你考虑如何很多时候,必须运行

计划以支付优惠的投资以及由此带来的维护成本增加。


请记住,首先使用Python包含一个

隐含的接受,表明性能不是你最关心的问题。


-Peter



And never forget to include the second criterion for bothering to
worry about performance: the code does not meet its performance
requirements.

Even if profiling shows you a hotspot (as it almost always would),
you are still wasting your time if you don''t actually *need* the
code to be faster. Shaving a few seconds of runtime of a program
that takes a minute to run is likely to be a waste of your time
in the long run, especially when you consider how many times the
program will have to be run to pay back the investment in
optimization and the resultant increase in maintenance costs.

And remember that use of Python in the first place includes an
implicit acceptance that performance is not your biggest concern.

-Peter


" Brian Patterson" < bp@computastore.com>写在

news:bm ********** @ titan.btinternet.com:
"Brian Patterson" <bp@computastore.com> wrote in
news:bm**********@titan.btinternet.com:
我在书中注意到了hasattr的工作原理是调用
getattr并在没有这样的属性时引发异常。如果我在任何情况下都需要这个值,我最好自己在一个尝试
声明中使用getattr,还是有一些聪明的实现增强
这使得这个想法不错?


如果您要内联代码,但是您在多个地方检查了属性

的存在,那么您自然会提取重复的

代码输出到您从每个位置调用的单个函数。

存在''hasattr''意味着可能重复的代码已经被提取到支持函数中,所以你应该在适当的时候使用它*

可缩短您的代码更容易阅读。
也许我更喜欢:
如果hasattr(self,datum):
datum = getattr(" datum")
else:
datum =无
self.datum =无

结束:
尝试:
datum = self.getattr(" datum")
除外:
self.datum =无
datum =无
I have noticed in the book of words that hasattr works by calling
getattr and raising an exception if no such attribute exists. If I
need the value in any case, am I better off using getattr within a try
statement myself, or is there some clever implementation enhancement
which makes this a bad idea?
If you were to inline the code, but you check the existence of an attribute
in more than one place, then you would naturally extract the duplicated
code out into a single function which you call from each location. The
presence of ''hasattr'' means the potentially duplicated code is already
extracted into a support function, so you should use it *where appropriate*
to make your code shorter & easier to read.
i.e. should I prefer:
if hasattr(self,"datum"):
datum=getattr("datum")
else:
datum=None
self.datum=None

over:
try:
datum=self.getattr("datum")
except:
self.datum=None
datum=None



可能你更喜欢:


datum = getattr(self ,datum,无


虽然这没有设置self.datum的副作用,如果它是

未设置。或者你可以每次设置self.datum:


self.datum = datum = getattr(self," datum",None)


-

Duncan Booth du****@rcp.co.uk

int month(char * p){return(124864 /((p [0] + p [1] -p [2]& 0x1f)+1)%12)[" \ 5 \\\x8 \ 3"

" \\\\\\\\\\\\ /谁说我的代码模糊不清?


Probably you should prefer:

datum = getattr(self, "datum", None)

although this doesn''t have the side effect of setting self.datum if it was
unset. Alternatively you could set self.datum every time with:

self.datum = datum = getattr(self, "datum", None)

--
Duncan Booth du****@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?


这篇关于琐碎的表演问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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