473,386 Members | 1,803 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,386 software developers and data experts.

which loop is faster

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
Nov 14 '05 #1
34 3373
Please dont ask interview questions in CLC....

Nov 14 '05 #2
sushant <th********@rediffmail.com> 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 ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #3
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

Nov 14 '05 #4
Neo

"EventHelix.com" <ev********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr 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

Nov 14 '05 #5
Neo

"sushant" <th********@rediffmail.com> wrote in message
news:51*************************@posting.google.co 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
Nov 14 '05 #6
Neo wrote:

"sushant" <th********@rediffmail.com> wrote in message
news:51*************************@posting.google.co 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.
Nov 14 '05 #7
"Neo" <ti***************@yahoo.com> wrote in message
news:33*************@individual.net...

"EventHelix.com" <ev********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr 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!

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

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.


Of course, the looped could just be unrolled by the compiler...

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Nov 14 '05 #11
In article <51*************************@posting.google.com> ,
th********@rediffmail.com (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.


some code;

is not legal C. Replace it with some legal C code, then the answer will
depend on the C code and in the implementation.

To get a correct answer, you'll have to measure it.
Nov 14 '05 #12
Shan wrote:
Please don't ask interview questions in CLC.


Please don't answer interview questions in CLC.
Nov 14 '05 #13
On 3 Jan 2005 04:11:39 -0800, "EventHelix.com" <ev********@gmail.com>
wrote in comp.lang.c:

Either figure out how to do one of the following:

1. Get the new Google groups reply to quote context.

2. Quote the context yourself.

3. If you can't do either of the above, use a real newsreader. Free
Agent, Mozilla Thunderbird, and Gravity are all free and quite good.
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


What's a 'cache'? Where is it defined in the C standard? I write C
code for quite a few platforms that don't have any such thing.

So what's your answer if the OP is writing code for a platform without
a cache?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #14
KiLVaiDeN wrote:
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


I do not know if you are wrong or not. What keeps popping into my head
is the commutive property of mathematics: (10 * 100) == (100 * 10). But
who can really say? After all, there are a plethora of C compilers out
there and they will all emit binary code differently, for whatever
platform they are targeted for.

--
Regards,
Stan Milam.
-----------------------------------------------------------------------------
Vita Brevis. Carpe Guitarum! - Jamie Kinscherff
-----------------------------------------------------------------------------
Nov 14 '05 #15
Neo

"Stan Milam" <st*****@swbell.net> wrote in message
news:q%*****************@newssvr12.news.prodigy.co m...
KiLVaiDeN wrote:
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


I do not know if you are wrong or not. What keeps popping into my head is
the commutive property of mathematics: (10 * 100) == (100 * 10). But who
can really say? After all, there are a plethora of C compilers out there
and they will all emit binary code differently, for whatever platform they
are targeted for.

--
Regards,
Stan Milam.
-----------------------------------------------------------------------------
Vita Brevis. Carpe Guitarum! - Jamie Kinscherff
-----------------------------------------------------------------------------


Yes, mathematically dis is true.

When it comes to relams of C compilers its totally up to the compiler how it
generates the machine instructions to do the same. and dat also depends on,
for which target machine you are going to generate dat code. Its properties
affect the binary code generation like : word length, chache - if any the
processor has, address n data buses (von newman vs. harvard architecture) n
of couse, how compiler optimize the code while exploiting all these
available facilites.

One important thing - Standard doesn't make any assumptions about it.

It is obvious by closely analysing the two loops that memory access will be
more in first case
[100]
....[10]

as compared to
[10]
....[100]

'coz inner loop breaks 10 times more and intialization has to be performed
for loop control variable 10 times more, assuming, all other things being
constant.

Is that make sense?

-Neo
Nov 14 '05 #16
Neo wrote:

<snip>
Yes, mathematically dis is true.

When it comes to relams of C compilers its totally up to the compiler how it generates the machine instructions to do the same. and dat also depends on, for which target machine you are going to generate dat code. Its properties affect the binary code generation like : word length, chache - if any the processor has, address n data buses (von newman vs. harvard architecture) n of couse, how compiler optimize the code while exploiting all these
available facilites.

One important thing - Standard doesn't make any assumptions about it.

It is obvious by closely analysing the two loops that memory access will be more in first case
[100]
...[10]

as compared to
[10]
...[100]

'coz inner loop breaks 10 times more and intialization has to be performed for loop control variable 10 times more, assuming, all other things being constant.

Is that make sense?

-Neo


I normally avoid spelling flames but...

dis -> this
its -> it's (in at least one case)
dat -> that
coz -> because
I've skipped a couple of genuine typos
--
Nick Keighley

Nov 14 '05 #17

Neo wrote:
"Stan Milam" <st*****@swbell.net> wrote in message
news:q%*****************@newssvr12.news.prodigy.co m...

Yes, mathematically dis is true.

When it comes to relams of C compilers its totally up to the compiler how it generates the machine instructions to do the same. and dat also depends on, for which target machine you are going to generate dat code. Its properties affect the binary code generation like : word length, chache - if any the processor has, address n data buses (von newman vs. harvard architecture) n of couse, how compiler optimize the code while exploiting all these
available facilites.

One important thing - Standard doesn't make any assumptions about it.

It is obvious by closely analysing the two loops that memory access will be more in first case
[100]
...[10]

as compared to
[10]
...[100]

'coz inner loop breaks 10 times more and intialization has to be performed for loop control variable 10 times more, assuming, all other things being constant.

Is that make sense?

-Neo


OK, I just had to find out the truth, so I compiled the program and
looked at the disassemly. The results seem to be pretty much the same.
Take a look :

# For loop 1
00411A8F mov dword ptr [i],0
00411A96 jmp main+71h (411AA1h)
00411A98 mov eax,dword ptr [i]
00411A9B add eax,1
00411A9E mov dword ptr [i],eax
00411AA1 cmp dword ptr [i],0Ah
00411AA5 jg main+0A0h (411AD0h)

# for loop 2
00411AA7 mov dword ptr [j],0
00411AAE jmp main+89h (411AB9h)
00411AB0 mov eax,dword ptr [j]
00411AB3 add eax,1
00411AB6 mov dword ptr [j],eax
00411AB9 cmp dword ptr [j],64h
00411ABD jg main+9Eh (411ACEh)

# the code (printf("") in this case)
00411ABF push offset string "" (4240C8h)
00411AC4 call @ILT+1170(_printf) (411497h)
00411AC9 add esp,4

# end of loop 2
00411ACC jmp main+80h (411AB0h)

# end of loop 1
00411ACE jmp main+68h (411A98h)

That's one way, and if it's done the other way then the only difference
will be the parameters used for the for looping. The thing that makes
it loop is jmp, and in both cases it will jmp the same number of times.
The other parts of the for loops have exactly the same instructions,
but with different parameters.
I think we can conclude that neither of these loops will be faster.

Nov 14 '05 #18
On Mon, 03 Jan 2005 03:58:19 -0800, sushant wrote:
hi all,

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

1) for(i=0;i<=100;i++)
This is suspicious because it will loop 101 times,
{
for(j=0;j<=10;j++)
and this will loop 11 times. If you expected 100 and 10 then make the
comparison a < rather than a <= . That's common and normal in C as we tend
to count from 0 a lot especially when array indexing is concerned. A 100
element array has elements indexed from 0 to 99 and trying to access an
element at position 100 would be an error.
{
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.


It is impossible to say. It will depend on your compiler (and the options
you use with it), the processor used to run the code and not least on what
exactly "some code" is. If "some code" uses i and/or j then the two
samples are not equivalent and it doesn't make much sense to talk about
which is faster. That's also the case if i and/or j are used after the
loops.

Lawrence
Nov 14 '05 #19
Jack Klein wrote:
What's a 'cache'? Where is it defined in the C standard?


Isn't it a place where you can hide your keywords?
Nov 14 '05 #20

Jack Klein wrote:
On 3 Jan 2005 04:11:39 -0800, "EventHelix.com" <ev********@gmail.com>
wrote in comp.lang.c:

Either figure out how to do one of the following:

1. Get the new Google groups reply to quote context.

2. Quote the context yourself.

3. If you can't do either of the above, use a real newsreader. Free
Agent, Mozilla Thunderbird, and Gravity are all free and quite good.
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
What's a 'cache'? Where is it defined in the C standard? I write C
code for quite a few platforms that don't have any such thing.

So what's your answer if the OP is writing code for a platform

without a cache?


Hmm, I dunno whether you're joking? The cache is a type of memory,
there are quite a few caches within the computer. In this case the
important caches are the ones CPU caches. Have a look at
http://www.pantherproducts.co.uk/Art...%20Cache.shtml for
more info.

Nov 14 '05 #21

<pe**************@yahoo.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...

Jack Klein wrote:
On 3 Jan 2005 04:11:39 -0800, "EventHelix.com" <ev********@gmail.com>
wrote in comp.lang.c:

Either figure out how to do one of the following:

1. Get the new Google groups reply to quote context.

2. Quote the context yourself.

3. If you can't do either of the above, use a real newsreader. Free
Agent, Mozilla Thunderbird, and Gravity are all free and quite good.
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
What's a 'cache'? Where is it defined in the C standard? I write C
code for quite a few platforms that don't have any such thing.

So what's your answer if the OP is writing code for a platform

without
a cache?


Hmm, I dunno whether you're joking?


He's not.
The cache is a type of memory,
there are quite a few caches within the computer.
1. The topic here is the C language, not computers.
2. The C language has no notion of 'cache'
2. Not all computers use 'caches'
In this case the
important caches are the ones CPU caches. Have a look at
http://www.pantherproducts.co.uk/Art...%20Cache.shtml for
more info.


No need. The hardware described there might include
'caches', but that's nothing to do with C.

-Mike
Nov 14 '05 #22

<pe**************@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...

Neo wrote:
"Stan Milam" <st*****@swbell.net> wrote in message
news:q%*****************@newssvr12.news.prodigy.co m...

Yes, mathematically dis is true.

When it comes to relams of C compilers its totally up to the compiler how it
generates the machine instructions to do the same. and dat also

depends on,
for which target machine you are going to generate dat code. Its

properties
affect the binary code generation like : word length, chache - if any

the
processor has, address n data buses (von newman vs. harvard

architecture) n
of couse, how compiler optimize the code while exploiting all these
available facilites.

One important thing - Standard doesn't make any assumptions about it.

It is obvious by closely analysing the two loops that memory access

will be
more in first case
[100]
...[10]

as compared to
[10]
...[100]

'coz inner loop breaks 10 times more and intialization has to be

performed
for loop control variable 10 times more, assuming, all other things

being
constant.

Is that make sense?

-Neo


OK, I just had to find out the truth,


The truth for your particular platform, implementation,
and current configuration of such.
so I compiled the program and
looked at the disassemly. The results seem to be pretty much the same.
For your current setup.
Take a look :
No need.

[snip]
That's one way, and if it's done the other way then the only difference
will be the parameters used for the for looping.
On your system.
The thing that makes
it loop is jmp, and in both cases it will jmp the same number of times.
The other parts of the for loops have exactly the same instructions,
but with different parameters.
I think we can conclude that neither of these loops will be faster.


On your system. On others, the results could be completely different.

-Mike
Nov 14 '05 #23
Neo

"sushant" <th********@rediffmail.com> wrote in message
news:51*************************@posting.google.co 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


2nd will be faster.
O'kay lets try to analyze it.

As per K&R2 (Page 60):
"The for statement
for(expr1; expr2; expr3)
statement
is equivalent to
expr1;
while(expr2) {
statement
expr3;
}

Grammatically, the three components of a for loop are expressions. Most
commonly, expr1 and expr3 are assignment or function calls and expr2 is a
relational expression."

In 1st one "expr1" will be executed 100 times, because inner loop breaks
more frequently. In 2nd case "expr1" will be executed 10 times only. So, all
things being equal (like generated target code) execution time will be more
in 1st case.

O'kay What about
for(i=0; i<1000; i++)
{
some code;
}
Will it run faster then any of the above versions?

Does it make sense, its all Standard C...

-Neo
Nov 14 '05 #24
sushant wrote:
suppose i have 2 loops one inside the other, like this

[ snipped: two nested loops, same with loops reversed ]

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


A good compiler will perform loop exchange where it's possibly and
beneficial on that platform. In other words, both run just as quickly
with a good compiler (because it transforms one into the other).
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Nov 14 '05 #25
On Wed, 5 Jan 2005 09:30:57 +0530, in comp.lang.c , "Neo"
<ti***************@yahoo.com> wrote:

"sushant" <th********@rediffmail.com> wrote in message
news:51*************************@posting.google.c om...
hi all,

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

(snip example of nested loops)
2nd will be faster.
There's no way to say that.
O'kay lets try to analyze it.
You can't, unless you know what "some code" inside the inner loop is. A
modern optimising compiler can optimise both these to be identical, if it
can determine there are no side-effects of the inner code. Many compilers
might even unroll both loops entirely and execute 1000 evaluations of 'some
code'.

And then of course modern CPUs can often run such loops as parallel
processes in different pipelines. Again this might (or might not) mean that
version X was faster than version Y.

This is all in concordance with the 'as if' principle which allows the
compiler and/or hardware to rearrange your code how it likes, as long as
the result is the same as if it hadn't.

(of a once-unrolled loop)
Will it run faster then any of the above versions?


Yes. No. Sometimes. Never.


--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Nov 14 '05 #26
Neo

"Mark McIntyre" <ma**********@spamcop.net> wrote in message
news:gq********************************@4ax.com...
On Wed, 5 Jan 2005 09:30:57 +0530, in comp.lang.c , "Neo"
<ti***************@yahoo.com> wrote:

"sushant" <th********@rediffmail.com> wrote in message
news:51*************************@posting.google. com...
hi all,

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

(snip example of nested loops)
2nd will be faster.


There's no way to say that.
O'kay lets try to analyze it.


You can't, unless you know what "some code" inside the inner loop is. A
modern optimising compiler can optimise both these to be identical, if it
can determine there are no side-effects of the inner code. Many compilers
might even unroll both loops entirely and execute 1000 evaluations of
'some
code'.

And then of course modern CPUs can often run such loops as parallel
processes in different pipelines. Again this might (or might not) mean
that
version X was faster than version Y.

This is all in concordance with the 'as if' principle which allows the
compiler and/or hardware to rearrange your code how it likes, as long as
the result is the same as if it hadn't.

(of a once-unrolled loop)
Will it run faster then any of the above versions?


Yes. No. Sometimes. Never.


O'kay! that may be, or may not be TRUE, for generated code
or a particular implementation/hardware combinattion.

Also as per C Standard -
5.1.2.3 Program execution
1 The semantic descriptions in this International Standard describe the
behavior of an
abstract machine in which issues of optimization are irrelevant.
[-snip-]
3 In the abstract machine, all expressions are evaluated as specified by the
semantics.
An actual implementation need not evaluate part of an expression if it can
deduce that
its value is not used and that no needed side effects are produced
(including any
caused by calling a function or accessing a volatile object).

But as per language semantics, as also abstract machine states (for loop)
6.6.5.3 The for statement
1 Except for the behavior of a continue statement in the loop body, the
statement
for ( clause*1 ; expression*2 ; expression*3 ) statement
and the sequence of statements
{
clause*1 ;
while ( expression*2 ) {
statement
expression*3 ;
}
}
are equivalent (where clause*1 can be an expression or a declaration). 114
2 Both clause*1 and expression*3 can be omitted. If either or both are an
expression,
they are evaluated as a void expression. An omitted expression*2 is replaced
by a
nonzero constant.

"clause 1" will make the difference, becase it will be evaluated every time
loop is entered.

-Neo


--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

Nov 14 '05 #27
Neo wrote:

<snip>
2 Both clause*1 and expression*3 can be omitted. If either or
both are an expression, they are evaluated as a void expression.
An omitted expression*2 is replaced by a nonzero constant.

"clause 1" will make the difference, becase it will be evaluated every time
loop is entered.


It needn't be, for the reasons you pointed out. If it can be
optimised out legally, then it won't make any difference at all.
Nov 14 '05 #28
Lawrence Kirby wrote:
On Mon, 03 Jan 2005 03:58:19 -0800, sushant wrote:

hi all,

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

1) for(i=0;i<=100;i++)

This is suspicious because it will loop 101 times,

{
for(j=0;j<=10;j++)

and this will loop 11 times. If you expected 100 and 10 then make the
comparison a < rather than a <= . That's common and normal in C as we tend
to count from 0 a lot especially when array indexing is concerned. A 100
element array has elements indexed from 0 to 99 and trying to access an
element at position 100 would be an error.

{
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.

It is impossible to say. It will depend on your compiler (and the options
you use with it), the processor used to run the code and not least on what
exactly "some code" is. If "some code" uses i and/or j then the two
samples are not equivalent and it doesn't make much sense to talk about
which is faster. That's also the case if i and/or j are used after the
loops.

Lawrence


Hi, nice to see you back, if you are the real "god".

Is the compiler allowed to optimize the two loops into one?
Given: (Example 1)
unsigned int i, j;
for (i = 0; i < 10; ++i)
{
for (j = 0; j < 100; ++j)
{
Some_Code();
}
}

According to my elementary computer science knowledge, the
statement:
Some_Code();
is executed 1000 times. I believe this would be the same
as: (Example 2)
unsigned int k;
for (k = 0; k < 1000; ++k)
{
Some_Code();
}

Is there any rule in the standard preventing the compiler from
converting Example 1 to Example 2 (i.e. factoring out the
nested loop to just one loop)?

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Nov 14 '05 #29
Thomas Matthews <Th*************************@sbcglobal.net> scribbled the following:
Lawrence Kirby wrote:
(snip)
Lawrence

Hi, nice to see you back, if you are the real "god".


Lawrence, next time someone asks you if you're a god, you say yes!

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"The day Microsoft makes something that doesn't suck is probably the day they
start making vacuum cleaners."
- Ernst Jan Plugge
Nov 14 '05 #30
Joona I Palaste wrote:
Thomas Matthews <Th*************************@sbcglobal.net> scribbled the following:

(snip)
Hi, nice to see you back, if you are the real "god".


Lawrence, next time someone asks you if you're a god, you say yes!


No, ignore them, because they have insufficient faith, and are thus
doomed. True worshippers know the truth and have no doubts.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #31
CBFalconer wrote:
Joona I Palaste wrote:
Thomas Matthews <Th*************************@sbcglobal.net> scribbled the following:

(snip)

Hi, nice to see you back, if you are the real "god".


Lawrence, next time someone asks you if you're a god, you say yes!

No, ignore them, because they have insufficient faith, and are thus
doomed. True worshippers know the truth and have no doubts.


Amen. There is a god and his name is fred.
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #32
CBFalconer <cb********@yahoo.com> scribbled the following:
Joona I Palaste wrote:
Thomas Matthews <Th*************************@sbcglobal.net> scribbled the following:

(snip)
Hi, nice to see you back, if you are the real "god".
Lawrence, next time someone asks you if you're a god, you say yes!

No, ignore them, because they have insufficient faith, and are thus
doomed. True worshippers know the truth and have no doubts.


You failed to get the reference, didn't you?

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"We sorcerers don't like to eat our words, so to say."
- Sparrowhawk
Nov 14 '05 #33

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:cr**********@oravannahka.helsinki.fi...
CBFalconer <cb********@yahoo.com> scribbled the following:
Joona I Palaste wrote:
Thomas Matthews <Th*************************@sbcglobal.net> scribbled the following:
(snip)

Hi, nice to see you back, if you are the real "god".

Lawrence, next time someone asks you if you're a god, you say yes!

No, ignore them, because they have insufficient faith, and are thus
doomed. True worshippers know the truth and have no doubts.


You failed to get the reference, didn't you?

Ghostbusters. Awesome movie!
Nov 14 '05 #34
On Sun, 09 Jan 2005 19:32:26 +0000, Thomas Matthews wrote:
Lawrence Kirby wrote:
On Mon, 03 Jan 2005 03:58:19 -0800, sushant wrote:

....
Hi, nice to see you back, if you are the real "god".

Err, thanks. :-)
Is the compiler allowed to optimize the two loops into one?
Given: (Example 1)
unsigned int i, j;
for (i = 0; i < 10; ++i)
{
for (j = 0; j < 100; ++j)
{
Some_Code();
}
}


That depends on what is in Some_Code() and whether it uses i and j. If it
is literally a function call with no arguments as shown here then yes it
can do that. It can do anything it likes as long as the program generates
correct output when run successfully.

Lawrence
Nov 14 '05 #35

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

Similar topics

17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
5
by: zaemin | last post by:
From a web-site that teaches optimization for c, I saw bellow optimization rule. int fact1_func (int n) { int i, fact = 1; for (i = 1; i <= n; i++) fact *= i; return (fact);
11
by: Farel | last post by:
Which is Faster in Python and Why? jc = {}; m = x = ,,.......] # upwards of 10000 entries def mcountb(): for item in x: b = item; b.sort(); bc = 0 for bitem in b: bc += int(bitem) try: m...
4
by: Sonnich | last post by:
Hi I have a costum function for a special search, which sort strings. This is currently the place where I can save a lot of time (~70%) if possible. So, which is faster: for($j =...
25
by: Ganesh | last post by:
Hi, This is a question that pertains to pointers in general (C or C++). Which of the following is faster and why? for (int i = 0; i < N; i++) = ... a... (or)
16
by: Brian Tkatch | last post by:
Is there a way to check the order in which SET INTEGRITY needs to be applied? This would be for a script with a dynamic list of TABLEs. B.
6
by: mgcclx | last post by:
For loop and while loop. which one is faster? I see many articles fighting over it and different people come up with different results.
17
by: onkar | last post by:
which one runs faster ?? for(i=0;i<100;i++) for(j=0;j<10;j++) a=0; OR for(j=0;j<10;j++) for(i=0;i<100;i++)
8
by: Sing | last post by:
Dear C Gurus, I would like to optimise a max() algo that my program uses many times. Which one is faster or are they the same? 1. #define max(a,b) (((a) (b)) ? (a) : (b)) 2. if (a>b) return a...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.