Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Collections;
- using System.Diagnostics;
- namespace SpeedTest
- {
- class Program
- {
- static void Main(string[] args)
- {
- Stopwatch stopwatch = new Stopwatch();
- int testLength = 10000;
- int testIterations = 5000;
- var results = new Dictionary<string, float>();
- // initialise the collections. pre-filling with zeros is not part of the test (but takes a long time)
- int[] intArray = new int[testLength];
- ArrayList intArrayList = new ArrayList(testLength);
- for (int n = 0; n < testLength; ++n) { intArrayList.Add(0); }
- List<int> intList = new List<int>(testLength);
- for (int n = 0; n < testLength; ++n) { intList.Add(0); }
- int a = 0;
- // ----------------- int[] ----------------------
- stopwatch.Reset();
- stopwatch.Start();
- for (int i = 0; i < testIterations; ++i)
- {
- for (int n = 0; n < intArray.Length; ++n)
- {
- intArray[n] = n;
- intArray[n]++;
- a = intArray[n];
- }
- if (i == 0) { stopwatch.Reset(); stopwatch.Start(); } // discard 1st iteration to avoid hiccups
- }
- stopwatch.Stop();
- Console.WriteLine("done " + intArray.Length + " on intArray");
- results.Add("Direct Length int[]", stopwatch.ElapsedTicks);
- // ----------------- List<int> ----------------------
- stopwatch.Reset();
- stopwatch.Start();
- for (int i = 0; i < testIterations; ++i)
- {
- for (int n = 0; n < intList.Count; ++n)
- {
- intList[n] = n;
- intList[n]++;
- a = intList[n];
- }
- if (i == 0) { stopwatch.Reset(); stopwatch.Start(); } // discard 1st iteration to avoid hiccups
- }
- stopwatch.Stop();
- Console.WriteLine("done " + intList.Count + " on intList");
- results.Add("Direct Length List<int>", stopwatch.ElapsedTicks);
- // ----------------- ArrayList ----------------------
- stopwatch.Reset();
- stopwatch.Start();
- for (int i = 0; i < testIterations; ++i)
- {
- for (int n = 0; n < intArrayList.Count; ++n)
- {
- intArrayList[n] = n;
- intArrayList[n] = ((int)intArrayList[n]) + 1;
- a = (int)intArrayList[n];
- }
- if (i == 0) { stopwatch.Reset(); stopwatch.Start(); } // discard 1st iteration to avoid hiccups
- }
- stopwatch.Stop();
- Console.WriteLine("done " + intArrayList.Count + " on intArrayList");
- results.Add("Direct Length ArrayList", stopwatch.ElapsedTicks);
- // ----------------- and now, without bounds check optimisation
- // ----------------- int[] ----------------------
- stopwatch.Reset();
- stopwatch.Start();
- for (int i = 0; i < testIterations; ++i)
- {
- for (int n = 0; n < testLength; ++n)
- {
- intArray[n] = n;
- intArray[n]++;
- a = intArray[n];
- }
- if (i == 0) { stopwatch.Reset(); stopwatch.Start(); } // discard 1st iteration to avoid hiccups
- }
- stopwatch.Stop();
- Console.WriteLine("done " + intArray.Length + " on intArray");
- results.Add("Cached Length int[]", stopwatch.ElapsedTicks);
- // ----------------- List<int> ----------------------
- stopwatch.Reset();
- stopwatch.Start();
- for (int i = 0; i < testIterations; ++i)
- {
- for (int n = 0; n < testLength; ++n)
- {
- intList[n] = n;
- intList[n]++;
- a = intList[n];
- }
- if (i == 0) { stopwatch.Reset(); stopwatch.Start(); } // discard 1st iteration to avoid hiccups
- }
- stopwatch.Stop();
- Console.WriteLine("done " + intList.Count + " on intList");
- results.Add("Cached Length List<int>", stopwatch.ElapsedTicks);
- // ----------------- ArrayList ----------------------
- stopwatch.Reset();
- stopwatch.Start();
- for (int i = 0; i < testIterations; ++i)
- {
- for (int n = 0; n < testLength; ++n)
- {
- intArrayList[n] = n;
- intArrayList[n] = ((int)intArrayList[n]) + 1;
- a = (int)intArrayList[n];
- }
- if (i == 0) { stopwatch.Reset(); stopwatch.Start(); } // discard 1st iteration to avoid hiccups
- }
- stopwatch.Stop();
- Console.WriteLine("done " + intArrayList.Count + " on intArrayList");
- results.Add("Cached Length ArrayList", stopwatch.ElapsedTicks);
- // ------------------ Results ---------------------
- string resultText = "Results: \n";
- foreach (var result in results)
- {
- resultText += result.Key + ": " + ((result.Value / System.TimeSpan.TicksPerSecond) * 1000).ToString("0") + "ms \n";
- }
- Console.WriteLine(resultText);
- // prevent compiler from knowing 'a' is never used!
- Console.WriteLine(a);
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment