Performance impact of using decorators

Peter Otten __peter__ at web.de
Sat Mar 11 03:39:48 EST 2006


vinjvinj wrote:

> 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?

A typical function calls a few other functions already, so three extra
function calls (I suppose expose just sets an attribute) shouldn't matter
much. You shouldn't even start rewriting your code unless you have
identified do_main_page() as a performance bottleneck. In a web app,
candidates would be functions that are called hundred or thousand times per
rendered page rather than once. Does do_main_page() render a complete page?
Forget optimizing three function calls away. You will see no effect.

Peter



More information about the Python-list mailing list