C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
And: At the end of the program, neither the List nor the Dictionary contain any elements.
DictionaryListInfo: The total milliseconds for the removal of all elements is displayed at the program's termination.
Result: The List required over 2 seconds to remove all its elements. The Dictionary required less than 2 ms. It was over 1000 times faster.
C# program that tests Remove methods
using System;
using System.Collections.Generic;
using System.Diagnostics;
class Program
{
const int _max = 100000;
static void Main()
{
var list = new List<int>();
for (int i = 0; i < _max; i++)
list.Add(i);
var dictionary = new Dictionary<int, int>();
for (int i = 0; i < _max; i++)
dictionary.Add(i, i);
var s1 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
list.Remove(i);
s1.Stop();
var s2 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
dictionary.Remove(i);
s2.Stop();
Console.WriteLine(s1.Elapsed.TotalMilliseconds);
Console.WriteLine(s2.Elapsed.TotalMilliseconds);
Console.Read();
}
}
Output
2373.9894 ms List Remove()
1.9896 ms Dictionary Remove()