• Post Reply Bookmark Topic Watch Topic
  • New Topic

regarding iteration  RSS feed

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

i get confused when i saw this question from my friend

Which one is faster in java ?

1. for(int i = 100000; i > 0; i�-) {}
2. for(int i = 1; i < 100001; i++) {}

please give me an idea
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is "none".

If you correct the code the compiler will notice the empty code block and ignore both the loops.

But IF we had the same code in


then they would both do the same amount of iterations.

Oh, and btw... with "SCJP1.4 83%" in the signature, you should have been able to solve this...

[ August 29, 2008: Message edited by: Ove Lindstr�m ]
[ August 29, 2008: Message edited by: Ove Lindstr�m ]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ove Lindstr�m:
the compiler will notice the empty code block and ignore both the loops.


Are you sure about that ?

Originally posted by Ove Lindstr�m:

then they would both do the same amount of iterations.


Just because they are both doing the same amount of iterations, doesn't mean they will both take the same amount of time.
I don't know the reason yet, but the decrement loop is faster than the increment loop.
 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RE:
"Oh, and btw... with "SCJP1.4 83%" in the signature, you should have been able to solve this... "

Maybe it was SCJP 1.483%

My view is that optimisations to this level are pointless in the extreme.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joanne Neal:


I don't know the reason yet, but the decrement loop is faster than the increment loop.


Ok, You don't know the reason , But what makes you believe or think so ?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sagar Rohankar:


Ok, You don't know the reason , But what makes you believe or think so ?


I wrote a quick program to test it.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tom Johnson:

My view is that optimisations to this level are pointless in the extreme.[/QB]


Hello sir Tom Johnson, did you check that? my friend found the question form this url:

see here

whether they are fool... ?

and onething i want to say you .. even james gosling may not know some thing in java...

but i think you know in and out of java
 
vanlalhmangaiha khiangte
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If some operation is there to perform
Decrement is quicker


Got the ouput
decrement:1405
increment:1703

If no operation is there to perform


Got the output as :
decrement:0
increment:0

Thanks ...
[ August 29, 2008: Message edited by: vanlalhmangaiha khiangte ]
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmm, Interestingly , I too found decrement loop is faster than that of increment loop ( roughly by 2 or 4 seconds )..

[I do not calculate any mean and consider much sample set , but what said above by Joanne is true]..

but the question is Why and How the difference comes ?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by seetharaman venkatasamy:


Hello sir Tom Johnson, did you check that? my friend found the question form this url:

see here

whether they are fool... ?

and onething i want to say you .. even james gosling may not know some thing in java...

but i think you know in and out of java


That site is just designed to show some unexpected/unusual behaviour in Java. The actual differences between the two forms of the for loop are so insignificant that there are bound to be other areas of your program that could be optimised before worrying about this minor difference.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sagar Rohankar:
hmmm, Interestingly , I too found decrement loop is faster than that of increment loop ( roughly by 2 or 4 seconds )..

[I do not calculate any mean and consider much sample set , but what said above by Joanne is true]..

but the question is Why and How the difference comes ?


I didn't find differences of that significance. Having run both loops a thousand times, the difference was in the range of tens of milliseconds.

If you want to know the reason, you will probably need to look at the bytecode that is generated for each of those loops. There is probably some optimisation that can be done when decrementing that can't be done when incrementing.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes. understand , Thanks Joanne Neal
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops my mistake agian , it miliseconds , not seconds !!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!