Borrar filtros
Borrar filtros

optimize and if statement

2 visualizaciones (últimos 30 días)
Victor
Victor el 24 de En. de 2014
Comentada: Victor el 24 de En. de 2014
I am running code that runs through a loop millions of times. I am trying to make the loop as effect as possible. The loop cannot be arrayed as future values depend on previous values. I used the profiler to see what was taking the most time in the loop. The line that is taking the the most time is an if statement:
if abs((a-b)/b) <.000001
Is there any way to speed this line up? change in the structure of the if or change in the way the condition is evaluated?

Respuestas (2)

Walter Roberson
Walter Roberson el 24 de En. de 2014
if b * 0.999999 < a & a < b * 1.000001
better recheck as it is pretty late at night for me.
  1 comentario
Victor
Victor el 24 de En. de 2014
Thanks for the reply. I tried your suggestion and it appears to take longer.

Iniciar sesión para comentar.


David Sanchez
David Sanchez el 24 de En. de 2014
You can try with different combinations like:
if abs((a-b)/b)*10^6 <1
if -1<(a-b)*10^6/b && (a-b)*10^6/b <1
if -.000001<(a-b)/b && (a-b)/b <.000001
if abs((a-b)/b) <1/1000000
if -b<(a-b)*10^6 && (a-b)*10^6 <b
but the time taken to check the condition is very similar in all of them. I think you can't hardly improve much the speed of that line.

Categorías

Más información sobre Loops and Conditional Statements 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