• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

which one is faster..?

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all please tell which one is faster


or


 
Ranch Hand
Posts: 174
Java ME Opera Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
noone
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please conclude me which one is faster or both will take the same time
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there some reason why you don't just try them yourself? Both could actually run in zero time, since they have no side effects and could be completely optimized away.

In any case, it doesn't much matter how fast empty loops go, since you won't be using an empty loop in a program. As soon as your loop contains some code, any microscopic differences in performance will be swamped by the time taken to run the loop contents.
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i heard that decrementation is much faster than increment please give me the reason for this issue
 
lowercase baba
Posts: 12760
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you have to go with one of those two, i'd go with

for(int i=1;i<100001;i++)

simply because it's closer to the commonly used idiom

for(int i=0;i<100000;i++)

READABILITY should always be your primary concern when writing code. since 99% of all programmers would write the loop with ++, they will all understand it easier than --, especially if you're not using a 0-base offset.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A compare to zero is typically faster than comparing to a number--but that can also be JVM/JIT/CPU-dependent.
 
author
Posts: 23840
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

David Newton wrote:A compare to zero is typically faster than comparing to a number--but that can also be JVM/JIT/CPU-dependent.



Slight detour... but topic is interesting ...

IMHO, while I think that this statement is true, I don't think that it is the actual comparison that is faster. At the machine level, comparing a register to a number will take one cycle, regardless of whether the number is zero or not. I think it is the fact that the comparison can actually be optimized out, that makes this faster.

Typically, the generated code should be something like a comparison followed by a jump based on how the comparison set the flags. However, non-compare instructions can also set the flags -- for example, loading a zero into a register can set the zero flag. So, it is possible to just get rid of the compare, and do the jump if zero (or not zero), right after loading the value into the register.

Henry



 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well said.
 
It runs on an internal combustion engine. This ad does not:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!