• 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

which one is faster..?  RSS feed

 
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 Java ME Opera
  • 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
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • 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
Bartender
Posts: 12627
50
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
Rancher
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
Sheriff
Posts: 23589
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well said.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!