# why estimating 3 values takes longer time than estimating 4 values?

3 ビュー (過去 30 日間)
Sadiq Akbar 2023 年 2 月 12 日
コメント済み: Sadiq Akbar 2023 年 2 月 14 日
In the attached code, when we estimate 2 values of u, it takes less time than the case where we estimate 3 values of u? It seems ok. But its strange that when we look at the time for estimating 3 values of u, it takes more time than estimating 4 values of u. Why it is so?
##### 3 件のコメント1 件の古いコメントを表示1 件の古いコメントを非表示
Sadiq Akbar 2023 年 2 月 13 日
Thanks a lot dear John D'Errico for your kind response. In this code we have three m files. 'fpa1.m' is the algorithm whose execution time needs to be estimated for different values of vector u using the user defined function 'myfun.m'. The fpa1.m has three outputs namely "best,fmin,time". In this best is the estimated values of aalgorithm fpa1. If u has 2 values, best will be having two values. If u has 3 values, then best will also have 3 values and so on. i.e., best the estimated vector of fpa1 to mach u. But I am interested in its time output.
Now to run fpa1.m, I have written 'main1TimeComparing.m'. In this 'main1TimeComparing.m', first I have put 2 values in u and run it to see how much time fpa1 takes in estimating these two values of u. Likewise, then I put 3 values in vector u inside 'main1TimeComparing.m' and run it again to see how much time fpa1 takes to estimate these three values of u. Similarly, then I put 4 values in u and run it and note the time. I found that the time in estimating 2 values of u is less than that required for 3 values of u. It is correct. But it is very strange that when I run it for 4 values, then fpa1 takes more time in estimating 3 values than estimating 4 values. Naturally, the time required for estimating 3 values of u should be less than the estimation of 4 values of u. But it is not so. It is the opposite case i.e., estimation of 4 values of u takes less time than estimation of 3 values. Wy it is so?
Sadiq Akbar 2023 年 2 月 14 日
@to all experts on this forum:
Is this a very difficult issue that no one is responding? I know from the past that many of you have solved very complicated issues, then why no one is responding here? I have been waiting since the day of posting this question. Please say something in response because this issue has stuck me. Regards,

サインインしてコメントする。

### 採用された回答

Anton Kogios 2023 年 2 月 14 日
I am not exactly an 'expert', but the times I get depend on:
• the order that you run them (like 3, then 2, then 1). From my basic understanding, you get different results based on the order due to how things run on processors (I think?).
• more probably, the mean of u. I am not exactly sure about the aim of your code/what it is doing, but values of u with an even number of elements and a mean of 0 perform faster than values of u with one less element and a mean not equal to 0.
I modified your original main1TimeComparing.m to a new one (attached to post). It loops 20 times per length of u and also tests lengths of 5 and 6. I got the following results after running it twice on a M1 Pro MacBook Pro with the R2022b Apple Silicon Beta:
• Run 1: [5.3750 6.1739 6.0049 6.6926 6.3277]
• Run 2: [5.4516 6.2095 6.0587 6.7427 6.3106]
Hope this points you in the right direction since you know what your code is doing! In-built MATLAB functions have a lot of optimisations for vectors, and this may be a product of that.
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Sadiq Akbar 2023 年 2 月 14 日
Thanks a lot dear Anton Kogios for your kind response. Though it didn't solve my issue because the same happens again. Even in your given Run1 and Run2, you can check it yourself, yet I am going to accept your answer because at least you tried it. Once again, thank you very much.

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeLogical についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by