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 Clear Example

Use the List Clear method. Compare the performance of calling List Clear and creating a new List.
List, Clear. Should you clear a List to reuse it? Or should you just allocate a new List? We test whether the Clear() method helps improve code that uses List.List
It seems likely that clearing and reusing an existing list would result in better performance. But this benchmark shows the opposite is true.Constructor
Example. Here we create a List, and call Add() twice to append 2 elements to the List. The List has 2 elements at that point. We call Clear(), and then it has 0 elements.
C# program that uses Clear on List using System; using System.Collections.Generic; class Program { static void Main() { List<int> ids = new List<int>(); ids.Add(10); ids.Add(11); Console.WriteLine("COUNT: {0}", ids.Count); // Do not assign anything to clear. ids.Clear(); Console.WriteLine("COUNT: {0}", ids.Count); } } Output COUNT: 2 COUNT: 0
Benchmark, clear. All code that uses a List must first allocate it, or clear an existing List to reuse that one. Consider this benchmark program. The program tests 2 methods.

Version 1: This code calls Clear on an existing List and then adds 100 ints to it.

Version 2: This code instead changes the reference to point to a new List with capacity of 100.

Capacity

Result: Both methods have the same result, which is a list of 100 integers from 0 to 99. The first version requires a non-null reference.

C# program that benchmarks List Clear method using System; using System.Collections.Generic; using System.Diagnostics; class Program { const int _max = 1000000; static void Main() { // ... New list. var list = new List<int>(100); // Version 1: clear 1 list and reuse it many times. var s1 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { list.Clear(); for (int x = 0; x < 100; x++) { list.Add(x); } } s1.Stop(); // Version 2: create a new list on each use. var s2 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { list = new List<int>(100); for (int x = 0; x < 100; x++) { list.Add(x); } } 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 534.52 ns List Clear 274.80 ns new List
Notes, results. In the results, calling Clear() on the List was slower. It is faster to simply allocate a new List rather than clear an existing List.

Info: This depends on how fast the Clear() method is versus the speed of the memory allocation.

A discussion. The version of the method that reuses the same List will cause less churn in the garbage collector. But it has an additional cost of the Clear method.

Important: Avoiding new collection allocations is not a reliable optimization. Simply creating a new List when one is needed is best.

A summary. We tested the List Clear method. In some cases, using a new List to replace the old List is faster. This depends on the relative performance of the managed heap.
© 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