Which one is better w.r.t. memory allocation but also w.r.t. speed:

## 1.1 ##

def forloop(a,b):

        for idx in range(a,b):
                ## ..
                ## do something
                ## ..

## 1.2 ##

def whileloop(a,b):

        idx = a
        while idx < b:
                ## ..
                ## do something
                ## ..
                idx += 1

#########

I mean what I really would like is to have something C++ - like "for 
(int idx = a; idx < b; i++) { .. }" where no internal vector or 
something like that is allocated but only a few op's on registers are 
performed; in the whileloop in python the picture is roughly the same 
behind the scenes I guess.. but what about in the forloop? Is python 
smart enough not to generate an internal vector as it does usually for 
lists or do I have to apply some modifications (xrange..)? What happens 
further if I do the following:

## 2.1 ##

def forloop(a,b,lst):

        for idx in lst.sort():
                ## ..
                ## do something
                ## ..

#######

Is the sorting function called in every iteration just to detect that 
(after the first sorting) it is useless comming back to the loop or does 
this happen:

## 2.2 ##

def forloop(a,b,lst):

        lst.sort()
        for idx in lst:
                ## ..
                ## do something
                ## ..

#######
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to