Why does this code give error?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
I want to implenet timeit() function to find the machine time for the execution of the m-file 'fpa1.m'. But when I run the attached 'main.m', it gives me the following error:
Error using timeit
First argument must be a function handle that takes no input argument.
Error in main (line 6)
fprintf('time taken by fpa1 = %e seconds', timeit(@fpa1,3))
채택된 답변
Walter Roberson
2023년 2월 15일
fcn = @() fpa1(10,0.8,2000,4,[-90 -90 -90 -90],[90 90 90 90],@(b)myfunction(b,u,Noise));
timeit(fcn, 3)
댓글 수: 10
[best,fmin,time]=fpa1(10,0.8,2000,4,[-90 -90 -90 -90],[90 90 90 90],@(b)myfunction(b,u,Noise));
then how can we use timeit() function?
Dyuman Joshi
2023년 2월 15일
Regardless of the number of outputs, the code to use timeit() will be the same as Walter mentioned.
Sadiq Akbar
2023년 2월 15일
Thanks a lot dear Dyuman Joshi for your kind response. You are right. But what I mean is that I want to get the outputs also along with the time i.e., I want to get the two outputs best and fmin also along with time. Then how will we do that?
Dyuman Joshi
2023년 2월 15일
You want the time taken by the code to run as an output to the code? or is time an output variable from fpa1?
Walter Roberson
2023년 2월 15일
You cannot get the outputs when you do timing using timeit(). timeit() tries to run the code several times to reduce the error estimates of the time.
[best,fmin]=fpa1(10,0.8,2000,4,[-90 -90 -90 -90],[90 90 90 90],@(b)myfunction(b,u,Noise));
and find these two outputs as well as the time from timeit() function.
Reply to Walter Roberson: You mean that 1st we will estimate the outputs and then we will have to run it separately for estimating time?
You can use
start = tic;
[best,fmin,time]=fpa1(10,0.8,2000,4,[-90 -90 -90 -90],[90 90 90 90],@(b)myfunction(b,u,Noise));
duration = toc(start);
to time how long the call took.
The purpose of using timeit() is to try to estimate the execution time more reliably. When you time something using a single tic/toc run, then the situation could be anywhere from "the code ran faster than typical" up to "oh, bad luck, the defragmenter decided to run at the same time that the anti-virus was scanning the drive at the same time someone was emailing you some meme videos." timeit() runs the code several times to try to estimate the amount of time it would take the code to run on a system that was not being interrupted with side quests.
Sadiq Akbar
2023년 2월 16일
Thanks a lot dear Walter Roberson for your kind response. I had used 'tic-toc' inside 'fpa1.m' but its time was not accurate as can be seen at the following URL:
Then someone told me to use 'timeit()'. It gives the accurate time. As you can see at the above URL, I am estimating the values of vector u using fpa1. There are three cases namely when u has 2 values, when u has 3 values and when u has 4 values. 'fpa1' estimates all the cases. But there ,as you can see, when u has 2 values, fpa1 takes less time as compared to the case when u has 3 values and logically its correct also. But when u has 3 values, then fpa1 takes more time as compared to the case where u has 4 values which is wrong. That's why I tried to use timeit() function.
Now I am stuck what to do? If I use 'tic-toc', then I face that problem but when I want to use timeit() then you say, we cannot estimate the output and time simultaneously. Then what should I do next? Any solution?
Walter Roberson
2023년 2월 16일
The original version of the timeit() function is in the File Exchange. You could load it from there and modify it.
But suppose it gives different output results on different timing iterations, then which version should be output?
Sadiq Akbar
2023년 2월 16일
Thanks a lot dear Walter Roberson for your kind response. Actually, I have already seached there, the code of the timeit() has been removed from the File Exchange. So, the conclusion is that first, I will run the code for the estimation of outputs and then I will run the code for timeit() because the 'tic-toc' gives wrong timing for estimating different number of values. Thanks a lot once again dear Walter Roberson for your kind response.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Performance and Memory에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
