"Pavan" <pa*******@gmail.com> wrote:
# Hi
# i have two nested loops as shown below:
# 1.
# for(i=0;i<=1000;i++)
# {
# for(i=0;i<=100;i++)
# {
# .....;
# .....;
# }
#
# }
Presumably you mean for(i...) for(j...) instead of really for(i...) for(i...)
In that case, it depends on details like whether you're using machine with caches
and virtual memory and memory access patterns, as well as register access patterns,
etc. Putting the small loop outside means 101 loop initialisations compared to 1001,
but the loop overhead is trivial compared to cache and page miss overheads.
What's more important is the pattern of memory access. If you're using arrays like
a[i][j] with virtual memory, then it's almost always better to have i in the outer
loop and j in the inner so that loads and stores tend to be clusterred in the same
pages. This also improves automated vectorisation if your compiler provides that.
More complicated access patterns can optimise the use of caches. You can look up
tiling for more information.
--
SM Ryan
http://www.rawbw.com/~wyrmwif/
So basically, you just trace.