tic toc in subfunctions

10 visualizaciones (últimos 30 días)
MG
MG el 1 de Nov. de 2015
Comentada: Walter Roberson el 2 de Nov. de 2015
I have the following code. I need to use tic toc to find out how much faster one loop runs versus the other. So far I have done several things. When I put tic after "function f = fact1(n);" and toc before "end" inside the for loop, and the same thing in the while loop, I get very tiny time numbers- 0.000006 seconds or something. And both of the times are the same. However, when I erase those commands in my editor window, and write the following in the command window instead: tic; my_factorial(5, 'for_loop'); toc; I get very different, and much larger numbers. However, I have also found that when I enter the above in the command window, and repeat the tic toc calculation several times, I get different values for elapsed time every time, even though the n and method remains the same. Sometimes I get a shorter time for my for loop vs. while, for the same n and method, and sometimes the situation is reversed. Why is that?
function f = my_factorial(n, method)
switch method
% To choose for loop method for computing factorial
case 'for_loop'
f = fact1(n);
% To choose while loop method for computing factorial
case 'while_loop';
f = fact2(n);
end
end
function f = fact1(n);
for loop
end
function f = fact2(n);
while loop
end

Respuesta aceptada

Walter Roberson
Walter Roberson el 2 de Nov. de 2015
Note: timeit was introduced in R2013b. For earlier releases, you can find timeit in the file exchange.
  2 comentarios
MG
MG el 2 de Nov. de 2015
The thing is, sometimes the while loop is faster than the for loop, and sometimes its the other way around. So I can't actually answer the question which is faster. Is that supposed to happen? I did the timeit thing too and I am still stuck with the same problem as before.
Walter Roberson
Walter Roberson el 2 de Nov. de 2015

Hold two pillows, one in each hand, at the same height, and release the two of them simultaneously. Which one hit the floor first? If you repeat again, does the same one hit the floor first? If you try a few more times? Which of the two pillows falls faster? Now switch hands and try again; does the answer stay the same?

Were you even able to tell the difference in falling speed? If not then does that mean there is no difference in falling speed, or does it just mean that you were not equipped to measure the difference?

If the same pillow did not always hit the floor first, does that mean that there is a difference in falling speed? Or does it mean that your mental control over releasing objects with both hands is not precise?

If you are having trouble telling which pillow falls faster, does that mean there is no difference in falling speed? After all, didn't someone prove that all objects fall at the same speed? No... the "same speed" applies only in vacuum where there is no air resistance, and you probably didn't hold the pillows exactly the same way each time, and there were probably small breezes... and maybe one of the pillows is more aerodynamic but you did not allow them to fall long enough to be able to tell the difference.

Now if you want to make this experiment more relevant to what happens on a computer, add to this mix a toddler who is wandering around near you, grabbing things and looking like they might put them in their mouth, at risk of knocking things over, and you have to mind the toddler while you are doing the experiments. Oh and sometimes you catch an odor and need to interrupt what you are doing while the pillows are falling to check the toddler's diaper.

You are using a general purpose computer, not a specialized device configured to only run your two routines. Sometimes you just can't tell whether one thing is faster than another, not without a lot of effort to build precise repeatable measurements. And that can mean that the two are the same speed but your measuring system has jitter (inconsistencies), or it can mean that the two are not the same speed but your measuring system is not fast enough to tell them apart.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Numeric Types en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by