Thursday, March 18, 2010

C# Generic Dictionary retrieval performance benchmarks

C# Dictionary select performance benchmarks

These benchmarks are testing a Dictionary <int,string> and a  Dictionary<IUserKey, string>

In theory retrieval on a Hash Table (which is the data container inside each dictionary) should be constant regardless of number of items in the Hash Table.
The reason being is that accessing a hash table is a random access of an array element  which of the key is generated by a Hashing algorithm.
1-      Dictionary.GetItem(key)
2-      f(Key) à indexValue: random access index of value. F is the hash algorithm.
3-      Dictionary.hashtable[indexValue]

However, The problem is that the hash algorithm is not generating a unique index all the time, when it doesn’t it a collision event!
Collisions are the culprit of decrease in hash table item retrievals (long story short)

In these set of tests I am examining the different items added to dictionary an int an object and a Object with specified HashCode algorithm.

DictionaryPerfTest10Mil1Mil means that Test was ran over a dictionary filled with 10 million items and it was queried 1 million times


Int dictionary-  

  private static void IntDictPerf(int fillSize, int selectionSize)
        {
            Random random = new Random();
            Dictionary<int, string> dictHash = new Dictionary<int, string>(fillSize);

            try
            {
                for (int i = 0; i < fillSize; i++)
                {
                    dictHash.Add(i, "value" + i.ToString());
                }
            }
            catch (Exception )
            {
                System.Diagnostics.Debug.WriteLine("Collision occured");
            }

            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();
            for (int i = 0; i < selectionSize; i++)
            {
                int index = random.Next(fillSize);
                var x = dictHash[index];
            }

            stopWatch.Stop();

            TimeSpan ts = stopWatch.Elapsed;

            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
            System.Diagnostics.Debug.WriteLine(elapsedTime, "RunTime");
        }

Result:
Under 200K items fetch time is almost constant above 300K to 500K it doubles and above 500K it triples.

1st Run:
IntDictionaryPerfTest10Mil1Mil - RunTime: 00:00:00.27
IntDictionaryPerfTest1Mil1Mil - RunTime: 00:00:00.20
IntDictionaryPerfTest500K1Mil - RunTime: 00:00:00.15
IntDictionaryPerfTest300K1Mil - RunTime: 00:00:00.11
IntDictionaryPerfTest200K1Mil - RunTime: 00:00:00.06
IntDictionaryPerfTest100K1Mil - RunTime: 00:00:00.06
IntDictionaryPerfTest10K1Mil - RunTime: 00:00:00.05
IntDictionaryPerfTest1K1Mil - RunTime: 00:00:00.04

2nd Run:
IntDictionaryPerfTest10Mil1Mil - RunTime: 00:00:00.27
IntDictionaryPerfTest500K1Mil - RunTime: 00:00:00.15
IntDictionaryPerfTest300K1Mil - RunTime: 00:00:00.09
IntDictionaryPerfTest100K1Mil - RunTime: 00:00:00.05
IntDictionaryPerfTest10K1Mil - RunTime: 00:00:00.05
IntDictionaryPerfTest1K1Mil - RunTime: 00:00:00.04

3rd Run:
IntDictionaryPerfTest10Mil1Mil - RunTime: 00:00:00.27
IntDictionaryPerfTest1Mil1Mil - RunTime: 00:00:00.21
IntDictionaryPerfTest500K1Mil - RunTime: 00:00:00.16
IntDictionaryPerfTest100K1Mil - RunTime: 00:00:00.05
IntDictionaryPerfTest10K1Mil - RunTime: 00:00:00.05

4th Run:
DictionaryPerfTest10Mil1Mil - RunTime: 00:00:00.27
DictionaryPerfTest1Mil1Mil - RunTime: 00:00:00.20
DictionaryPerfTest100K1Mil - RunTime: 00:00:00.05
DictionaryPerfTest10K1Mil - RunTime: 00:00:00.05

5th Run:
DictionaryPerfTest10Mil1Mil - RunTime: 00:00:00.27
DictionaryPerfTest100K1Mil - RunTime: 00:00:00.05
DictionaryPerfTest1Mil1Mil - RunTime: 00:00:00.21
DictionaryPerfTest10K1Mil - RunTime: 00:00:00.05

