English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

separate nested loops
for{ }
for{}
....
}
}


or
for{}
for{}
for{}
for{}
.....
}
}
}
}

which option executes faster?

2007-02-05 18:02:34 · 5 answers · asked by Anonymous in Computers & Internet Programming & Design

5 answers

Of course, two is faster

2007-02-05 18:12:53 · answer #1 · answered by montblanc_polter 2 · 0 1

depends on the loops value, but given a loop size n and m the nested structure executes in n x m time, and the four nested example of loops size i, j, k, l will run in i x j x k x l time.

in the case where n = i x j and m = k x l then there may exist certain method specific reasons why one may run faster than the other, it which case your just going to have to performance test each.

2007-02-06 02:17:12 · answer #2 · answered by KnightSpot 2 · 1 0

If you have 2 separate FOR loops, chances are that they would be fast, as controlling values may be different.

If you have FOR loop nested to 4 level, it may be slower in execution.

If you can put in some more details of context in which you want to use these loops, may be you'll get better and more accurate answer.

2007-02-06 02:15:04 · answer #3 · answered by kvasani 2 · 1 0

The two nested for loops will have less overhead than the four.
The overhead is so small usually as to be negligible. (on the order of say 25 machine cycles).
Unless you are doing something really computationally intensive go for readability.
Be aware that the multiplication needed for multi-dimensional array handling can eat up a lot of time. (relatively speaking.)

2007-02-06 02:15:30 · answer #4 · answered by J C 5 · 1 0

The shorter code with the same output is the better. so, the two loops is better if you have the same output. The execution will work faster because it will not check on many condition before it will work with a statement.

2007-02-06 02:12:00 · answer #5 · answered by joseph domingo 1 · 0 1

fedest.com, questions and answers