Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > which loop is faster

Reply
Thread Tools

which loop is faster

 
 
sushant
Guest
Posts: n/a
 
      01-03-2005
hi all,

suppose i have 2 loops one inside the other, like this

1) for(i=0;i<=100;i++)
{
for(j=0;j<=10;j++)
{
some code;
}
}

2) for(i=0;i<=10;i++)
{
for(j=0;j<=100;j++)
{
some code;
}
}

so which loops will work faster the 1st one or the 2nd one.

thnx in advance
sushant
 
Reply With Quote
 
 
 
 
Shan
Guest
Posts: n/a
 
      01-03-2005
Please dont ask interview questions in CLC....

 
Reply With Quote
 
 
 
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      01-03-2005
sushant <(E-Mail Removed)> wrote:
> suppose i have 2 loops one inside the other, like this


> 1) for(i=0;i<=100;i++)
> {
> for(j=0;j<=10;j++)
> {
> some code;
> }
> }


> 2) for(i=0;i<=10;i++)
> {
> for(j=0;j<=100;j++)
> {
> some code;
> }
> }


> so which loops will work faster the 1st one or the 2nd one.


Nobody can tell but you after you made careful measurements. There's
nothing in the sppecifications of the C language that would require
one of the loops to be faster than the other. If one of them should
be faster than it's due to the way the implementation, i.e. the C com-
piler, is written and may also depend on what you're doing in the loop.
You can only find out by measuring what happens. Unfortunately, that
result will only be valid for the compiler/system combination you did
the tests with.
Regards, Jens
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
EventHelix.com
Guest
Posts: n/a
 
      01-03-2005
It will really depend upon "some code".
If "some code" just fits into the cache but the outer loop doesn't:
(2) would be faster.

If the inner and the outer loop fit into the cache, I don't expect to
see
a large difference in performance.

Deepa
--
http://www.EventHelix.com/EventStudio
EventStudio 2.5 - Automate sequence diagram generation

 
Reply With Quote
 
Neo
Guest
Posts: n/a
 
      01-03-2005

"EventHelix.com" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> It will really depend upon "some code".
> If "some code" just fits into the cache but the outer loop doesn't:
> (2) would be faster.


Whtz da meaning of outer loop doesn't, here?
-Neo

>
> If the inner and the outer loop fit into the cache, I don't expect to
> see
> a large difference in performance.
>
> Deepa
> --
> http://www.EventHelix.com/EventStudio
> EventStudio 2.5 - Automate sequence diagram generation
>



 
Reply With Quote
 
Neo
Guest
Posts: n/a
 
      01-03-2005

"sushant" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> hi all,
>
> suppose i have 2 loops one inside the other, like this
>
> 1) for(i=0;i<=100;i++)
> {
> for(j=0;j<=10;j++)
> {
> some code;
> }
> }
>
> 2) for(i=0;i<=10;i++)
> {
> for(j=0;j<=100;j++)
> {
> some code;
> }
> }
>
> so which loops will work faster the 1st one or the 2nd one.
>
> thnx in advance
> sushant


Implementation Defined!
How your target compiler optimize the code... and the target platform
you are compiling your code for...

-Neo


 
Reply With Quote
 
infobahn
Guest
Posts: n/a
 
      01-03-2005
Neo wrote:
>
> "sushant" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) m...
> >


<snip>

> > so which loops will work faster the 1st one or the 2nd one.
> >
> > thnx in advance
> > sushant

>
> Implementation Defined!


Not in the C sense of that term. Compilers are not required to
document their choice in this case.
 
Reply With Quote
 
xarax
Guest
Posts: n/a
 
      01-03-2005
"Neo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "EventHelix.com" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> > It will really depend upon "some code".
> > If "some code" just fits into the cache but the outer loop doesn't:
> > (2) would be faster.

>
> Whtz da meaning of outer loop doesn't, here?
> -Neo


Seems obvious from the next piece:

> >
> > If the inner and the outer loop fit into the cache, I don't expect to
> > see
> > a large difference in performance.


If the outer loop doesn't fit within the cache
line and the inner does fit, then there is a
performance hit each time the inner loop exits.

If both inner and outer loops fit entirely
within a cache line and the compiler has properly
placed the code within the cache line, then there
should be no significant difference between the
two examples.

--
----------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS for FREE!



 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      01-03-2005
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

sushant wrote:
> hi all,
>
> suppose i have 2 loops one inside the other, like this
>
> 1) for(i=0;i<=100;i++)
> {
> for(j=0;j<=10;j++)
> {
> some code;
> }
> }
>
> 2) for(i=0;i<=10;i++)
> {
> for(j=0;j<=100;j++)
> {
> some code;
> }
> }
>
> so which loops will work faster the 1st one or the 2nd one.


Sorry, but as others have pointed out, this question cannot be answered
properly within the confines of standard C. The C language makes no
distinctions between these two examples, and the only operational distinction
is made by your specific compiler. In some cases, both examples will have
equal execution time, while in other cases, one example will have different
time than the other.

However, at the risk of being accused of a 'premature optimization', I'd guess
that the average 'dumb' compiler implementation would generate 'faster' code
for the second example than the first.

In the first example, the outer loop is initialized once, but the inner loop
is initialized 100 times.

In the second example, the outer loop is still initialize once, but the inner
loop is initialized 10 times.

Assuming a discernable execution penalty is imposed for loop initialization,
the first example will incur 10 times more penalty than the second example would.

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFB2WoSagVFX4UWr64RAsvlAJ9rWtroUaYuINsUd5hQuz PUorRAzwCfcFp+
G48dsa76llsXr09cKpD8SkA=
=5T2N
-----END PGP SIGNATURE-----
 
Reply With Quote
 
KiLVaiDeN
Guest
Posts: n/a
 
      01-03-2005
I would say that generally it's better to make the loops with the more
occurences the more inside.

Only looking at memory talking about indexes :

[loop 1000]
[loop 10]
This configuration means doing 10 times memory access for the inner loop,
then 1 for outter loop, and so on :
10
1
10
1
....1000 times



[loop 10]
[loop 1000]
This one, means doing 1000 memory access to first index, then 1 to outter

1000
1
1000
1
....10 times


Looking this, we can see that on the second case, there is fewer memory
access to different positions.
On the first case, we go from one point of memory to the other all the time.
Meaning that if the memory is swapped for some reason, then first one will
be slower.

My 2 cents, tell me if I'm wrong

K


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Memset is faster than simple loop? AndersWang@gmail.com C Programming 24 03-12-2013 09:42 PM
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Re: Why is my code faster with append() in a loop than with a largelist? Dave Angel Python 4 07-06-2009 10:51 PM
method calls faster than a loop? tom fredriksen Java 42 03-20-2006 08:06 PM
Microcontrollers: which one ? which language ? which compiler ? The Jesus of Suburbia NZ Computing 2 02-11-2006 06:53 PM



Advertisments