{"id":292,"date":"2023-08-23T06:08:38","date_gmt":"2023-08-23T05:08:38","guid":{"rendered":"https:\/\/ishooscode.com\/?p=292"},"modified":"2023-09-04T06:06:40","modified_gmt":"2023-09-04T05:06:40","slug":"comparing-performance-of-lists","status":"publish","type":"post","link":"https:\/\/ishooscode.com\/index.php\/2023\/08\/23\/comparing-performance-of-lists\/","title":{"rendered":"IL code performance of Generic List And Non Generic List"},"content":{"rendered":"\n<p>You will never see your code like before once you understand JIT. In this article we will demonstrate how JIT generates IL code, by comparing performance of List and generics by demonstrating how JIT generated IL code is created and its use.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What happens when you run the test application for performance comparison?<\/strong><\/h2>\n\n\n\n<ol>\n<li>When you run the application, JIT (Just in Time) compiler runs.<\/li>\n\n\n\n<li>JIT compiles the application, optimizes the code, and finds the best approach to executes the code.<\/li>\n\n\n\n<li>All these JIT steps are time-consuming which are included in the first execution, so the first execution time is always higher than the actual run time<\/li>\n\n\n\n<li>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.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Example code<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>private static void GenericListPerformance() {\n    var genList = new List &lt; int &gt; () {\n        10,\n        100,\n        13,\n        19,\n        14,\n        100,\n        99\n    };\n    var nonGenList = new ArrayList() {\n        10,\n        100,\n        13,\n        19,\n        14,\n        100,\n        99\n    };\n    var stWatch = Stopwatch.StartNew();\n    genList.Sort();\n    stWatch.Stop();\n    Console.WriteLine(\"Generic List sorting \" + stWatch.Elapsed.TotalMilliseconds.ToString());\n    stWatch = Stopwatch.StartNew();\n    nonGenList.Sort();\n    stWatch.Stop();\n    Console.WriteLine(\"Non Generic List sorting \" + stWatch.Elapsed.TotalMilliseconds.ToString());\n}<\/code><\/pre>\n\n\n\n<p>Here, I have created a simple method to compare the two lists &#8211; ArrayList and List&lt;T&gt;. I have used Stopwatch which is present in the System.Diagnostics namespace of .NET framework and then, printed the messages with time.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static void Main(string&#91;] args) {\n    for (int i = 0; i &lt; 5; i++) {\n        Console.WriteLine(string.Format(\"&lt;--------Call Number {0} --------&gt;\", i));\n        GenericListPerformance();\n        Console.WriteLine();\n    }\n}<\/code><\/pre>\n\n\n\n<p>Now, here I have used the actual concept. I called the same method in main, inside a loop. 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Result<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"601\" height=\"546\" src=\"https:\/\/ishooscode.com\/wp-content\/uploads\/2023\/08\/Result.png\" alt=\"Comparing performance of Lists\" class=\"wp-image-293\" srcset=\"https:\/\/ishooscode.com\/wp-content\/uploads\/2023\/08\/Result.png 601w, https:\/\/ishooscode.com\/wp-content\/uploads\/2023\/08\/Result-300x273.png 300w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">performance of List<\/h2>\n\n\n\n<p>The first run, the execution time is-<\/p>\n\n\n\n<p>For Generic &#8211; 0.0263<\/p>\n\n\n\n<p>For Non-Generic \u2013 0.233<\/p>\n\n\n\n<p>And later, it came to be like &#8211;<\/p>\n\n\n\n<p>Generic \u2013 0.0012, 0.0013, 0.0011, 0.0013<\/p>\n\n\n\n<p>Non-Generic \u2013 0.0032, 0.0026, 0.0026, 0.0029<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"198\" height=\"232\" src=\"https:\/\/ishooscode.com\/wp-content\/uploads\/2023\/08\/Awestruck.png\" alt=\"\" class=\"wp-image-294\"\/><\/figure><\/div>\n\n\n<p>Therefore, we should always consider the JIT time.&nbsp;<\/p>\n\n\n\n<p>That is it. Comment if you have any questions or want me to do some analysis on a topic you want.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">More like this<\/h2>\n\n\n\n<ul id=\"block-ca01eeb3-ac94-4994-8396-22c35c9761f8\">\n<li><a href=\"https:\/\/ishooscode.com\/index.php\/2023\/07\/30\/how-does-a-dictionary-maintains-data\/\">How does a dictionary maintains data<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/ishooscode.com\/index.php\/2023\/07\/12\/enumerator-or-foreach\/\">What is better between Enumerator or foreach<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/ishooscode.com\/index.php\/2023\/07\/07\/access-private-member\/\">How to access private member of class<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/ishooscode.com\/index.php\/2023\/07\/03\/service-bus-queue-and-topic\/\">Better make use of Service bus Queue and Topic<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/ishooscode.com\/index.php\/2023\/06\/27\/test-azure-service-bus-trigger\/\">How to trigger azure service bus trigger functions from postman<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>You will never see your code like before once you understand JIT. In this article&#8230;<\/p>\n","protected":false},"author":1,"featured_media":295,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[22,56],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/posts\/292"}],"collection":[{"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/comments?post=292"}],"version-history":[{"count":3,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/posts\/292\/revisions"}],"predecessor-version":[{"id":303,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/posts\/292\/revisions\/303"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/media\/295"}],"wp:attachment":[{"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/media?parent=292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/categories?post=292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ishooscode.com\/index.php\/wp-json\/wp\/v2\/tags?post=292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}