使用装饰器对性能的影响 [英] Performance impact of using decorators
问题描述
我正在使用cherrypy构建一个应用程序,并且已经开始广泛使用
装饰器了。很多暴露的功能看起来像:
@expose
@startTransactrionAndBuildPage
@partOfTabUi(tabId)
@convert(arg1 = int,arg2 = str)
def do_main_page(self,arg1,arg2):
一些代码
我非常喜欢装饰师并且非常喜欢使用它们。我已经准备好了,在python中函数调用很昂贵。在上面的例子中,解释器是否调用了5种不同的函数?
I''m building an application with cherrypy and have started using
decorators quite extensively. A lot of my exposed functions look like:
@expose
@startTransactrionAndBuildPage
@partOfTabUi(tabId)
@convert(arg1=int, arg2=str)
def do_main_page(self, arg1, arg2):
some code
I''ve become really fond of decorators and use them quite a lot. I''ve
also ready that function calls are expensive in python. In the above
example, does the interpreter call 5 different functions?
推荐答案
vinjvinj< vi ******@gmail.com>写道:
vinjvinj <vi******@gmail.com> wrote:
我正在构建一个带有cherrypy的应用程序,并且已经开始广泛使用
装饰器。很多我公开的函数看起来像:
@expose
@startTransactrionAndBuildPage
@partOfTabUi(tabId)
@convert(arg1 = int,arg2 = str)
def do_main_page(self,arg1,arg2):
一些代码
我已经非常喜欢装饰器并且使用它们了很多。我已经准备好了python中的函数调用很昂贵。在上面的示例中,解释器是否调用了5个不同的函数?
I''m building an application with cherrypy and have started using
decorators quite extensively. A lot of my exposed functions look like:
@expose
@startTransactrionAndBuildPage
@partOfTabUi(tabId)
@convert(arg1=int, arg2=str)
def do_main_page(self, arg1, arg2):
some code
I''ve become really fond of decorators and use them quite a lot. I''ve
also ready that function calls are expensive in python. In the above
example, does the interpreter call 5 different functions?
在执行时,可能是6(两个装饰器没有args,加上2
每个w / args);在调用时,它取决于装饰器是什么
做的(如果每个都添加一个包装闭包,例如,那么
确实是5个嵌套调用)。不幸的是,我不太了解
今天的樱桃内部,所以我不知道每个装饰师在做什么
内部。
Alex
At def-execution time, presumably 6 (the two decorators w/o args, plus 2
each for those w/args); at call time, it depends what the decorators are
doing (if each adds exactly one wrapping closure, for example, there
will indeed be 5 nested calls). Unfortunately I do not know much of
today''s cherrypy internals, so I don''t know what each decorator is doing
internally.
Alex
vinjvinj写道:
vinjvinj wrote:
我正在构建一个带有cherrypy的应用程序并已开始使用
装饰者相当广泛。很多我公开的函数看起来像:
@expose
@startTransactrionAndBuildPage
@partOfTabUi(tabId)
@convert(arg1 = int,arg2 = str)
def do_main_page(self,arg1,arg2):
一些代码
我已经非常喜欢装饰器而且使用它们非常多。我已经准备好了python中的函数调用很昂贵。在上面的示例中,解释器是否调用了5个不同的函数?
I''m building an application with cherrypy and have started using
decorators quite extensively. A lot of my exposed functions look like:
@expose
@startTransactrionAndBuildPage
@partOfTabUi(tabId)
@convert(arg1=int, arg2=str)
def do_main_page(self, arg1, arg2):
some code
I''ve become really fond of decorators and use them quite a lot. I''ve
also ready that function calls are expensive in python. In the above
example, does the interpreter call 5 different functions?
如果没有读取代码我就不能确定,但这肯定是在寻找比如
经典的给一个只有锤子的男人看起来像钉子一样
解决方案。我不会告诉你,装饰者不是所有编程问题的答案,因为你已经知道了在你心中:-)
问候
Steve
-
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC / Ltd www.holdenweb.com
Love我喜欢我的博客holdenweb.blogspot.com
Without reading your code I can''t be sure, but this is sure looking like
the classic "to a man with only a hammer all problems look like a nail"
solution. I''m not going to tell you that decorators aren''t the answer to
all programming problems, because you already know that in your heart :-)
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd www.holdenweb.com
Love me, love my blog holdenweb.blogspot.com
>>解。我不会告诉你装饰器不是
>> solution. I''m not going to tell you that decorators aren''t the answer to
所有编程问题的答案,因为你已经知道了,在你心中: -
all programming problems, because you already know that in your heart :-
我当时很害怕。曝光装饰器是唯一一个带有cherrypy的
。其他的是我的,格式如下:
def decorator(func):
def wrapper(self,* args,** kwargs)
一些代码
返回包装器
我会坚持我目前正在做的事情并最终(如果需要的话)
来自性能视角)将三者合并为一个
装饰器。我还需要最终添加一个缓存docrator.Do其他
人有这个问题,尤其是开发Web应用程序。
有多少装饰器,如果有的话,你用吗?
@expose - > cherrypy decorator
@startTransactrionAndBuildPage - >启动数据库事务,在会话中填充用户
。有一些错误处理。将页眉,页脚
和错误消息添加到页面。
@partOfTabUi - >除了顶级导航,我有标签级别
(带动作)在各个页面上导航
@convert - >这将boolean类似''True''或''0'转换为python True,
''231'' - > int
@cache - > (未实现,但会广告)。
我很想听听其他人使用装饰器的经验
用于构建Web应用程序。
I was fearing that. The expose decorator is the only one that comes
with cherrypy. The other ones are mine and are of the format:
def decorator(func):
def wrapper(self, *args, **kwargs)
some code
return wrapper
I''ll stick with what I''m doing currently and eventually (if the need
arises from a performance perspective) merge the three into one
decorator. I''ll also need to eventually add a caching docrator.Do other
people have this problem, especially for developing web applications.
How many decorators, if any, do you use?
@expose -> cherrypy decorator
@startTransactrionAndBuildPage -> starts a db transaction, populates
the user in the session. Does some error handling. Adds header, footer
and error messages to the page.
@partOfTabUi -> besides the top level navigation, I have tab level
(with actions) for navigation on individual pages
@convert -> this converts boolean like ''True'' or ''0'' to python True,
''231'' -> int
@cache -> (not implemented, but will ad it).
I would love to hear other people''s experience with using decorators
for web application building.
这篇关于使用装饰器对性能的影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!