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

Home » Public Forums » archive » Re: More efficient method of appending to arrays when using pointers?
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: More efficient method of appending to arrays when using pointers? [message #74164] Tue, 04 January 2011 14:54 Go to next message
Matt Francis is currently offline  Matt Francis
Messages: 94
Registered: May 2010
Member
On Jan 5, 9:44 am, nata <bernat.puigdomen...@gmail.com> wrote:
> Try this:
>
> temp = [ TEMPORARY(*self.foo),next_array ]
> ptr_free,self.foo
> self.free = ptr_new(temp,/NO_COPY)
>
> When you are creating the new pointer you are duplicating memory.
> The same occurs when you are retrieving the content of *self.foo. Use
> the function TEMPORARY and the keyword NO_COPY, your code will be more
> efficient.
>
> Cheers,
> nata

Thanks Nata. I see from the TEMPORARY docs that it works by using some
scratch space IDL keeps allocated on hand. Do you know if there is an
upper limit on the size of the arrays you use this approach with
before you don't gain any efficiency (or start to be slower than not
using TEMPORARY), for instance because you are using more memory than
IDL keeps on hand for use with TEMPORARY?
Re: More efficient method of appending to arrays when using pointers? [message #74166 is a reply to message #74164] Tue, 04 January 2011 14:44 Go to previous messageGo to next message
natha is currently offline  natha
Messages: 482
Registered: October 2007
Senior Member
Try this:

temp = [ TEMPORARY(*self.foo),next_array ]
ptr_free,self.foo
self.free = ptr_new(temp,/NO_COPY)

When you are creating the new pointer you are duplicating memory.
The same occurs when you are retrieving the content of *self.foo. Use
the function TEMPORARY and the keyword NO_COPY, your code will be more
efficient.

Cheers,
nata
Re: More efficient method of appending to arrays when using pointers? [message #74169 is a reply to message #74166] Tue, 04 January 2011 14:39 Go to previous messageGo to next message
Matt Francis is currently offline  Matt Francis
Messages: 94
Registered: May 2010
Member
On Jan 5, 9:30 am, Gray <grayliketheco...@gmail.com> wrote:
> On Jan 4, 5:01 pm, Matt Francis <mattjamesfran...@gmail.com> wrote:
>
>
>
>> I have some code I've written that looks clunky and I was wondering if
>> there is a more efficient (faster and or using less memory) way to do
>> this.
>
>> I am using a custom object with a member self.foo which will end up
>> being a matrix, built up by appending arrays one at a time as I loop
>> over each step of a process. This update code currently looks like
>> this:
>
>> temp = [ *(self.foo),next_array]
>> ptr_free,self.foo
>> self.free = ptr_new(temp)
>
>> This seems to be a bit wastefull in terms of how many times memory is
>> allocated and deallocated to get the job done. Something simple like
>
>> self.free = ptr_new([*(self.foo),next_array]
>
>> causes a memory leak due to the dangling pointer. I don't see how the
>> TEMPORARY function can be used here without causing a leak.
>
>> Any tips from the pros?
>
> Why mess about with ptr_new and ptr_free?  Unnecessary.
>
> temp = [*self.foo,next_array]
> *self.foo = temp
>
> Or, the minimalist approach:
>
> *self.foo = [*self.foo,next_array]

Hmmm, I didn't expect that would work! Still keep forgetting IDL
'pointers' aren't really pointers and can do funny things (I'm a C++
programmer and this wouldn't work with a 'real' pointer).

Thanks for your help.
Re: More efficient method of appending to arrays when using pointers? [message #74170 is a reply to message #74169] Tue, 04 January 2011 14:30 Go to previous messageGo to next message
Gray is currently offline  Gray
Messages: 253
Registered: February 2010
Senior Member
On Jan 4, 5:01 pm, Matt Francis <mattjamesfran...@gmail.com> wrote:
> I have some code I've written that looks clunky and I was wondering if
> there is a more efficient (faster and or using less memory) way to do
> this.
>
> I am using a custom object with a member self.foo which will end up
> being a matrix, built up by appending arrays one at a time as I loop
> over each step of a process. This update code currently looks like
> this:
>
> temp = [ *(self.foo),next_array]
> ptr_free,self.foo
> self.free = ptr_new(temp)
>
> This seems to be a bit wastefull in terms of how many times memory is
> allocated and deallocated to get the job done. Something simple like
>
> self.free = ptr_new([*(self.foo),next_array]
>
> causes a memory leak due to the dangling pointer. I don't see how the
> TEMPORARY function can be used here without causing a leak.
>
> Any tips from the pros?

Why mess about with ptr_new and ptr_free? Unnecessary.

temp = [*self.foo,next_array]
*self.foo = temp

Or, the minimalist approach:

*self.foo = [*self.foo,next_array]
Re: More efficient method of appending to arrays when using pointers? [message #74258 is a reply to message #74164] Tue, 04 January 2011 19:07 Go to previous message
natha is currently offline  natha
Messages: 482
Registered: October 2007
Senior Member
I don't know but I don't thing so. The use of the TEMPORARY function
allows to save time programming with large amounts of data (in my
experience) I didn't find any contradictions to this.
I think that the use of TEMPORARY is always better. You will lose
efficiency using small data (for example, single scalars or small
arrays).

The simple reason is that you are not duplicating memory. In the case
discussed above, you are not copying the content of your pointer, you
are just retrieving it.

You can do your own tests, for example:

a=PTR_NEW(BYTARR(10000000),/NO_COPY)

tt=SYSTIME(/SEC)
b=*a
PRINT, SYSTIME(/SEC)-tt

tt=SYSTIME(/SEC)
b=TEMPORARY(*a) ;; do not forget that you are losing the content of
your pointer
PRINT, SYSTIME(/SEC)-tt

nata
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: How to extract a scalar from a vector?
Next Topic: Re: Associating GeoTiFF tags with basic Mercator projection parameters?

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

Current Time: Wed Oct 08 15:40:12 PDT 2025

Total time taken to generate the page: 0.00500 seconds