Re: How smart is the Python interpreter?
- From: Ulrich Eckhardt <eckhardt@xxxxxxxxxxxxxx>
- Date: Thu, 31 Jul 2008 13:30:21 +0200
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
.
- References:
- How smart is the Python interpreter?
- From: ssecorp
- How smart is the Python interpreter?
- Prev by Date: Re: Difference between type and class
- Next by Date: Re: Difference between type and class
- Previous by thread: How smart is the Python interpreter?
- Next by thread: Re: How smart is the Python interpreter?
- Index(es):
Relevant Pages
|