• 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

why is first version of code slower than the second ?

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First:

Second:





Why is the first line of code slower than the first ?!

Here is the code to test this:



Its output is:
one = 265
two = 266


Why is that ?
 
Marshal
Posts: 7083
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change code places. Put method "two" to the top of your program, and "one" to the bottom.
See what happens then.

Also, try to separate methods to a two different programs.
 
Bartender
Posts: 6135
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a one millisecond difference, that's well within your margin of error.
Use System.nanoTime() for performance testing.
Empty loops have the potential for being optimized away.
For that many loop iterations, would 1 millisecond make a difference? Is that difference worth the possibility of making the code less readable?
You would need to run the test many times to see what the average duration is.
You would need to run one() followed by two() and also two() followed by one().
 
Lucian Whiteman
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:You have a one millisecond difference, that's well within your margin of error.



As I want to learn java, I have no margin of error.

Carey Brown wrote:
For that many loop iterations, would 1 millisecond make a difference?



Yes

Carey Brown wrote:Is that difference worth the possibility of making the code less readable?



No


Where is this difference coming from ? How can I use it in my advantage ?
 
lowercase baba
Posts: 12760
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lucian Whiteman wrote:How can I use it in my advantage ?


You shouldn't even try. First, you have no proof there IS a real difference. Did you follow the suggestions people gave - reversing the order, using nano time, etc.?

Next...what possible situation do you have where one millisecond in this loop makes any difference? Considering you'll probably be doing stuff inside a loop that takes time (disk I/O, network communication, DB access...something), the millisecond here will be lost in all that processing.

It is simply NOT WORTH IT to fret over something as meaningless as this at the cost of making your code harder to read/maintain.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran your code multiple times, sometimes one was faster, sometimes two was faster, and sometimes they were equal. How many times did you test your code?
 
Shiny ad:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!