{"id":2216,"date":"2019-12-09T11:45:00","date_gmt":"2019-12-09T10:45:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/lavanack\/?p=2136"},"modified":"2023-05-04T12:17:24","modified_gmt":"2023-05-04T10:17:24","slug":"powershell-array-vs-arraylist-performance","status":"publish","type":"post","link":"https:\/\/laurentvanacker.com\/index.php\/2019\/12\/09\/powershell-array-vs-arraylist-performance\/","title":{"rendered":"PowerShell : Array vs. ArrayList Performance."},"content":{"rendered":"<p><a href=\"#en-US\">English version<\/a><\/p>\n<p>[MAJ : 04\/05\/2023] En f\u00e9vrier 2017 je <a href=\"https:\/\/twitter.com\/lavanack\/status\/832245707218231297\" target=\"_blank\" rel=\"noopener noreferrer\">retwittais<\/a> cet <a href=\"https:\/\/stealthfield.wordpress.com\/2015\/08\/21\/powershell-array-vs-arraylist-performance\/\" target=\"_blank\" rel=\"noopener noreferrer\">article<\/a> : <a href=\"https:\/\/stealthfield.wordpress.com\/2015\/08\/21\/powershell-array-vs-arraylist-performance\/\" target=\"_blank\" rel=\"noopener noreferrer\">PowerShell \u2013 Array vs. ArrayList Performance<\/a> (dont je ne suis pas l\u2019auteur). Cet article \u00e9tablissait une comparaison sur la performance (en termes de temps d\u2019ex\u00e9cution) lors de l\u2019ajout de 100 000 \u00e9l\u00e9ments dans un tableau PowerShell en comparaison avec la m\u00eame op\u00e9ration dans une <a href=\"https:\/\/docs.microsoft.com\/fr-fr\/dotnet\/api\/system.collections.arraylist?view=netframework-4.8\">ArrayList<\/a> (object .Net : <a href=\"https:\/\/docs.microsoft.com\/fr-fr\/dotnet\/api\/system.collections.arraylist?view=netframework-4.8\">System.Collections.ArrayList<\/a>). Le r\u00e9sultat \u00e9tant largement en faveur de la <a href=\"https:\/\/docs.microsoft.com\/fr-fr\/dotnet\/api\/system.collections.arraylist?view=netframework-4.8\">ArrayList<\/a> : En effet les tableaux sont des objets de taille fixe et le fait de rajouter un \u00e9l\u00e9ment dans un tableau \u00e9quivaut \u00e0 recr\u00e9er un nouveau tableau de taille n+1 et de recopier le contenu existant dans cette nouvelle structure (Autant dire que c\u2019est une op\u00e9ration co\u00fbteuse).<\/p>\n<p>Je ne suis donc demand\u00e9 \u00e0 quel point la performance d\u00e9croit (d\u2019un point de vue des tableaux PowerShell) en fonction du nombre des \u00e9l\u00e9ments \u00e0 ajouter. J\u2019ai donc cr\u00e9\u00e9 un petit <a href=\"https:\/\/github.com\/lavanack\/laurentvanacker.com\/blob\/master\/Windows%20Powershell\/Misc\/ArrayVsArrayList.ps1\">script<\/a> PowerShell qui ajoute jusqu\u2019\u00e0 100 000 \u00e9l\u00e9ments dans un tableau (en partant de 0, 1 par 1 et par palier de 100 pour chaque it\u00e9ration: 0-100, 0-200 jusque 0-100 000). De mani\u00e8re \u00e0 ne pas perturber le <a href=\"https:\/\/github.com\/lavanack\/laurentvanacker.com\/blob\/master\/Windows%20Powershell\/Misc\/ArrayVsArrayList.ps1\">script<\/a> je l\u2019ai lanc\u00e9 sur une VM <a href=\"http:\/\/portal.azure.com\" target=\"_blank\" rel=\"noopener noreferrer\">Azure<\/a> vierge (Standard DS1 v2 &#8211; 1 vcpus, 3.5 GiB memory).<\/p>\n<p>Je vous laisse juge des r\u00e9sultats ci-dessous (Le fichier Excel des r\u00e9sultats est \u00e9galement disponible <a href=\"https:\/\/github.com\/lavanack\/laurentvanacker.com\/blob\/master\/Windows%20Powershell\/Misc\/ArrayVsArrayList.xlsx\" target=\"_blank\" rel=\"noopener noreferrer\">ici<\/a>).<\/p>\n<h4 align=\"center\"><strong>Comparaison des temps d\u2019ex\u00e9cution<\/strong><\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2683 aligncenter\" src=\"https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/ArrayVsArrayListComp.jpg\" alt=\"\" width=\"1146\" height=\"559\" srcset=\"https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/ArrayVsArrayListComp.jpg 1146w, https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/ArrayVsArrayListComp-300x146.jpg 300w, https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/ArrayVsArrayListComp-1024x499.jpg 1024w, https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/ArrayVsArrayListComp-768x375.jpg 768w\" sizes=\"auto, (max-width: 1146px) 100vw, 1146px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>La courbe orange (Tableau PowerShell) inque clairement que plus on ajoute des \u00e9l\u00e9ments dans un tableau plus le temps augmente (de mani\u00e8re exponentielle \u2013 On atteint 9 minutes pour 100 000 \u00e9lements).<\/p>\n<p>Notez au passage la courbe bleue au niveau des abscisses (X = Nombre d\u2019\u00e9lements; Y= Temps exprim\u00e9 en ticks soit 0.0001 ms) qui correspond au temps d\u2019un point de vue ArrayList<\/p>\n<p>&nbsp;<\/p>\n<h4 align=\"center\"><span style=\"font-weight: bold;\">Zoom : Temps d\u2019ex\u00e9cution c\u00f4t\u00e9 ArrayList<\/span><\/h4>\n<p align=\"center\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2682\" src=\"https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/ArrayList.png\" alt=\"\" width=\"1148\" height=\"553\" srcset=\"https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/ArrayList.png 1148w, https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/ArrayList-300x145.png 300w, https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/ArrayList-1024x493.png 1024w, https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/ArrayList-768x370.png 768w\" sizes=\"auto, (max-width: 1148px) 100vw, 1148px\" \/><\/p>\n<h4 align=\"center\"><\/h4>\n<p align=\"left\">Si on zoome sur la courbe bleue (<a href=\"https:\/\/docs.microsoft.com\/fr-fr\/dotnet\/api\/system.collections.arraylist?view=netframework-4.8\">ArrayList<\/a>) on constate que l\u2019\u00e9volution est lin\u00e9aire (ce que ne laisse pas deviner la premi\u00e8re courbe). On atteint moins de 2 secondes (!) pour 100 000 \u00e9l\u00e9ments.<\/p>\n<h4 align=\"center\"><span style=\"font-weight: bold;\">Ecart<\/span><\/h4>\n<p align=\"center\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2684\" src=\"https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/delta.png\" alt=\"\" width=\"1148\" height=\"578\" srcset=\"https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/delta.png 1148w, https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/delta-300x151.png 300w, https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/delta-1024x516.png 1024w, https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/delta-768x387.png 768w\" sizes=\"auto, (max-width: 1148px) 100vw, 1148px\" \/><\/p>\n<p>L\u2019\u00e9cart Array vs. <a href=\"https:\/\/docs.microsoft.com\/fr-fr\/dotnet\/api\/system.collections.arraylist?view=netframework-4.8\">ArrayList<\/a> lui aussi grandit en fonction du nombre d\u2019\u00e9l\u00e9ments. On constate donc que plus le nombre d\u2019\u00e9l\u00e9ments \u00e0 ajouter et grand et plus les tableaux sont a proscrire (on pouvait d\u00e9j\u00e0 tirer cette conclusion du premier graphique).<\/p>\n<p>En conclusion : Bannissez l\u2019utilisation des tableaux en PowerShell pour des structures de grandes tailles.<\/p>\n<hr \/>\n<div id=\"en-US\"><a href=\"#fr-FR\">Version fran\u00e7aise<\/a><\/div>\n<div><\/div>\n<div><\/div>\n<p>[Update : 05\/04\/2023] In February 2017 I <a href=\"https:\/\/twitter.com\/lavanack\/status\/832245707218231297\" target=\"_blank\" rel=\"noopener noreferrer\">retwitted<\/a> this <a href=\"https:\/\/stealthfield.wordpress.com\/2015\/08\/21\/powershell-array-vs-arraylist-performance\/\" target=\"_blank\" rel=\"noopener noreferrer\">article<\/a> : <a href=\"https:\/\/stealthfield.wordpress.com\/2015\/08\/21\/powershell-array-vs-arraylist-performance\/\" target=\"_blank\" rel=\"noopener noreferrer\">PowerShell \u2013 Array vs. ArrayList Performance<\/a> (I am not the author). This article compares performance (in terms of execution time) when adding 100,000 items into a PowerShell array as compared to the same operation in an ArrayList (object .Net: <a href=\"https:\/\/docs.microsoft.com\/fr-fr\/dotnet\/api\/system.collections.arraylist?view=netframework-4.8\">System.Collections.ArrayList<\/a>) . The result is largely in favor of the <a href=\"https:\/\/docs.microsoft.com\/fr-fr\/dotnet\/api\/system.collections.arraylist?view=netframework-4.8\">ArrayList<\/a>: Indeed arrays are objects of fixed size and the fact of adding an element in a table is equivalent to recreate a new table with the size n + 1 and copy the existing content in this new structure (which means that it is an expensive operation).<\/p>\n<p>So I&rsquo;m wondering how much the performance decreases (from a PowerShell tables perspective) depending on the number of elements to add. So I created a little PowerShell <a href=\"https:\/\/github.com\/lavanack\/laurentvanacker.com\/blob\/master\/Windows%20Powershell\/Misc\/ArrayVsArrayList.ps1\">script<\/a> that adds up to 100,000 elements in a table (starting from 0, 1 by 1 and in increments of 100 for each iteration: 0-100, 0-200 up to 0-100,000). So as not to disturb the <a href=\"https:\/\/github.com\/lavanack\/laurentvanacker.com\/blob\/master\/Windows%20Powershell\/Misc\/ArrayVsArrayList.ps1\">script<\/a> I launched it on a blank Azure VM (Standard DS1 v2 &#8211; 1 vcpus, 3.5 GiB memory).<\/p>\n<p>I leave you judge of the results below (The Excel file of the results is also available <a href=\"https:\/\/github.com\/lavanack\/laurentvanacker.com\/blob\/master\/Windows%20Powershell\/Misc\/ArrayVsArrayList.xlsx\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>).<\/p>\n<h4 align=\"center\"><span style=\"font-weight: bold;\">Comparison of execution times<\/span><\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2683 aligncenter\" src=\"https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/ArrayVsArrayListComp.jpg\" alt=\"\" width=\"1146\" height=\"559\" srcset=\"https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/ArrayVsArrayListComp.jpg 1146w, https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/ArrayVsArrayListComp-300x146.jpg 300w, https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/ArrayVsArrayListComp-1024x499.jpg 1024w, https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/ArrayVsArrayListComp-768x375.jpg 768w\" sizes=\"auto, (max-width: 1146px) 100vw, 1146px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The orange curve (PowerShell Table) clearly indicates that the more elements are added to a table the more time increases (exponentially &#8211; 9 minutes for 100,000 elements).<\/p>\n<p>Note in passing the blue curve at the abscissa (X = number of elements, Y = time expressed in ticks or 0.0001 ms) which corresponds to the time from an <a href=\"https:\/\/docs.microsoft.com\/fr-fr\/dotnet\/api\/system.collections.arraylist?view=netframework-4.8\">ArrayList<\/a> point of view<\/p>\n<p>&nbsp;<\/p>\n<h4 align=\"center\"><span style=\"font-weight: bold;\">Zoom : Execution time from an ArrayList point of view<\/span><\/h4>\n<p align=\"center\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2682\" src=\"https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/ArrayList.png\" alt=\"\" width=\"1148\" height=\"553\" srcset=\"https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/ArrayList.png 1148w, https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/ArrayList-300x145.png 300w, https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/ArrayList-1024x493.png 1024w, https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/ArrayList-768x370.png 768w\" sizes=\"auto, (max-width: 1148px) 100vw, 1148px\" \/><\/p>\n<h4 align=\"center\"><\/h4>\n<p align=\"left\">If we zoom in on the blue curve (<a href=\"https:\/\/docs.microsoft.com\/fr-fr\/dotnet\/api\/system.collections.arraylist?view=netframework-4.8\">ArrayList<\/a>) we see that the evolution is linear (which is not to be guessed by the first curve). We reach less than 2 seconds (!) for 100,000 elements.<\/p>\n<h4 align=\"center\"><span style=\"font-weight: bold;\">Gap<\/span><\/h4>\n<p align=\"center\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2684\" src=\"https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/delta.png\" alt=\"\" width=\"1148\" height=\"578\" srcset=\"https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/delta.png 1148w, https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/delta-300x151.png 300w, https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/delta-1024x516.png 1024w, https:\/\/laurentvanacker.com\/wp-content\/uploads\/2019\/12\/delta-768x387.png 768w\" sizes=\"auto, (max-width: 1148px) 100vw, 1148px\" \/><\/p>\n<p>The difference Array vs. <a href=\"https:\/\/docs.microsoft.com\/fr-fr\/dotnet\/api\/system.collections.arraylist?view=netframework-4.8\">ArrayList<\/a> also grows according to the number of elements. So we see that the more elements to add and larger and the more tables are to be avoided (we could already draw this conclusion from the first graph).<br \/>\nIn conclusion: Ban the use of tables in PowerShell for large structures.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\">Laurent.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>English version [MAJ : 04\/05\/2023] En f\u00e9vrier 2017 je retwittais cet article : PowerShell \u2013 Array vs. ArrayList Performance (dont je ne suis pas l\u2019auteur). [&#8230;]<\/p>\n","protected":false},"author":2,"featured_media":2463,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,9,19],"tags":[22,56,39],"class_list":["post-2216","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-net","category-performances","category-powershell","tag-array","tag-performances","tag-powershell"],"_links":{"self":[{"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/posts\/2216","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/comments?post=2216"}],"version-history":[{"count":12,"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/posts\/2216\/revisions"}],"predecessor-version":[{"id":3077,"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/posts\/2216\/revisions\/3077"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/media\/2463"}],"wp:attachment":[{"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/media?parent=2216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/categories?post=2216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/laurentvanacker.com\/index.php\/wp-json\/wp\/v2\/tags?post=2216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}