importing a package is very slow

조회 수: 2 (최근 30일)
covariant_cat
covariant_cat 2018년 6월 7일
편집: covariant_cat 2018년 6월 7일
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?

답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

제품


릴리스

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by