6th Run:
DictionaryPerfTest10Mil1Mil - RunTime: 00:00:00.27
DictionaryPerfTest1Mil1Mil - RunTime: 00:00:00.21
DictionaryPerfTest100K1Mil - RunTime: 00:00:00.05
DictionaryPerfTest10K1Mil - RunTime: 00:00:00.05
DictionaryPerfTest1K1Mil - RunTime: 00:00:00.04

User Key struct - Default hashcode (GetHashCode())

public interface IUserKey
    {
        int ID { get; set; }
    }

    public struct UserKey : IUserKey
    {
        public int ID
        {
            get;
            set;
        }

       // public override int GetHashCode()
        //{
        //    return this.ID.GetHashCode();
        //}
    }

private static void UserKeyDictPerf(int fillSize, int selectionSize)
        {
            Random random = new Random();
            Dictionary<IUserKey, string> dictHash = new Dictionary<IUserKey, string>(fillSize);

            try
            {
                for (int i = 0; i < fillSize; i++)
                {
                    IUserKey usr = new UserKey() { ID=i };
                    dictHash.Add(usr, "value" + i.ToString());
                }
            }
            catch (Exception )
            {
                System.Diagnostics.Debug.WriteLine("Collision occured");
            }

            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();
            for (int i = 0; i < selectionSize; i++)
            {
                //int index = random.Next(fillSize);
                IUserKey usr = new UserKey() { ID = random.Next(fillSize) };
                var x = dictHash[usr];
            }

            stopWatch.Stop();

            TimeSpan ts = stopWatch.Elapsed;

            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
            System.Diagnostics.Debug.WriteLine(elapsedTime, "RunTime");
        }


DictionaryPerfTest10Mil1Mil - RunTime: 00:00:00.80
DictionaryPerfTest1Mil1Mil - RunTime: 00:00:00.47
DictionaryPerfTest100K1Mil - RunTime: 00:00:00.29
DictionaryPerfTest10K1Mil - RunTime: 00:00:00.16
DictionaryPerfTest1K1Mil - RunTime: 00:00:00.14

User Key struct - Overriden hash code

Same as the above test with the exception  of defining the HashCode method
    public struct UserKey : IUserKey
    {
        public int ID
        {
            get;
            set;
        }

        public override int GetHashCode()
        {
            return this.ID.GetHashCode();
        }
    }

DictionaryPerfTest10Mil1Mil - RunTime: 00:00:00.78
DictionaryPerfTest1Mil1Mil - RunTime: 00:00:00.48
DictionaryPerfTest100K1Mil - RunTime: 00:00:00.30
DictionaryPerfTest10K1Mil - RunTime: 00:00:00.17
DictionaryPerfTest1K1Mil - RunTime: 00:00:00.15

User Lo Key struct –

UserLoKeyDictionaryPerfTest10Mil1Mil - RunTime: 00:00:00.79
UserLoKeyDictionaryPerfTest1Mil1Mil - RunTime: 00:00:00.48
UserLoKeyDictionaryPerfTest100K1Mil - RunTime: 00:00:00.28
UserLoKeyDictionaryPerfTest10K1Mil - RunTime: 00:00:00.16
UserLoKeyDictionaryPerfTest1K1Mil - RunTime: 00:00:00.15


Hosting Options for WCF services


Hosting Options

a.    IIS 7 (WAS)
b.    Windows Service



