aspose file tools*
The moose likes Performance and the fly likes For loop performance in windows vs linux Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "For loop performance in windows vs linux" Watch "For loop performance in windows vs linux" New topic
Author

For loop performance in windows vs linux

Gerardo Tasistro
Ranch Hand

Joined: Feb 08, 2005
Posts: 362
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?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30309
    
150

What does the inner loop do? Maybe Linux is optimizing it?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Gerardo Tasistro
Ranch Hand

Joined: Feb 08, 2005
Posts: 362
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.
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 860
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.

http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

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.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Gerardo Tasistro
Ranch Hand

Joined: Feb 08, 2005
Posts: 362
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

Joined: Feb 24, 2006
Posts: 6661
    
    5

The comparison would be unfair unless you use the same JDK, Same processor speed, memory, cache.
Gerardo Tasistro
Ranch Hand

Joined: Feb 08, 2005
Posts: 362
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

Joined: Jun 26, 2002
Posts: 860
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

Joined: Feb 08, 2005
Posts: 362
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

Joined: Jun 26, 2002
Posts: 860


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

Joined: Feb 08, 2005
Posts: 362
Steve, you nailed it. Using the -server flag improves performance under Windows. Yet it seems to not affect Linux performance.
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 860
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

Joined: Jun 26, 2002
Posts: 860
Out of curiosity. How much did "-server" improve performance.
Gerardo Tasistro
Ranch Hand

Joined: Feb 08, 2005
Posts: 362
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

Joined: Jun 26, 2002
Posts: 860
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

Joined: Jun 26, 2002
Posts: 860
The default for the JVM that comes on my mac is "-client"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: For loop performance in windows vs linux