can't assign value to array element

Joe Heafner heafnerj at spam.vnet.net
Mon Nov 4 22:05:33 EST 2002


Carl Banks <imbosol at vt.edu> wrote:
> The solution is to rename the scalar y, maybe to yi or something.
> Same thing with t.
>
After about two hours, I finally figured that out. :-)

> 1. Since you're interested in numerical methods, you're probably
>   interested in speed.  You should put the code inside a function,
>   like this:

>   def main():
>       <your code here>
>   main()
>
I've already done that; I just didn't show that here.

>   This is the single easiest thing you can do to speed up code.
>   Variables defined inside a function are faster than those defined
>   at top level.  (Although for some numerical applications, the
>   numerical time dominates and it won't make too much of a
>   difference.)
>
Thanks for the tip. This code is from a very simple RK4 integrator.

> 2. If you intend to do long simulations (more than a few thousand time
>   steps), you might want to use xrange(0,m,1) instead of
>   range(0,m,1).  The reason is that range allocates a large block of
>   memory (probably about m*4 bytes) and fills it with sequential
>   integers (which might involve more allocation), all before the
>   first iteration.  xrange returns integers on-the-fly each
>   iteration.
Great tip! Thanks!





More information about the Python-list mailing list