To get that out of the way - i am not asking about which platform is faster or try to start a flame war.
Question: Why is the .NET version slower 5x in the first test ? The results i got were:
.NET Test1: 48992696 Test2: 1210070
Java Test1: 9651995 Test2: 1029374
Is this strictly behind list implementations ? If yes, is there something to make list insertion faster on .NET side ?
EDIT:
Used preallocated lists and removed int appends, got this down to
.NET Test1: 11 838 363
Java Test1: 712 481
.NET
namespace TestApp
{
class Program
{
static void Main(string[] args) {
long samples = 100;
long test1Average = 0;
for (int i = 0; i < samples; i++) {
test1Average += runTest1();
}
test1Average = test1Average / samples;
System.Diagnostics.Debug.WriteLine("TEST 1 AVERAGE: " + test1Average);
List<String> list = new List<String>();
for (int i = 0; i < 100000; i++)
{
list.Add("String: " + i);
}
long test2Average = 0;
for (int i = 0; i < samples; i++)
{
test2Average += runTest2(list);
}
test2Average = test2Average / samples;
System.Diagnostics.Debug.WriteLine("TEST 2 AVERAGE: " + test2Average);
}
private static long runTest1()
{
List<String> list = new List<String>();
long time = System.DateTime.Now.Ticks;
for (int i = 0; i < 100000; i++)
{
list.Add("String: " + i);
}
return (System.DateTime.Now.Ticks - time) * 100;
}
private static long runTest2(List<String> list)
{
long time = System.DateTime.Now.Ticks;
foreach (String s in list) {
s.ToString();
}
return (System.DateTime.Now.Ticks - time) * 100;
}
}
}
Java
public class Main {
public static void main(String[] args) {
long samples = 100;
long test1Average = 0;
for (int i = 0; i < samples; i++) {
test1Average += runTest1();
}
test1Average = test1Average / samples;
System.out.println("TEST 1 AVERAGE: "+test1Average);
List<String> list = new ArrayList<String>();
for (int i = 0; i < 100000; i++) {
list.add("String: " + i);
}
long test2Average = 0;
for (int i = 0; i < samples; i++) {
test2Average += runTest2(list);
}
test2Average = test2Average / samples;
System.out.println("TEST 2 AVERAGE: "+test2Average);
}
private static long runTest1() {
long time = System.nanoTime();
List<String> list = new ArrayList<String>();
for (int i = 0; i < 100000; i++) {
list.add("String: " + i);
}
return System.nanoTime()-time;
}
private static long runTest2(List<String> list) {
long time = System.nanoTime();
for (String s : list) {
s.toString();
}
return System.nanoTime()-time;
}
}