In this experiment I add these 5 people
Name = "Tom", Age = 32
Name = "John", Age = 22
Name = "Sandra", Age = 36
Name = "Julie", Age = 54
Name = "Samantha", Age = 21
into
1>A Hashtable,
2>A Dictionary<TKey, TValue>,
3>An Array,
4>An ArrayList and
5>A List<T>.
Then I try to find Samantha's age multiple times (Note that Samantha is the last entry) in each of these collections. These were the times taken.
Given below is the code used for the experiment
Name = "Tom", Age = 32
Name = "John", Age = 22
Name = "Sandra", Age = 36
Name = "Julie", Age = 54
Name = "Samantha", Age = 21
into
1>A Hashtable,
2>A Dictionary<TKey, TValue>,
3>An Array,
4>An ArrayList and
5>A List<T>.
Then I try to find Samantha's age multiple times (Note that Samantha is the last entry) in each of these collections. These were the times taken.
Rank
|
Class
|
Time taken for the searches
|
1
|
Dictionary
|
|
2 |
Hashtable
|
|
3
|
Array
|
|
4
|
List
|
|
5
|
ArrayList
|
Given below is the code used for the experiment
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace DictionaryPerformanceComparison
{
internal class Program
{
private static void Main(string[] args)
{
const int searchCount = 99999999;
DateTime begin;
DateTime end;
/*Create
a hash table*/
var hashTable = new Hashtable { { "Tom", 32 }, { "John", 22 }, { "Sandra", 36 }, { "Julie", 54 }, { "Samantha", 21 } };
/*Create
a dictionary*/
var dict = new Dictionary<string, int>
{
{"Tom", 32},
{"John", 22},
{"Sandra", 36},
{"Julie", 54},
{"Samantha", 21}
};
/*Create
an array*/
var pArray = new Person[5];
pArray[0] = new Person { Name = "Tom", Age =
32 };
pArray[1] = new Person { Name = "John", Age =
22 };
pArray[2] = new Person { Name = "Sandra", Age =
36 };
pArray[3] = new Person { Name = "Julie", Age =
54 };
pArray[4] = new Person { Name = "Samantha", Age =
21 };
/*Create
an arraylist */
var arLst = new ArrayList
{
new Person {Name = "Tom", Age =
32},
new Person {Name = "John", Age =
22},
new Person {Name = "Sandra", Age =
36},
new Person {Name = "Julie", Age =
54},
new Person {Name = "Samantha", Age =
21}
};
/*Create
a List*/
var li = new List<Person>
{
new Person {Name = "Tom", Age =
32},
new Person {Name = "John", Age = 22},
new Person {Name = "Sandra", Age =
36},
new Person {Name = "Julie", Age =
54},
new Person {Name = "Samantha", Age =
21}
};
begin = DateTime.Now;
for (int i = 0; i < searchCount; i++)
{
int x = (int)hashTable["Samantha"];
}
end = DateTime.Now;
Console.WriteLine("Hash table
search took {0} seconds",
end.Subtract(begin).TotalSeconds);
begin = DateTime.Now;
for (int i = 0; i < searchCount; i++)
{
int x = dict["Samantha"];
}
end = DateTime.Now;
Console.WriteLine("Dictionary
search took {0} seconds",
end.Subtract(begin).TotalSeconds);
begin = DateTime.Now;
for (int i = 0; i < searchCount; i++)
{
int length = pArray.Length;
for (int j = 0; j < length; j++)
{
if (pArray[j].Name == "Samantha")
{
int age = pArray[j].Age;
}
}
}
end = DateTime.Now;
Console.WriteLine("Array search
took {0}
seconds",
end.Subtract(begin).TotalSeconds);
begin = DateTime.Now;
for (int i = 0; i < searchCount; i++)
{
int x = arLst.OfType<Person>().FirstOrDefault(p => p.Name == "Samantha").Age;
}
end = DateTime.Now;
Console.WriteLine("ArrayList
search took {0} seconds",
end.Subtract(begin).TotalSeconds);
begin = DateTime.Now;
for (int i = 0; i < searchCount; i++)
{
int x = li.FirstOrDefault(p => p.Name == "Samantha").Age;
}
end = DateTime.Now;
Console.WriteLine("List search
took {0}
seconds",
end.Subtract(begin).TotalSeconds);
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
}
thanks!!!
ReplyDeleteCheck this one too....Dictionary Vs Hashtable
DeleteLing
Thanks
ReplyDeleteYou constructed the array in wrong way to get its bad performance.
ReplyDeleteI am not sure what you mean. Could you please elaborate?
DeleteThanks, I had a problem similar to this and I was wondering about performance.
ReplyDeleteIn general;
The comparison between a dictionary and hashtable is useful.
The comparison between an Arraylist and List is also useful.
Other than that, this is information helpful only if you are wanting to use a data structure where the lookup key is a string and you do not have many entries.
When you are going through the array, you are looking at the first three entrees each time to get to Sandra. I suppose you chose that location to represent average lookup when your data set is only 5. If your data set was 1000, sequentially looking through 500 people objects would probably be significantly slower than using a hashing data structure such as a hashtable or dictionary (a hash map).
It might be interesting to see how fast an array is if you used it as it was intended and looked up values rather than iterating through. You could write a simple hash function (get and set) that takes the first and last letters of a string and converts them to an int (char * char = index), then stores them in the proper location in the array. Then look up the items in the array using the function.
It would also be interesting to see this same thing run with a int key lookup to a string value. That is where an array would shine.
this make sanse to me
ReplyDelete