Choosing a Hosting Environment
The following table summarizes some of the key benefits and scenarios associated with each of the hosting options.
Hosting Environment
Common Scenarios
Key Benefits and Limitations
Managed Application ("Self-Hosted")
·         Console applications used during development.
·         Rich WinForm and WPF client applications accessing services.
·         Flexible.
·         Easy to deploy.
·         Not an enterprise solution for services.
Windows Services (formerly known as NT services)
·         A long-running WCF service hosted outside of IIS.
·         Service process lifetime controlled by the operating system, not message-activated.
·         Supported by all versions of Windows.
·         Secure environment.
IIS 5.1, IIS 6.0
·         Running a WCF service side-by-side with ASP.NET content on the Internet using the HTTP protocol.
·         Process recycling.
·         Idle shutdown.
·         Process health monitoring.
·         Message-based activation.
·         HTTP only.
Windows Process Activation Service (WAS)
·         Running a WCF service without installing IIS on the Internet using various transport protocols.
·         IIS is not required.
·         Process recycling.
·         Idle shutdown.
·         Process health monitoring.
·         Message-based activation.
·         Works with HTTP, TCP, named pipes, and MSMQ.
IIS 7.0
·         Running a WCF service with ASP.NET content.
·         Running a WCF service on the Internet using various transport protocols.
·         WAS benefits.
·         Integrated with ASP.NET and IIS content.

The choice of a hosting environment depends on the version of Windows on which it is deployed, the transports it requires to send messages and the type of process and application domain recycling it requires. The following table summarizes the data related to these requirements.

Hosting Environment
Platform Availability
Transports Supported
Process and AppDomain Recycling
Managed Applications ("Self-Hosted")
Windows XP, Windows Server 2003, Windows Vista,
Windows Server 2008
HTTP,
net.tcp,
net.pipe,
net.msmq
No
Windows Services (formerly known as NT services)
Windows XP, Windows Server 2003, Windows Vista,
Windows Server 2008
HTTP,
net.tcp,
net.pipe,
net.msmq
No
IIS 5.1
Windows XP
HTTP
Yes
IIS 6.0
Windows Server 2003
HTTP
Yes
Windows Process Activation Service (WAS)
Windows Vista, Windows Server 2008
HTTP,
net.tcp,
net.pipe,
net.msmq
Yes

It is important to note that running a service or any extension from an untrusted host compromises security. Also, note that when opening a ServiceHost under impersonation, an application must ensure that the user is not logged off, for example by caching the WindowsIdentity of the user.

See Also



Hosting Options References :




Scrum – Agile methodology (High level notes)

Scrum – Agile methodology (mentioned 1986- implemented 1993)

-          Start with a living back log owned by product owner
o   Very high level in terms of days
o   Contains features and bugs
o   Breaks sown to tasks : in terms of hours
o   Prioritize them
-          Daily scrum: Less than 15 minutes!
o   What is it that I have done
o   What is it that is in my way
o   What is it that I need to do before next meeting
-          Sprints : 2-3 weeks
-          Demo at the end of each sprint

-          Collective code ownership

-          Collective task ownership

-          Scrum master

o   handles all the road blocks (hardware and etc …)

o   not necessarily the team lead

Helpful Windows XP/2000 Commands & Tools

Found this is an interesting published/unpublished list of windows commands/tools which could be useful in your day to day activities.

