ReDim cost?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

ReDim cost?

Post by Tenaja »

Is there any "cost" to using ReDim? If you are making an array to fill with an arbitrary size, is there a disadvantage (i.e heavy overhead) to using ReDim for each new element?

...and with ReDim, why would anyone use a List?

Thanks.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: ReDim cost?

Post by ts-soft »

ReDim is slower, Memory is not immediately freed by windows ...
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: ReDim cost?

Post by rsts »

Tenaja wrote:
...and with ReDim, why would anyone use a List?

Thanks.
Lists and arrays each have their own advantages and disadvantages.

Have you looked at the list commands?
Insert, delete, split. . .
User avatar
PureLeo
Enthusiast
Enthusiast
Posts: 221
Joined: Fri Jan 29, 2010 1:05 pm
Location: Brazil

Re: ReDim cost?

Post by PureLeo »

I think ReDim is slower, but you can access an array element by index
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Re: ReDim cost?

Post by tinman »

I guess you mean you are going to perform something in a loop and possibly call ReDim in each loop to increase the size of the array by 1?

When you call ReDim you possibly perform three operations:

1) Allocate a new array. The array will be allocated a block of memory that may be bigger than the size of each element in the array * the number of elements.
2) Copy data from the old array to the new array.
3) Free the previous array.

These steps are not always performed if the block from the previous allocation is big enough to hold the new array. For example, in some tests I have done (with an array of integers) the array memory was only re-allocated every time the array increased by approximately 269 elements.

So instead of this:

1) Dim (allocate)
2) Use new item
3) ReDim (allocate, copy, free)
4) Use new item
5) ReDim (allocate, copy, free)
6) Use new item
; etc

the program flow was like this:

1) Dim (allocate)
2) Use new item
3) ReDim (keep block, but allow access to more of it)
4) Use new item
5) ReDim (keep block, but allow access to more of it)
6) Use new item
; etc
269) ReDim (allocate, copy, free)
270) Use new item
271) ReDim (keep block, but allow access to more of it)
272) Use new item

There is also the memory overhead. If the array memory does need to be re-allocated then you will need twice as much memory as you think you will need because of the copy (one for the original plus another for the new array).

If you can Dim the array to the correct size to start with then it will remove all the re-allocations and copies so it will be faster. You could also allocate a bigger array than you need and reduce it afterwards, if there is a sensible maximum size. If you cannot know the correct size quickly then you could use a linked list and then copy it back to an array at the end. It will still copy all the elements but it will only be one copy for all elements rather than (possibly) multiple.

But I suppose you should really only worry about this if you know you have a speed/memory problem at the point you are calling ReDim :)
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Post Reply