importing a package is very slow
3 views (last 30 days)
Show older comments
Say I have two packages:
+pack1
-- func1.m
-- func1_no_import.m
+pack2
Pack1 contains the following code:
function func1()
import('pack2.*');
function func1_no_import()
Then I test how much time it takes to run func1 3 million times:
import('pack1.*')
tic;
for i = 1:3000000
func1;
end
toc;
Elapsed time is 6.679269 seconds.
If you do the same thing in python, it takes less than half a second.
import time
start = time.time()
for i in range(3000000):
import os
end = time.time()
print(end - start)
0.3926999568939209
Also compare it with a control experiment where no import is performed.
import('pack1.*')
tic;
for i = 1:3000000
func1_no_import;
end
toc;
Elapsed time is 0.100767 seconds.
The main thing I don't understand is that since I heard that import is statically analyzed by Matlab (that is, it doesn't matter where you put the import statement in the function. https://stackoverflow.com/questions/37386754/why-can-you-import-a-package-after-using-its-content-in-a-function ). Why couldn't the cache of the function (I know that function contents are cached in Matlab) store the result of the import instead of running it 3 million times?
Answers (0)
See Also
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!