Questions tagged [pre-allocation]

Pre-allocation (or preallocation) refers to the allocation of resources that are not immediately necessary.

Pre-allocation (or preallocation) is the allocation of resources before they are strictly required, with the expectation that they will be necessary in the near future. The purpose is usually efficiency.

The term is used a lot in the MATLAB community, but occurs in many other contexts as well. In MATLAB, preallocation refers to the allocation of an array before a loop that will fill the array, as opposed to growing the array repeatedly within the loop.

42 questions
7
votes
1 answer

Preallocating arrays in Matlab?

I am using a simple for loop to crop a large amount of images and then storing them in a cell array. I keep getting the message: The variable croppedSag appears to change size on every loop iteration. Consider preallocating for speed. I have seen…
Ben Fossen
  • 997
  • 6
  • 22
  • 48
5
votes
3 answers

How can I preallocate a non-numeric vector in MATLAB?

I've often found myself doing something like this: unprocessedData = fetchData(); % returns a vector of structs or objects processedData = []; % will be full of structs or objects for dataIdx = 1 : length(unprocessedData) …
Dan Vinton
  • 26,401
  • 9
  • 37
  • 79
5
votes
0 answers

Does MATLAB's implicit broadcasting optimise based on surrounding code?

A question asked today gave a surprising result with respect to implicit array creation: array1 = 5*rand(496736,1); array2 = 25*rand(9286,1); output = zeros(numel(array1), numel(array2)); % Requires 34GB RAM output = zeros(numel(array1),…
Adriaan
  • 17,741
  • 7
  • 42
  • 75
4
votes
2 answers

Why can an empty array have a non-null dimension?

If we write, for example: x = int8.empty(0,5) whos x outputs: % Name Size Bytes Class Attributes % x 0x5 0 int8 Thus, we obtain a 0x5 empty array of class int8. What is the purpose of preallocating an empty array…
obchardon
  • 10,614
  • 1
  • 17
  • 33
4
votes
1 answer

Windows (ReFS,NTFS) file preallocation hint

Assume I have multiple processes writing large files (20gb+). Each process is writing its own file and assume that the process writes x mb at a time, then does some processing and writes x mb again, etc.. What happens is that this write pattern…
Jaka
  • 1,205
  • 12
  • 19
4
votes
2 answers

python array initialisation (preallocation) with nans

I want to initialise an array that will hold some data. I have created a random matrix (using np.empty) and then multiplied it by np.nan. Is there anything wrong with that? Or is there a better practice that I should stick to? To further explain my…
krg
  • 317
  • 3
  • 11
3
votes
2 answers

Efficiently store an N-dimensional array of mostly zeros in Matlab

I implemented a finite differences algorithm to solve a PDE. The grid is a structured 2D domain of size [Nx, Nz], solved Nt times. I pre-allocate the object containing all solutions: sol = zeros(Nx, Nz, Nt, 'single') ; This becomes very easily too…
Guido
  • 59
  • 4
3
votes
1 answer

pre-allocation of records using count

I've read that pre-allocation of a record can improve the performance, which should be beneficial especially when handling many records of a time series dataset. updateRefLog = function(_ref,year,month,day){ var id = _ref,"|"+year+"|"+month; …
Daniel
  • 34,125
  • 17
  • 102
  • 150
2
votes
0 answers

pre-allocation vs re-allocation in Mac and Windows

I run into an issue while trying to understand the difference (in terms of computational time) between re-allocate a structure each time it is needed, against allocate a priori and then re-fill (a sort of resetting to default values) the…
2
votes
1 answer

Does preallocation of R list improve loop run time? how?

I am running a simulation in R, in which the outputs should be stored in numeric vectors in a variable of the type list. However, I am wondering why when I preallocated the list with numeric vectors, the computational time remains the same instead…
Abbas
  • 807
  • 7
  • 14
2
votes
1 answer

Pre-allocate logical array with unassigned elements (not true or false)

I'm looking for the most efficient method of pre-allocating a logical array in MATLAB without specifying true or false at the time of pre-allocation. When pre-allocating e.g. a 1×5 numeric array I can use nan(1,5). To my mind, this is better than…
CaptainProg
  • 5,610
  • 23
  • 71
  • 116
2
votes
1 answer

Preallocation of array sizes in Modelica mos script

I am writing a mos script in Dymola in which I am dynamically computing array elements inside for loops. A lot of information gets printed on the command window. every time it prints Redeclaring variable: Real traj_phie [34, 1002]; Redeclaring…
2
votes
2 answers

Pre-allocation of memory in custom R function to improve performance (using dplyr)

EDIT: as I am not at all familiar with data.table, does anyone have any ideas for other solutions besides switching to data.table? Thanks a ton! I have quite a big data set, which contains the startdates and enddates of different types of incidents…
2
votes
1 answer

Can you preallocate an array of random size?

The essential part of the code in question can be distilled into: list=rand(1,x); % where x is some arbitrarily large integer hitlist=[]; for n=1:1:x if rand(1) < list(n) hitlist=[hitlist n]; end end list(hitlist)=[]; This program…
Gage
  • 21
  • 4
2
votes
1 answer

I want advice about how to optimize my code. It takes too long for execution

I wrote a MATLAB code for finding seismic signal (ex. P wave) from SAC(seismic) file (which is read via another code). This algorithm is called STA/LTA trigger algorithm (actually not that important for my question) Important thing is that actually…
Senna
  • 149
  • 1
  • 1
  • 10
1
2 3