List.Count() Vs List.Any()

C# Performance Tips

Check List has items List.Count() vs List.Any()

Stopwatch watch = new Stopwatch();  
List < string > strs = new List < string > () {  
    "Akshay",  
    "Patel",  
    "Panth",  
    "Patel"  
};  
watch.Start();  
if (strs.Count() > 0) {}  
Console.WriteLine("List.Count()-{0}", watch.Elapsed);

--------

watch.Restart();  
if (strs.Any()) {}  
Console.WriteLine("List.Any() - {0}", watch.Elapsed);

List.Count() Vs List.Any()

 

Loop on items in a list Array.Length vs var i = Array.Length

Stopwatch watch = new Stopwatch();  
watch.Start();  
string[] names = {  
    "Akshay",  
    "Patel",  
    "Panth"  
};  
for (int i = 0; i < names.Length; i++) {}  
Console.WriteLine("Name.Length Direct-{0}", watch.Elapsed); 

---------

watch.Restart();  
string[] names1 = {  
    "Akshay",  
    "Patel",  
    "Panth"  
};  
int k = names1.Length;  
for (int j = 0; j < k; j++) {}  
Console.WriteLine("Name.Length Parameter-{0}", watch.Elapsed);

Array Length

String split Char vs Char[]

string str = "Akshay|Patel";  
Stopwatch s1 = new Stopwatch();  
s1.Start();  
string[] temp1 = str.Split('|');  
Console.WriteLine(s1.ElapsedTicks.ToString()); 

------------------

Stopwatch s2 = new Stopwatch();  
s2.Start();  
string[] temp = str.Split(new char[] {  
    '|'  
});  
Console.WriteLine(s2.ElapsedTicks.ToString());  

C# Programming Performance

Count of items in a list List.Count() vs List.Count

watch.Start();  
int count = strs.Count();  
Console.WriteLine("Count()-{0}", watch.Elapsed);  

----
watch.Restart();  
int count1 = strs.Count;  
Console.WriteLine("Count - {0}", watch.Elapsed);

List.Count() vs List.Count

Source : https://www.c-sharpcorner.com/blogs/c-sharp-programming-performance-tips-part-one-string-split

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.