Listing 1: Comparable Programs in C++ and Java
//Program.cs using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { RunIt(); DateTime start = DateTime.Now; for (int i = 0; i < 5; i++) { RunIt(); } TimeSpan elapsed = DateTime.Now - start; Console.WriteLine("Elapsed time: {0} ms", elapsed); Console.ReadLine(); } static void RunIt() { List<int>[] n = new List<int>[5]; for (int i = 0; i < n.Length; i++) { n[i] = new List<int>(); } for (int i = 0; i < 1000000; i++) { n[0].Add(1); } for (int i = 1; i < n.Length; i++) { List<int> newArray = n[i]; List<int> oldArray = n[i - 1]; foreach (int j in oldArray) { newArray.Add(oldArray[j] * 2); } } for (int i = 0; i < n.Length; i++) { List<int> array = n[i]; foreach (int j in array) { int number = j; } } } } }
//GenericValueArrayListTest.java import java.util.*; public class GenericValueArrayListTest { public static void main(String[] args) { RunIt(); Date start = new Date(); for(int i = 0; i < 5; i++){ RunIt(); } Date finish = new Date(); System.out.printf("Elapsed time: %d ms", finish.getTime() - start.getTime()); } static void RunIt() { ArrayList<Integer>[] n = new ArrayList[5]; for(int i = 0; i < n.length; i++){ n[i] = new ArrayList<Integer>(); } for (int i = 0; i < 1000000; i++) { n[0].add(1); } for(int i = 1; i < n.length; i++){ ArrayList<Integer> newArray = n[i]; ArrayList<Integer> oldArray = n[i - 1]; for(int j : oldArray){ newArray.add(j * 2); } } for (int i = 0; i < n.length; i++) { ArrayList<Integer> array = n[i]; for(int j : array){ int number = j; } } } }