which one is faster for loop or switch

Hi, I want to know which looping is better from programming point of view i.e. for loop or switch.
In my coding I have used switch and case , and it is taking lots of time. I am using it for optimization.
Kindly help me out in order to improve the speed of my code.
Any suggestion is appreciated.
Thanks, Suraj

1 comentario

Stephen23
Stephen23 el 9 de Abr. de 2015
Editada: Stephen23 el 9 de Abr. de 2015
Considering one single switch or loop in isolation might not be the best way to optimize code. Upload your whole Mfile and we can see at it in context! There might be other improvements and simplifications that you have not considered, that make the switch/loop irrelevant or insignificant.

Iniciar sesión para comentar.

 Respuesta aceptada

Thomas Koelen
Thomas Koelen el 9 de Abr. de 2015

0 votos

read in the comments. (I guess you are talking about if statements and not loops)

4 comentarios

Suraj Srivastava
Suraj Srivastava el 9 de Abr. de 2015
Hi Thomas,
Thanks for the reply.
I simply want to know which one is betetr......switch or if-statements.
Thomas Koelen
Thomas Koelen el 9 de Abr. de 2015
Editada: Thomas Koelen el 9 de Abr. de 2015
That depends on how many iterations you are planning to do! You should just try it out yourself with tic and toc, but I don't think the time you are going to win is more than the time youll spend on testing!
Suraj Srivastava
Suraj Srivastava el 9 de Abr. de 2015
suppose I am using it in Optimization process and there is around 10000 iterations.
Thomas Koelen
Thomas Koelen el 9 de Abr. de 2015
I'm gonna go with switch in this case.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 9 de Abr. de 2015

Comentada:

el 9 de Abr. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by