• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

For loop performance in windows vs linux

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've ended up doing a simple test on how long it takes Java to do 20 million iterations on a for loop. Simple code:



He's the thing. If I contain this loop inside another loop (say 10 iterations) the following happens.

In Linux the time it takes to do the 20M loop starts at say 25 milliseconds. Then drops to 0 ms after the third or fourth iteration.

Doing the same test on Windows all loops seem to take about the same time (25-30 ms) all the way through the 10 iterations.

Why does the time drop on Linux to practically 0 and not on Windows?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does the inner loop do? Maybe Linux is optimizing it?
 
Gerardo Tasistro
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It does nothing. Just like I showed it. I'll make a long story short. This comes from a php vs ASP .NET shootout regarding performance. There were some discrepancies between the results so I decided to do a test in Java. I'm aware this is a bogus test, but it caught my attention when I ran the same code on Windows and Linux. So I'm intrigued now why this happens.
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make sure you are running the same version of jdk, the same number of bit jvm (32 bit vs 64), and that both are running in either client or server mode. Beyond this I imagine there certainly would be differences in various jvm's, but those might show big differences and are testable.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gerardo Tasistro wrote:I've ended up doing a simple test on how long it takes Java to do 20 million iterations on a for loop. Simple code:



He's the thing. If I contain this loop inside another loop (say 10 iterations) the following happens.

In Linux the time it takes to do the 20M loop starts at say 25 milliseconds. Then drops to 0 ms after the third or fourth iteration.

Doing the same test on Windows all loops seem to take about the same time (25-30 ms) all the way through the 10 iterations.

Why does the time drop on Linux to practically 0 and not on Windows?



How you define the time might well affect the results. In some windows versions, time slices are limited by 16ms slots. Which means, if your code takes 2ms in reality, it might still display the result as 16ms. Not the same case with linux.

Try this with nano time and compare the results. Linux might also be optimizing it in some way that is not known.
 
Gerardo Tasistro
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I ran the tests using nanoseconds. The time it takes is better observed now. Here are the results:

There is a significant time difference:

average :31510736 on Windows with jdk1.6.0_07
average :214105 on Linux with jdk1.6.0_13

It's not the same jdk, but I wonder if such a minor revision could cause such a performance issue. What is still present is the decline in time between the initial iterations on Linux and the later ones. The first three usually take longer and then things converge to a small value. On Windows time consumed remains pretty much the same.


Windows



Linux


 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The comparison would be unfair unless you use the same JDK, Same processor speed, memory, cache.
 
Gerardo Tasistro
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It the same exact machine with dual boot Vista Home Premium and Ubuntu.

The differences in version are just updates and don't influence the overall result. Here is data from a test run using update 7 on Linux (save version as the one used on Windows). The patter persists.

 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make sure both are running in the same mode say client or server. I would run such a test in server mode.

Beyond that I would never expect java code to have the same performance on different operating systems.
 
Gerardo Tasistro
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

steve souza wrote:Make sure both are running in the same mode say client or server. I would run such a test in server mode.



What do you mean server or client mode? I've been running these tests either from the IDE (Eclipse) or the command line.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


the above will use an algorithm to compile to native any frequently exected code. This can make a big difference in performance of some code. To ensure that both are running in server mode pass '-server' .
 
Gerardo Tasistro
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Steve, you nailed it. Using the -server flag improves performance under Windows. Yet it seems to not affect Linux performance.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It may be the default value for the linux box. This is why it can be very hard to compare performance across jvm's and operating systems.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Out of curiosity. How much did "-server" improve performance.
 
Gerardo Tasistro
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using JDK 6 Update 7. All times nanoseconds obtained using System.nanotime();

Without -server
average :31882641 (max time obtained in various runs)
average :30785736 (min time obtained in various runs)

With -server
average :1057542 (max time obtained in various runs)
average :896338 (min time obtained in various runs)


The following is an example of the first iterations of the 200 it runs. Notice how the initial time is about 1/3 the time without -server and it quickly converges to even less time per iteration.

Run:12898427
Run:10425627
Run:1278444
Run:1300934
Run:1069130
Run:958711
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Recently, I had my own 'premature performance is the root of all evil' story related to "-server". I have some code that has to process GiigaBytes of log files, and so I was trying to make it fast. I was doing a number of changes that all added up to minimal performance improvements. It still wasn't fast enough when I remembered the "-server" parameter. Just by adding that one parameter I was able to make my existing code 50% faster.

It is one of the best and easiest performance tips out there. I believe some jvm's default to "-server" and others don't.
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The default for the JVM that comes on my mac is "-client"
 
I think she's lovely. It's this tiny ad that called her crazy:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic