Wow, Python much faster than MatLab

Klaas mike.klaas at gmail.com
Mon Jan 1 00:15:07 CET 2007


sturlamolden wrote:

> as well as looping over the data only once. This is one of the main
> reasons why Fortran is better than C++ for scientific computing. I.e.
> instead of
>
> for (i=0; i<n; i++)
>   array1[i] = (array1[i] + array2[i]) * (array3[i] + array4[i]);
>
> one actually gets something like three intermediates and four loops:
>
> tmp1 = malloc(n*sizeof(whatever));
> for (i=0; i<n; i++)
>    tmp1[i] = array1[i] + array2[i];
> tmp2 = malloc(n*sizeof(whatever));
> for (i=0; i<n; i++)
>    tmp2[i] = array3[i] + array4[i];
> tmp3 = malloc(n*sizeof(whatever));
> for (i=0; i<n; i++)
>    tmp3[i] = tmp1[i] + tmp2[i];
> free(tmp1);
> free(tmp2);
> for (i=0; i<n; i++)
>   array1[i]  = tmp3[i];
> free(tmp3);

C/C++ do not allocate extra arrays.  What you posted _might_ bear a
small resemblance to what numpy might produce (if using vectorized
code, not explicit loop code).  This is entirely unrelated to the
reasons why fortran can be faster than c.

-Mike




More information about the Python-list mailing list