-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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
iD8DBQFB2WoSagVFX4UWr64RAsvlAJ9rWtroUaYuINsUd5hQuzPUorRAzwCfcFp+
G48dsa76llsXr09cKpD8SkA=
=5T2N
-----END PGP SIGNATURE-----