Optimising literals away

Cameron Simpson cs at zip.com.au
Wed Sep 1 00:11:28 CEST 2010


On 31Aug2010 22:53, MRAB <python at mrabarnett.plus.com> wrote:
[...]
| >>>def m(arg):
| >>>if arg& set([1,2,3]):
| >
| >set() is a function call, not a literal. When m is called, who knows
| >what 'set' will be bound to? In Py3, at least, you could write {1,2,3},
| >which is much faster as it avoids creating and deleting a list. On my
| >machine, .35 versus .88 usec. Even then, it must be calculated each time
| >because sets are mutable and could be returned to the calling code.
| >
| There's still the possibility of some optimisation. If the resulting
| set is never stored anywhere (bound to a name, for example) then it
| could be created once. When the expression is evaluated there could be
| a check so see whether 'set' is bound to the built-in class, and, if it
| is, then just use the pre-created set.

Wouldn't you need to repeat that check every time, otherwise "set" may
no longer be the builtin?
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

From sci.physics:
tron at mailzone.com:
  The only problem is, how do you send a message from Earth to Mars
  instantly?  Does anyone have any ideas about where we can start?
John Baez <baez at math.mit.edu:
  Just use a coordinate system in which the point at which the message is
  received has the same t coordinate as the point at which the message was sent.



More information about the Python-list mailing list