1-     at (windows XP/2000)
Scheduling utility.
2-     bootcfg (XP only)
This utility allows you to set up your boot options, such as your default OS and other loading options.
3-     cacls (XP, 2000, & NT4.0)
Changes the ACLs (security Settings) of files and folders. Very similar to chmod in Linux. 
4-     comp (XP & 2000)
This utility is very similar to diff in Linux.  Use the /? switch to get examples of command usage.
5-     contig (works with NT4.0 and newer)
A great defrag utility for NTFS partitions.
6-     control (XP only) - unpublished!
Allows you to launch control panel applets from the command line. 
control userpasswords2, for example will launch a helpful local user admin utility.
7-     defrag (XP only - NT4.0 and Win2k use contig)
Yes, XP comes with a command line disk defrag utility. If you are running Win2k or NT4.0 there is still hope. Contig is a free defrag program that I describe on the defrag page.
8-     diskpart (XP only)
Use this command to manage your disk partitions.  This is the text version for the GUI Disk Manager.
9-     driverquery (XP only)
Produces a list of drivers, their properties, and their versions. Great for computer documentation.
10-  eudcedit (XP only) - unpublished!
Private Character editor.  Yes with this program built into Windows XP you can create your own font!
11-  findstr
Find String - similar to Linux's Grep.
12-  fsutil (XP only) - unpublished!
This is a utility with a lot of capability.  Come back soon for great examples.
13-  getmac (XP & 2000)
This command gets the Media Access Control (MAC) address of your network cards.
14-  gpresult (XP & 2000)
This generates a summary of the user settings and computer group policy settings.
15-  gpupdate (XP only)
Use this utility to manually apply computer and user policy from your windows 2000 (or newer) domain.
16-  ipconfig (XP, 2000 & NT4.0)
This handy tool displays IP settings of the current computer and much more.
17-  MMC (XP, 2000 & NT4.0) - Microsoft Management Console
This is the master tool for Windows, it is the main interface in which all other tools use starting primarily in Windows 2000 and newer systems.
18-  more
Utility used to display text output one screen at a time. Ex. more c:\windows\win.ini
19-  msconfig (XP only)
The ultimate tool to change the services and utilities that start when your Windows machine boots up. You can also copy the executable from XP and use it in Win2k.
20-  msinfo32 (XP &smp; 2000)
An awesome diagnostic tool. With it you can get a list of running processes, including the residing path of the executable (great for manually removing malware) and get detailed information about hardware and system diagnostics.
21-  narrator (XP only)
Turns on the system narrator (can also be found in accessibility options in control panel).  Will will allow your computer to dictate text to you.
22-  netsh (XP & 2000)
A network configuration tool console.  At the 'netsh>' prompt, use the '?' to list the available commands and type "exit" to get back to a command prompt.
23-  netstat (XP)
A local network port tool - try netstat -ano.
24-  nslookup (all)
A DNS name resolution tool.
25-  openfiles (XP Only)
Allows an administrator to display or disconnect open files in XP professional. Type "openfiles /?" for a list of possible parameters.
26-  Pathping (XP & 2000)
A cross between the ping and traceroute utilities. Who needs Neotrace when you can use this? Type "pathping <ip address>" and watch it go.
27-  recover (XP & 2000)
This command can recover readable information from a damaged disk and is very easy to use.
28-  reg (XP & 2000)
A console registry tool, great for scripting Registry edits.
29-  sc (XP & 2000)
A command line utility called the Service Controller.  A power tool to make service changes via a logon/logoff or startup/shutdown script.
30-  schtasks (XP only)
A newer version of the AT command.  This allows an administrator to schedule and manage scheduled tasks on a local and remote machines.
31-  secedit (XP & 2000)
Use this utility to manually apply computer and user policy from your windows 2000 (or newer) domain.  Example to update the machine policy: secedit /refreshpolicy machine_policy /enforce
To view help on this, just type secedit.
NOTE: In Windows XP SP1 and news, this command is superceded by: gpupdate /force
32-  sfc (XP & 2000)
The system file checker scans important system files and replaces the ones you (or your applications) hacked beyond repair with the real, official Microsoft versions.
33-  shutdown (XP & 2000)
With this tool, You can shut down or restart your own computer, or an administrator can shut down or restart a remote computer.
34-  sigverif (XP only)
Microsoft has created driver signatures. A signed driver is Microsoft tested and approved. With the sigverif tool you can have all driver files analyzed to verify that they are digitally signed. Just type 'sigverif' at the command prompt.
35-  systeminfo (XP only)
Basic system configuration information, such as the system type, the processor type, time zone, virtual memory settings, system uptime, and much more.  This program is great for creating an inventory of computers on your network.
36-  sysedit (XP/2000)
System Configuration File Editor.  An old tool that was very handy for the Windows 9X days.  msconfig is what you want to use now.
37-  tasklist (XP pro only)
Tasklist is the command console equivalent to the task manager in windows. It is a must have when fighting scumware and viruses. Try the command:
tasklist /svc
to view the memory resources your services take up.
38-  taskkill (XP only)
Taskkill contains the rest of the task manager functionality.  It allows you to kill those unneeded or locked up applications.
39-  tree (XP & 2000)
An amazing experience everyone should try!  This command will provide a 'family tree' style display of the drive/folder you specify.
40-  WMIC (XP & 2000)
Windows Management Instrumentation Command tool.  This allows you to pull an amazing amount of low-level system information from a command line scripting interface.