TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# List Insert Performance

Use the Insert method on List. Compare the performance of Add versus Insert.
Insert. The Insert method puts an element into a List. Any position can be specified. We can insert an element to the start of the List.List
Insert, performance. We see how List.Insert performs. The Insert method on List has serious problems that may significantly degrade performance.List AddOptimization
An example. To begin, we use the Insert method to add an element at the start of the List. We specify the first argument of 0 to indicate "index 0."
C# program that uses List Insert using System.Collections.Generic; class Program { static void Main() { List<long> list = new List<long>(); for (long i = 0; i < 100000; i++) { list.Insert(0, i); } } }
Add example. Next, we look at code that also adds an element to the List instance, but does so at the end of the List. This code is faster: it uses List in the expected and optimized way.
C# program that uses List Add using System.Collections.Generic; class Program { static void Main() { List<long> list = new List<long>(); for (long i = 0; i < 100000; i++) { list.Add(i); } } }
A benchmark. Consider this benchmark: it has two versions of code that do slightly different things. But each version increases the size of the List by 100000 elements.Benchmark

Version 1: This version of the code uses the Insert method to add an element at index 0.

Version 2: This code invokes the Add() method to append a method at the end of the List.

Result: In 2020 we can see that the Insert method takes about 1000 times more time to increase the List by 100000 elements one-by-one.

C# program that times List Insert, Add using System; using System.Collections.Generic; using System.Diagnostics; class Program { const int _max = 10; static void Main() { var s1 = Stopwatch.StartNew(); // Version 1: insert at index 0. for (int i = 0; i < _max; i++) { List<long> list = new List<long>(); for (long z = 0; z < 100000; z++) { list.Insert(0, i); } } s1.Stop(); var s2 = Stopwatch.StartNew(); // Version 2: use Add() method. for (int i = 0; i < _max; i++) { List<long> list = new List<long>(); for (long z = 0; z < 100000; z++) { list.Add(i); } } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); } } Output 1181336470.00 ns Insert 1002800.00 ns Add
A summary. The List type is not optimized for Insert, but is fast for Add. Use LinkedList or another data structure if you need to insert items at the start.LinkedList
Often you can sort the elements later if you need a specific order. We can call Add() and then scan through the List backwards, avoiding Insert calls.For
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf