Speed-up for loops
Terry Reedy
tjreedy at udel.edu
Thu Sep 2 18:20:27 EDT 2010
On 9/2/2010 8:55 AM, Tim Wintle wrote:
> On Thu, 2010-09-02 at 12:02 +0200, Michael Kreim wrote:
>> Hi,
>>
>> I was comparing the speed of a simple loop program between Matlab and
>> Python.
>
>> Unfortunately my Python Code was much slower and I do not understand why.
>
> The main reason is that, under the hood, cpython does something like
> this (in psudo-code)
>
> itterator = xrange(imax)
> while 1:
> next_attribute = itterator.next
> try:
> i = next_attribute()
> except:
> break
> a = a + 10
>
> where C (and I'm assuming matlab) does this:
>
> while 1:
> i = i + 1
> if (i> imax):
> break
> a = a + 10
Which is to say, 'for' in python is not the same as 'for' in C/matlab
and the latter is what Michael should use in a fair comparison.
Otherwise, apples and oranges.
--
Terry Jan Reedy
More information about the Python-list
mailing list