|
|
Subscribe / Log in / New account

A public relations problem

A public relations problem

Posted Jul 17, 2015 11:51 UTC (Fri) by arvidma (guest, #6353)
In reply to: A public relations problem by Kwi
Parent article: A better story for multi-core Python

I had no idea about the difference in cost between x.y() and y(), I assumed that that type of thing would be optimized away by the interpreter.

Thanks for a very informative response!


to post comments

A public relations problem

Posted Jul 17, 2015 15:51 UTC (Fri) by Kwi (subscriber, #59584) [Link]

I should clarify that 99% of the time, the performance hit is insignificant, but in a tight spot, one may want to replace while ...: x.y() by:

z = x.y
while ...: z()

This saves a lookup of the y attribute on every iteration, in favor of a much faster local variable access (assuming this is in a function body).

The Python interpreter can't do this optimization automatically, because it'd change the semantics if one thread assigned to x.y while another was in the loop. It's just one example of the performance difficulties imposed by a highly dynamic language like Python (which doesn't have C's volatile keyword).

But again, 99% of the time, you care more about the language than the performance. So don't go "optimize" every bit of Python code like this. :-)


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds