Re: How smart is the Python interpreter?



ssecorp wrote:
def str_sort(string):
s = ""
for a in sorted(string):
s+=a
return s


if i instead do:

def str_sort(string):
s = ""
so = sorted(string)
for a in so:
s+=a
return s

will that be faster or the interpreter can figure out that it only has
to do sorted(string) once?

Actually, by replacing sorted() with a function that outputs when it is
called you could have seen that this is only called once in both cases. It
must not be called more than once in fact, consider e.g. the case that it
introduces side effects (like e.g. reading a file).

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

.



Relevant Pages

  • Newbie completely confused
    ... Now reading ... ... The original problem showed up using Python 2.4.3 under linux. ... class SmallClass: ... def input: ...
    (comp.lang.python)
  • Re: Filtering through an external process
    ... or else separate threads for reading and writing. ... > and debugging it, I've always ended up using a temp file instead, ... The time machine lives! ... def encode: ...
    (comp.lang.python)
  • Re: Can I run an operation on an objects attribute when reading?
    ... When I'm reading the item.tags, I'd like to automagically have the value ... def get_tags: ... Item.tags = tags ... return getattr(self._item, attr) ...
    (comp.lang.python)
  • Re: Dynamic use of property() fails
    ... probably doing something very stupid... ... After reading the how-to for ... descriptors at http://users.rcn.com/python/download/Descriptor.htm I ... def delete: raise AttributeError ...
    (comp.lang.python)
  • Passing blocks further down
    ... at first I must say that after reading this newsgroups for some weeks now I'm happy to learn from so many smart and kind people in here. ... I managed to implement the class so far but I am unsure whether I use the right technique to pass the ... def traverse_inorder ...
    (comp.lang.ruby)