Generic List And Non Generic List Performance Comparison After JIT Compilation

The performance of the generic and non-generic lists is well known; however, there is a catch. We should always consider the results that come after the JIT compilation. The difference between the performance after a test run of the code snippet and running the code after JIT does its job may surprise you.

 
 
 
What happens when you run the test application for performance comparison?
  1. When you run the application, JIT (Just in Time) compiler runs.
  2. JIT compiles the application, optimizes the code, and finds the best approach to executes the code.
  3. All these JIT steps are time-consuming which are included in the first execution, so the execution time is always way more than the actual run time
  4. So how can you calculate the actual run time? Simple! To check the performance of your test application with test data, call the code snippet in a loop.
Example
  1. private static void GenericListPerformance() {  
  2.     var genList = new List < int > () {  
  3.         10,  
  4.         100,  
  5.         13,  
  6.         19,  
  7.         14,  
  8.         100,  
  9.         99  
  10.     };  
  11.     var nonGenList = new ArrayList() {  
  12.         10,  
  13.         100,  
  14.         13,  
  15.         19,  
  16.         14,  
  17.         100,  
  18.         99  
  19.     };  
  20.     var stWatch = Stopwatch.StartNew();  
  21.     genList.Sort();  
  22.     stWatch.Stop();  
  23.     Console.WriteLine("Generic List sorting " + stWatch.Elapsed.TotalMilliseconds.ToString());  
  24.     stWatch = Stopwatch.StartNew();  
  25.     nonGenList.Sort();  
  26.     stWatch.Stop();  
  27.     Console.WriteLine("Non Generic List sorting " + stWatch.Elapsed.TotalMilliseconds.ToString());  
  28. }  
Here, I have created a simple method to compare the two lists - ArrayList and List<T>. I have used Stopwatch which is present in the System.Diagnostics namespace of .NET framework and then, printed the messages with time.
  1. static void Main(string[] args) {  
  2.     for (int i = 0; i < 5; i++) {  
  3.         Console.WriteLine(string.Format("<--------Call Number {0} -------->", i));  
  4.         GenericListPerformance();  
  5.         Console.WriteLine();  
  6.     }  
  7. }  
Now, this is where I have used the actual concept. I have called the same method in main, inside a loop which runs 5 times. So, when it runs for the first time, JIT can optimize the code and find the best approach to run this code. Later, JIT will use the native code and give us the best results.
 
Result
 
See the results.
 
 
 
For the first run, the execution time is-
 
For Generic - 0.0263

For Non-Generic – 0.233
 
And later, it came to be like -
 
Generic – 0.0012, 0.0013, 0.0011, 0.0013

Non-Generic – 0.0032, 0.0026, 0.0026, 0.0029
 
 
 
Therefore, we should always consider the JIT time. 
 
That is it. Comment if you have any questions or want me to do some analysis on a topic you want.