• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

which one is faster..?

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
noone
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please conclude me which one is faster or both will take the same time
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 13081
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well said.
 
We don't have time to be charming! Quick, read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic