473,404 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

abt nested loops

Hi
i have two nested loops as shown below:
1.
for(i=0;i<=1000;i++)
{
for(i=0;i<=100;i++)
{
.....;
.....;
}

}
and other one is
2.
for(i=0;i<=100;i++)
{
for(i=0;i<=1000;i++)
{
.....;
.....;
}

}
so my question is which loop is best in terms of performance..please
comment why is it so ..
what i feel is 2nd loop is best in performance wise.

Regards'
Pavan

Nov 14 '05 #1
10 1848
Pavan wrote:

Hi
i have two nested loops as shown below:
1.
for(i=0;i<=1000;i++)
{
for(i=0;i<=100;i++)
{
.....;
.....;
}

}

and other one is
2.
for(i=0;i<=100;i++)
{
for(i=0;i<=1000;i++)
{
.....;
.....;
}

}
so my question is which loop is best in terms of performance..please
comment why is it so ..
what i feel is 2nd loop is best in performance wise.


2nd loop stops.
1st loop doesn't.

--
pete
Nov 14 '05 #2
Hi Pete
thx for replying ..i am afraid ,, i did't get you !!
why does 2nd loops stops and where it stops

please be clear

cheers
Pavan

Nov 14 '05 #3
Pavan wrote:

Hi Pete
thx for replying ..i am afraid ,, i did't get you !!
why does 2nd loops stops and where it stops

please be clear


2.
for(i=0;i<=100;i++)

/* first, i equals 0 */

{
for(i=0;i<=1000;i++)
{
.....;
.....;
}

/*
** second, i equals 1001,
** outer loop stops because i > 100
*/

}

--
pete
Nov 14 '05 #4
Hi.

You should really do it something like this instead:

1.
for(i=0;i<=1000;i++)
{
for(j=0;j<=100;j++)
{
.....;
.....;
}
}
and other one is
2.
for(i=0;i<=100;i++)
{
for(j=0;j<=1000;j++)
{
.....;
.....;
}
}

Notice that I change i with j in one of the loops. If you don't do that
the value of i will be changed by both loops.

When it comes to performance I guess you wouldn't notice that bi a
difference. It would depend more upon what you're doing.

--
bjrnove

Nov 14 '05 #5


pete wrote:

Pavan wrote:

Hi Pete
thx for replying ..i am afraid ,, i did't get you !!
why does 2nd loops stops and where it stops

please be clear


2.
for(i=0;i<=100;i++)

/* first, i equals 0 */

{
for(i=0;i<=1000;i++)
{
.....;
.....;
}

/*
** second, i equals 1001,
** outer loop stops because i > 100
*/

}

--
pete


I think the OP is an extreme newbie, and isn't asking the question
properly. I fear his use of index "i" in both loops is not what he meant
- probably meant to inquire about two nested loops with different linit
on the outer loop and inner loop. The use of "<=" also shows probable
non-understanding of C.

If what the OP meant is
for (i=0; i<n; i++) {
for (j=0; j<m; j++) {
...
}
}
and then asking which is the better way: m<n or m>n?
The answer: it depends on what happens inside the inner loop, and what
happens only inside the outer loop, especially if i and j are used as
indices of multiple-dimensioned arrays.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
Nov 14 '05 #6
Pavan wrote:

Hi
i have two nested loops as shown below:
1.
for(i=0;i<=1000;i++)
{
for(i=0;i<=100;i++)
{
.....;
.....;
}

}

and other one is
2.
for(i=0;i<=100;i++)
{
for(i=0;i<=1000;i++)
{
.....;
.....;
}

}
so my question is which loop is best in terms of performance..please
comment why is it so ..
what i feel is 2nd loop is best in performance wise.


I can't say which one is "better in performance", but I can tell you that
the outer loop in the first example is an infinite loop, while the second
will have the outer loop execute only once.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Nov 14 '05 #7
Pavan wrote on 06/05/05 :
Hi Pete
thx for replying ..i am afraid ,, i did't get you !!
why does 2nd loops stops and where it stops


Because you are using the same counter. 2 loops, 2 counters.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"

Nov 14 '05 #8
"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.
Nov 14 '05 #9

"Pavan" <pa*******@gmail.com> wrote in message
i have two nested loops as shown below:
1.
for(i=0;i<=1000;i++)
{
for(i=0;i<=100;i++)
{
.....;
.....;
}

}
and other one is
2.
for(i=0;i<=100;i++)
{
for(i=0;i<=1000;i++)
{
.....;
.....;
}

}
so my question is which loop is best in terms of performance..please
comment why is it so ..
what i feel is 2nd loop is best in performance wise.

I'd expect the second loop to be marginally faster on some machines, because
the inner loop counter may be held in a register whilst the outer loop
counter may not be. So if the loop body is completely trivial you may have a
measureable speed difference.
However this is likely to be overwhelmed by the caches issues that other
people have mentioned in typical real code that loops to access arrays of
memory.
Nov 14 '05 #10
"Malcolm" <re*******@btinternet.com> writes:

"Pavan" <pa*******@gmail.com> wrote in message
i have two nested loops as shown below:
1.
for(i=0;i<=1000;i++)
{
for(i=0;i<=100;i++)
{
.....;
.....;
}

}
and other one is
2.
for(i=0;i<=100;i++)
{
for(i=0;i<=1000;i++)
{
.....;
.....;
}

}
so my question is which loop is best in terms of performance..please
comment why is it so ..
what i feel is 2nd loop is best in performance wise.

I'd expect the second loop to be marginally faster on some machines, because
the inner loop counter may be held in a register whilst the outer loop
counter may not be. So if the loop body is completely trivial you may have a
measureable speed difference.
However this is likely to be overwhelmed by the caches issues that other
people have mentioned in typical real code that loops to access arrays of
memory.

I would expect a big performance impact because the two loops use the same
loop index variable.

__________________________________________________ ____________________________
Dr Chris McDonald E: ch***@csse.uwa.edu.au
Computer Science & Software Engineering W: http://www.csse.uwa.edu.au/~chris
The University of Western Australia, M002 T: +618 6488 2533
Crawley, Western Australia, 6009 F: +618 6488 1089
Nov 14 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
0
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # David Eppstein of the Geometry Junkyard fame gave this elegant # version for returing all possible pairs from a range of n numbers. def combo2(n): return...
4
by: dw | last post by:
Hello all. We're doing a site with teams and their members. We've got a page where we need to display people according to who belongs to a which team. I've heard that nested loops are bad, but...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
9
by: Gregory Petrosyan | last post by:
I often make helper functions nested, like this: def f(): def helper(): ... ... is it a good practice or not? What about performance of such constructs?
5
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
Could someone give me a simple example of nested scope in C#, please? I've searched Google for this but have not come up with anything that makes it clear. I am looking at the ECMA guide and...
4
by: toddlahman | last post by:
I am using two while loops that are nested. The first loop (post name) returns the full column of results, but the second (post modified) only returns the first row of the column. Is there another...
13
by: Fredrik Lundh | last post by:
Patrol Sun wrote: so why exactly are you trying to nest 20 or 100 for-in loops? </F>
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.