How to apply stop time condition with multiple time intervals?

3 visualizzazioni (ultimi 30 giorni)
n=100;
tic
Time = 0;
for i = 1:n
if Time >= 35
fprintf('You run out of time')
break
end
A = rand(12000,4400);
Time1 = toc;
B = rand(12000,4400);
tic
C = A.*B;
Time2 = toc;
tic
D = A./B;
Time3 = toc;
E = A.*A;
tic
E = B.*B;
Time4 = toc;
Time = Time1 + Time2 + Time3 + Time4;
end
I want to stop the code when Time = 35 sec, I have tried the above but my code is not terminating the code at specified time?

Risposta accettata

Davide Masiello
Davide Masiello il 29 Set 2022
Modificato: Davide Masiello il 29 Set 2022
n=100;
time = 0;
for i = 1:n
if sum(time) >= 35
fprintf('You run out of time')
break
end
tic
A = rand(12000,4400);
t1(i) = toc;
B = rand(12000,4400);
tic
C = A.*B;
t2(i) = toc;
tic
D = A./B;
t3(i) = toc;
E = A.*A;
tic
E = B.*B;
t4(i) = toc;
time(i) = t1(i)+t2(i)+t3(i)+t4(i);
disp(sum(time))
end
T = table((1:i-1)',t1',t2',t3',t4',cumsum(time)');
T.Properties.VariableNames = {'Iter.','t1','t2','t3','t4','Total time'}
  3 Commenti
Davide Masiello
Davide Masiello il 29 Set 2022
Understood. I will assume that you would also like to visualize these intervals in some way.
I have modified my answer above so that it displays a table with single interval times at each iteration plus the total cumulative time in the last column.
Mind that if you run this online it won't work, because it'll take longer than 55 seconds, which is Matlab's runtime cutoff online.
The reason why it takes longer than 35s is that you are not counting the time needed for some operations such as
E = A.*A;
I run it on my PC and it worked just fine.
I obtained the following result
T =
73×6 table
Iter. t1 t2 t3 t4 Total time
_____ _______ ________ ________ ________ __________
1 0.36939 0.068282 0.062946 0.035976 0.53659
2 0.37888 0.048764 0.046185 0.034659 1.0451
3 0.35138 0.049733 0.045011 0.035079 1.5263
4 0.34368 0.049401 0.044619 0.035098 1.9991
5 0.35859 0.049233 0.044094 0.034975 2.486
6 0.34777 0.048913 0.043947 0.034611 2.9612
7 0.34229 0.049768 0.043808 0.03503 3.4321
8 0.3427 0.048232 0.043639 0.035583 3.9023
9 0.34194 0.05184 0.044397 0.034535 4.375
10 0.33466 0.048371 0.047798 0.03501 4.8408
11 0.34679 0.051393 0.047315 0.035554 5.3219
12 0.34171 0.04984 0.045351 0.034455 5.7932
13 0.34365 0.052302 0.043544 0.035486 6.2682
14 0.33695 0.051611 0.046274 0.036184 6.7392
15 0.36399 0.051459 0.044339 0.034732 7.2337
16 0.37215 0.049525 0.044569 0.035415 7.7354
17 0.35725 0.049478 0.046472 0.035136 8.2237
18 0.37066 0.048594 0.05014 0.035303 8.7284
19 0.33719 0.044721 0.043755 0.034421 9.1885
20 0.35062 0.049076 0.047046 0.034546 9.6698
21 0.35845 0.049139 0.047191 0.035541 10.16
22 0.35675 0.04527 0.048512 0.034678 10.645
23 0.34079 0.054753 0.045062 0.034532 11.12
24 0.3687 0.049977 0.043941 0.034611 11.618
25 0.33985 0.048851 0.046382 0.035164 12.088
26 0.3429 0.044942 0.04464 0.03497 12.555
27 0.36748 0.049056 0.048524 0.034779 13.055
28 0.34472 0.049342 0.044766 0.034986 13.529
29 0.36218 0.050626 0.045363 0.034661 14.022
30 0.36545 0.050014 0.044266 0.035955 14.518
31 0.33707 0.053633 0.044236 0.035049 14.988
32 0.34205 0.048684 0.049069 0.035021 15.462
33 0.34096 0.048564 0.047281 0.034843 15.934
34 0.34483 0.05002 0.04479 0.034955 16.409
35 0.3486 0.048555 0.046611 0.034456 16.887
36 0.41002 0.049711 0.044558 0.034498 17.426
37 0.33541 0.044762 0.045437 0.039099 17.89
38 0.34224 0.048564 0.045476 0.034788 18.361
39 0.34035 0.050783 0.044695 0.034843 18.832
40 0.35756 0.049778 0.044396 0.035087 19.319
41 0.36009 0.046751 0.044817 0.035935 19.807
42 0.39459 0.050433 0.043778 0.037127 20.332
43 0.34884 0.048504 0.04566 0.035078 20.811
44 0.34405 0.049823 0.04368 0.034994 21.283
45 0.33656 0.051724 0.049868 0.034597 21.756
46 0.36171 0.048757 0.045144 0.034488 22.246
47 0.34071 0.048463 0.046175 0.035084 22.716
48 0.34944 0.04906 0.046477 0.036401 23.198
49 0.33494 0.048195 0.045897 0.034958 23.662
50 0.34986 0.047678 0.044767 0.035154 24.139
51 0.34773 0.048137 0.045249 0.039057 24.619
52 0.34926 0.056467 0.0448 0.034575 25.104
53 0.34466 0.04823 0.047419 0.041498 25.586
54 0.38337 0.050785 0.044854 0.035539 26.101
55 0.33795 0.048054 0.043818 0.035378 26.566
56 0.34112 0.049547 0.050481 0.034612 27.042
57 0.36411 0.050886 0.046011 0.03456 27.537
58 0.34991 0.048303 0.046958 0.034947 28.017
59 0.33776 0.049369 0.046196 0.037852 28.489
60 0.34505 0.048388 0.043728 0.034452 28.96
61 0.35878 0.048209 0.044458 0.03511 29.447
62 0.40436 0.049268 0.047189 0.034544 29.982
63 0.3523 0.048919 0.048061 0.035648 30.467
64 0.35395 0.051569 0.05037 0.034398 30.957
65 0.37335 0.050237 0.050272 0.035661 31.467
66 0.37425 0.050641 0.047039 0.041582 31.98
67 0.41363 0.049155 0.046312 0.035153 32.525
68 0.37333 0.049417 0.047219 0.035022 33.03
69 0.35292 0.049107 0.046173 0.035683 33.514
70 0.36 0.049019 0.044233 0.039235 34.006
71 0.35941 0.05211 0.044699 0.035086 34.497
72 0.36071 0.049099 0.047193 0.035966 34.99
73 0.36268 0.047597 0.044428 0.034712 35.48

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Startup and Shutdown in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by