Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# combine list of bool

// setup initial vars
var myList = new List<bool>();
for (int x = 0; x < 10000000; x++)
    myList.Add(false);  

var containsAllFalse = false;
Stopwatch sw = new Stopwatch();

// start test
sw.Start();
containsAllFalse = !myList.Any(x => x);
sw.Stop();

// get result for Any
var timeAny = sw.ElapsedMilliseconds;

// reset variable state (just in case it affects anything)
containsAllFalse = false;   

// start test 2
sw.Restart();
containsAllFalse = myList.All(x => x == false);
sw.Stop();

// get result for All
var timeAll = sw.ElapsedMilliseconds;

// reset variable state (just in case it affects anything)
containsAllFalse = false;   

// start test 3
sw.Restart();
containsAllFalse = !myList.Exists(x => x == true);
sw.Stop();

// get result for All
var timeExists = sw.ElapsedMilliseconds;

// reset variable state (just in case it affects anything)
containsAllFalse = false;   

// start test 4
sw.Restart();   
containsAllFalse = !myList.Contains(true);          
sw.Stop();

// get result from Contains
var timeContains = sw.ElapsedMilliseconds;

// print results
var percentFaster = Math.Round((double)timeAny / timeContains, 2);
Console.WriteLine("Elapsed via Any = {0}ms", timeAny);
Console.WriteLine("Elapsed via All = {0}ms", timeAll);
Console.WriteLine("Elapsed via Exists = {0}ms", timeExists);
Console.WriteLine("Elapsed via Contains = {0}ms", timeContains);
Console.WriteLine("Contains is ~{0}x faster than Any!", percentFaster);
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# parse the date in DD/MMM/YYYY format 
Csharp :: c sharp list length 
Csharp :: c# difference between two dates in milliseconds C# 
Csharp :: trigger collider unity 
Csharp :: unity hide mesh 
Csharp :: convert string to date in c# 
Csharp :: VLC .net 
Csharp :: unique items in list c# 
Csharp :: convert ienumerable to list 
Csharp :: if statement swiftui 
Csharp :: triangle minimum path sum c# 
Csharp :: unity set dropdown value 
Csharp :: c# dictionary initializer 
Csharp :: csharp 
Csharp :: how to make a mouse down condition in unity 
Csharp :: c# integer to bit string 
Csharp :: c# socket connect timeout 
Csharp :: how to remove all buttons on a form C# 
Csharp :: c# day of week number 
Csharp :: unity log error 
Csharp :: c# combobox selected item 
Csharp :: unity key down 
Csharp :: c# datagridview selected row index 
Csharp :: hash table in c# 
Csharp :: unity stop animation from playing at start 
Csharp :: console.writeline in c# 
Csharp :: how to pass string value to enum in c# 
Csharp :: swap two numbers c# 
Csharp :: connection string in asp.net with username and password 
Csharp :: route attribute controller with parameter asp.net core 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =