You have prompted me to run the test on various VMs.
Following are conclusions drawn from multiple test runs.
All test runs and compilations are performed on the Windows 2003 platform.
Machine Specification: dual Intel 1 GHz CPUs, 2 GB DDR RAM.
Ratios given are the result on the average of a number of test runs.
Array size was kept constant at size 1024 - this value likely affects the
outcome of the test results.
Running on Sun JVM 1.4.0
Compiled with Sun SDK 1.4.2_02
Conclusion: Arrays.fill to "for loop" performance ratio: 750:938
Compiled with Sun SDK 1.4.0
Conclusion: Arrays.fill to "for loop" performance ratio: 838:573
Running on Sun JVM 1.4.1_03
Compiled with Sun SDK 1.4.2_02
Conclusion: Arrays.fill to "for loop" performance ratio: 1024:833
Compiled with Sun SDK 1.4.1_03
Conclusion: Arrays.fill to "for loop" performance ratio: 1094:1140
Running on Sun JVM 1.4.2_02
Compiled with Sun SDK 1.4.2_02
Conclusion: Arrays.fill to "for loop" performance ratio: 828:953
Running on Sun JVM 1.5.0 beta
Compiled with Sun SDK 1.4.2_02
Conclusion: Arrays.fill to "for loop" performance ratio: 1042:740
Compiled with Sun SDK 1.5.0 beta
Conclusion: Arrays.fill to "for loop" performance ratio: 1047:735
Running on IBM JVM 1.3.1
Compiled with Sun SDK 1.4.2_02
Conclusion: Arrays.fill to "for loop" performance ratio: 359:219
Compiled with IBM SDK 1.3.1
Conclusion: Arrays.fill to "for loop" performance ratio: 359:219
Running on IBM JVM 1.4.1
Compiled with Sun SDK 1.4.2_02
Conclusion: Arrays.fill to "for loop" performance ratio: 359:210
Compiled with IBM SDK 1.4.1
Conclusion: Arrays.fill to "for loop" performance ratio: 360:203
Overall conclusion
The test runs indicate that there is bytecode optimisation performed by
different compilers.
Interestingly, the Arrays.fill performance seemed to improve with later
versions of the JVM with the exception of Sun 1.4.1, but fell when using
1.5.0, however, it must be noted that this JVM is in beta state.
It may even be speculated that any optimisation that is occurring, is yet to
be implemented in the 1.5.0 JVM. The authors of the Sun 1.5.0 JVM will be
consulted regarding these results.
From a maintenance perspective (as opposed to performance), duplicating core
API functionality is generally poor practice, and therefore, the use of
Arrays.fill is recommended.
--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
Andrew Hobbs said:
Hi,
I copy/pasted your code and got the results below. I had to make a couple
of slight changes to allow it to run within the environment I use and to get
several results to check for consistency, but the results seem to be pretty
consistent. One thing that I did notice is that the time difference is a
bit smaller if I am filling many small arrays rather than a few large
arrays, but the for loop still comes out faster.
import java.util.Arrays;
public class Test
{
public static void main(String[] args)
{
for(int k = 0; k < 5; k++) {
byte[] x = new byte[1024];
byte b = 0;
long times = 1000000;
long time1 = System.currentTimeMillis();
for(int i = 0; i < times; i++)
{
Arrays.fill(x, b);
}
long time2 = System.currentTimeMillis();
for(int i = 0; i < times; i++)
{
for(int j = 0; j < x.length; j++)
{
x[j] = b;
}
}
long time3 = System.currentTimeMillis();
System.out.println("Fill time = " + (time2 - time1));
System.out.println("For loop time = " + (time3 - time2));
}
}
}
Fill time = 3625
For loop time = 3375
Fill time = 3672
For loop time = 3375
Fill time = 3640
For loop time = 3391
Fill time = 3625
For loop time = 3391
Fill time = 3672
For loop time = 3468
Cheers
Andrew
--
********************************************************
Andrew Hobbs PhD
MetaSense Pty Ltd -
www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020
61 8 9246 2026
(e-mail address removed)
*********************************************************
Tony Morris said:
Surprisingly, the IBM 1.3.1 and 1.4.1 VM on Windows 2003 executes
Arrays.fill 5-10 times faster.
I don't have access to Sun 1.4.1, I'd be surprised to see the result.
--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)