This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

which one is faster..?  RSS feed

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


or


 
Zandis Murāns
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
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
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.
 
David Newton
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.
 
Henry Wong
author
Sheriff
Posts: 23283
125
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!