[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
speed of n_elements
I have found the n_elements was dramatically slowing down my programs.
Specifically, I had an array of structures (450 elements) that I examine
for length in the code. Structures have 4 fields of fltarr(900) and some
scalar fields, which adds up to a substantial dataset. Obtaining the
number of elements in the array of structures was very slow (0.1 s) and
caused severe performance degradation after I called it too many times(I
probably should have just saved the array size in widget tree State
structure but I never thought that n_elements would slow me down and too
lazy to go back and change it all). However, there is a workaround that
turned out to be simple:
temp = fltarr(1000)
one_record = {ch1:temp, ch2:temp, ch3:temp, ch4:temp, flag:0L, temp:0.0,
press:0.0}
data = replicate(one_record, 450)
temp = n_elements(data) ; Very slow. Temp = 450
temp = n_elements(Data.Flag) ; Very fast. Temp = 450
So, briefly: if you want to get the size of an array of structures,
examine the size of an array of scalar fields (if available) and you
will have 10X5 faster n_elements.
Cheers,
Pavel