Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
2.1.1 Basic types: Lists and Linq - caveat
//2.1.1 Basic types: Lists and Linq - caveat using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { //Linq uses deffered execution. That means that unless you have "materialized" result of linq query (by calling .ToList() or other .ToMethod()) //the result is actually the query itself and not the actual result of it. Therefore whenever you use the query it will be executed again and again. //ToList() turns the query into result produced by that query. //Here is an example. var dic = new Dictionary<int, string>(); for(int i=0; i<20000; i++) { dic.Add(i, i.ToString()); } var list = dic.Where(f => f.Value.StartsWith("1")).Select(f => f.Key);//.ToList(); //uncomment for fast results //list is actually the query when there is no .ToList() above Console.WriteLine(list.GetType()); //which means that whenever we call list.Contains() below we will evaluate this query again and again turning this algorithm into O(n^2) //with ToList() uncommented the algorithm is O(n) var list2 = dic.Where(f => list.Contains(f.Key)).ToList(); Console.WriteLine(list2.Count()); } } }
run
|
edit
|
history
|
help
0
Recur5
4
Problem: fb_series
money
Fórum ➡ Finding STRONG tags that contain UL tags, using Regex class♦
manju sql
Dungeon Game
GFV SDXZASX
oooo
SIN_in_Pow