Performance of build-in functions: parseInputs takes quite long, how to improve?
2 views (last 30 days)
Show older comments
When profiling my codes, I've quite often the case that internal parseInputs takes a significant amount of time compared to the actual calculation. That especially the case when the specific amount of data is small and the number of calls of the specific build-in function is large. Here an example:
function testLocalMax()
for i = 1:100000
data = rand(1, 257);
islocal = islocalmax(data);
end
end
Profile results of this function, going down the way to isLocalExtrema, looks like this:
As you see, parsing the input data is more than 20% of the overall runtime, and nothing spectacular is going on therein. Is there anything I can improve on the overall runtime on this scenario? As far as I can see, I cannot access isLocalExtremaArray from users code, right?
0 Comments
Answers (1)
Shubham
on 24 Nov 2023
Hey Thomas,
I understand that the built-in functions use significant time in parsing the inputs as compared to the actual computations being performed. This happens specially in the case when the number of function calls are very high as compared to the computational work being done per call.
As per my understanding, a built-in function offers best time and space complexity, and it is optimised to consider input types and sizes for performing various computations. However, a built-in function offers function overloading, validates all the inputs, provides an option to assign default values to some parameters, etc. This provides consistency and robustness to the functions but also adds a small overhead to it. This overhead is often neglected when the task is computationally heavy. In general, it is better to use built-in functions.
In your case there could be some workarounds to improve the efficiency of the code. For such computationally light tasks, you could write a user defined function (or check for options in the built-in function that improves efficiency). You could also adopt techniques such as vectorization, preallocation and parallelization.
Refer to the below code snippet:
compareLocalMaxFunctions();
function compareLocalMaxFunctions()
% Original function
function testLocalMax()
for i = 1:100000
data = rand(1, 257);
islocal = islocalmax(data);
end
end
function testLocalMaxCustom()
for i = 1:100000
data = rand(1, 257);
islocal = [false, data(2:end-1) > data(1:end-2) & data(2:end-1) > data(3:end), false];
end
end
function testLocalMaxVectorized()
data = rand(100000, 257);
islocal = islocalmax(data, 2);
end
function testLocalMaxPreallocate()
data = zeros(1, 257);
for i = 1:100000
data(:) = rand(1, 257);
islocal = islocalmax(data);
end
end
% Parallel processing
function testLocalMaxParallel()
parfor i = 1:100000
data = rand(1, 257);
islocal = islocalmax(data);
end
end
% Measure runtimes
timeOriginal = timeit(@testLocalMax);
fprintf('Original function time: %f seconds\n', timeOriginal);
timeCustom = timeit(@testLocalMaxCustom);
fprintf('Custom implementation time: %f seconds\n', timeCustom);
timeVectorized = timeit(@testLocalMaxVectorized);
fprintf('Vectorized approach time: %f seconds\n', timeVectorized);
timePreallocate = timeit(@testLocalMaxPreallocate);
fprintf('Preallocation approach time: %f seconds\n', timePreallocate);
tic;
testLocalMaxParallel();
timeParallel = toc;
fprintf('Parallel processing time: %f seconds\n', timeParallel);
end
The output produced is as follows:
Original function time: 3.848763 seconds
Custom implementation time: 0.325053 seconds
Vectorized approach time: 1.146267 seconds
Preallocation approach time: 3.822985 seconds
Parallel processing time: 1.033663 seconds
All approaches reduced the run-time significantly except for preallocation. Preallocation helps in the case when the size of data structure is known beforehand, and it does not change in between execution.
You can also refer to some techniques listed out here: https://www.mathworks.com/help/matlab/matlab_prog/techniques-for-improving-performance.html
Hope this helps!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!