C#/Mono results:
Primitive test n=1000000 completed in 0.2528684 seconds
ArrayList test n=1000000 completed in 1.0193116 seconds
Quick + Dirty Interpretation:
C# int[] is dramatically faster than List<int>, even with the lack of
boxing. In this one-off benchmark, List<int> took ~4x the time.
Also interesting, Java int[] ran much faster than C# int[]. I assume the
Linux JDK is simply faster than the Mono runtime for array processing.
As expected, C# List<int> ran significantly faster than Java List<Integer>
presumably due to boxing issues. The Java version took ~1.8x the time.
I can also benchmark Scala again if anyone wants (I've done this before).
Array[int] matches Java int[] performance and gives you full language
integration and consistency. In other words, the best of both worlds.
Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace CollectionsTest
{
public delegate void Runnable();
public class ArrayTest
{
public static void benchmark(String name, Runnable runnable) {
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
runnable();
stopWatch.Stop();
Console.WriteLine("{0} completed in {1} seconds", name,
stopWatch.Elapsed.TotalSeconds);
}
public static void Main(string[] args) {
const int n = 1000000;
testWithPrimitiveArray(n);
testWithArrayList(n);
}
public static void testWithPrimitiveArray(int n) {
benchmark(String.Format("Primitive test n={0}", n), () => {
int[] a = new int[n];
// Do something with the array...
for (int i = 0; i < n; i++) {
a[i] = i;
}
for (int k = 0; k < 100; k++) {
for (int i = 0; i < n; i++) {
a[i] = a[n-1-i] * 2;
}
}
});
}
public static void testWithArrayList(int n) {
benchmark(String.Format("ArrayList test n={0}", n), () => {
List<int> a = new List<int>(n);
// Do something with the array...
for (int i = 0; i < n; i++) {
a.Add (i);
}
for (int k = 0; k < 100; k++) {
for (int i = 0; i < n; i++) {
a[i] = a[n-1-i] * 2;
}
}
});
}
}
}
>
>
--
You received this message because you are subscribed to the Google Groups "Java
Posse" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/javaposse/-/dfyH85uh0HcJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en.