comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: For loops vs. matrix operations
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: For loops vs. matrix operations [message #37438 is a reply to message #37437] Wed, 17 December 2003 15:24 Go to previous messageGo to previous message
Wonko[3] is currently offline  Wonko[3]
Messages: 9
Registered: February 2003
Junior Member
greenberg@ucdavis.edu (Jonathan Greenberg) wrote:

> I know some matrix programs perform better if you do straigh matrix math
> vs. a for-next loop -- is idl this way? E.g. is:

> array=intarr(10000)
> for i=0,(10000-1) do begin
> array[i]=array[i]+1
> endfor

> MUCH slower than:

> array=intarr(10000)
> array=array+1

Not only MUCH, but **MUCH** slower, at least.

Even faster is this: aray = temporary( array ) + 1
This avoids duplicating the a variable first, saving time and memory.

But why don't you try it yourself?

IDL> a = intarr( 10000000L )
IDL> t=systime(1) & for i = 0L, 10000000-1 do a[i]=a[i]+1 & print, systime(1)-t
4.2659999
IDL> t=systime(1) & a=a+1 & print, systime(1)-t
0.18999994
IDL> t=systime(1) & a=temporary(a)+1 & print, systime(1)-t
0.040000081

> I'm trying to figure out how much time I should be using rewriting some
> code to optimize the algorithm, which is why I'm asking (the code is more
> complex than above, obviously, but I did notice I could "matricize" some
> of the code in places)...

Matricyzation should always save time, especially if you have small
inner loops. I also think this makes the code more readable and
universal.

Alex
--
Alex Schuster Wonko@wonkology.org PGP Key available
alex@pet.mpin-koeln.mpg.de
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: always widget!
Next Topic: Re: How to make IDL be quiet

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 18:20:08 PDT 2025

Total time taken to generate the page: 0.00457 seconds