필터 지우기
필터 지우기

Methods in Making a Simple Program More Efficient

조회 수: 2 (최근 30일)
David C
David C 2013년 5월 30일
Hi,
I would like to speed up the execution of my program. It's abbreviated as follows:
Definition of myScript.m is as follows:
%start of myScript.m
...
for m=1:1000
myFunction(m)
end
...
%end of myScript.m
Definition of myFunction(m):
%start of myFunction.m
function output=myFunction(m)
... %code that uses m
internalVar=takeALongTimeFunction() %does not use m
end
%end of myFunction.m
The function takeALongTimeFunction() does not have an input argument, takes a long time to execute, and returns the same result for each call to myFunction(m). How can I speed up the execution of the entire program?
Thanks,
David

채택된 답변

per isakson
per isakson 2013년 5월 30일
편집: per isakson 2013년 5월 30일
Try
clear takeALongTimeFunction
tic, takeALongTimeFunction, toc
tic, takeALongTimeFunction, toc
it should return something like
ans =
17
Elapsed time is 7.004011 seconds.
ans =
17
Elapsed time is 0.000456 seconds.
where
function out = takeALongTimeFunction()
persistent result_of_last_time
if not( isempty( result_of_last_time ) )
out = result_of_last_time;
return
end
out = 17;
pause( 7 )
result_of_last_time = out;
end
  댓글 수: 2
Sean de Wolski
Sean de Wolski 2013년 5월 30일
I would even suggest saving the result to disk, then if the persistent variable is empty && and the matfile with the result doesn't exist, then do the computation.
David C
David C 2013년 5월 31일
Thanks to you both for these great ideas!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by