diff --git a/EnumerablePerformance.sln b/EnumerablePerformance.sln new file mode 100644 index 0000000..c56888f --- /dev/null +++ b/EnumerablePerformance.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32811.315 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EnumerablePerformance", "EnumerablePerformance\EnumerablePerformance.csproj", "{37DC452D-B053-4540-8AD4-DA7CB6A7E90B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {37DC452D-B053-4540-8AD4-DA7CB6A7E90B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {37DC452D-B053-4540-8AD4-DA7CB6A7E90B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {37DC452D-B053-4540-8AD4-DA7CB6A7E90B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {37DC452D-B053-4540-8AD4-DA7CB6A7E90B}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B3DD8F12-987C-48AE-8397-DB44558BF61B} + EndGlobalSection +EndGlobal diff --git a/EnumerablePerformance/EnumerablePerformance.csproj b/EnumerablePerformance/EnumerablePerformance.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/EnumerablePerformance/EnumerablePerformance.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/EnumerablePerformance/Program.cs b/EnumerablePerformance/Program.cs new file mode 100644 index 0000000..de8ff64 --- /dev/null +++ b/EnumerablePerformance/Program.cs @@ -0,0 +1,80 @@ +/* + * This method of enumerables makes it so that GetCustomers() is run twice, + * doing the same read line and return of the same data, for two different + * proccesses. Since customers is not immediately used, the function isn't + * happening yet. Count() causes the first run of GetCustomers() in order + * to get the customers to receive the count. GetCustomers() is then rerun + * during the Console.WriteLine() where it retrieves each customer. + */ + +Console.WriteLine("_______________________________________"); +Console.WriteLine("STANDARD"); +Console.WriteLine(); + + +var customers = GetCustomers(); + + +var count = customers.Count(); +Console.WriteLine($"\tThere are {count} customers"); + +foreach(var customer in customers) +{ + Console.WriteLine("\t" + customer); +} +Console.WriteLine(); + +/* + * By retrieving a list of the items from the function, you mitigate the issue + * of returning to the function later, as the data is saved as a list to a variable. + */ + +Console.WriteLine("_______________________________________"); +Console.WriteLine("ENHANCED-PERFORMANCE"); +Console.WriteLine(); + + +var customersList = GetCustomersList(); + +var countList = customersList.Count(); +Console.WriteLine($"\tThere are {countList} customers"); + +foreach (var customer in customersList) +{ + Console.WriteLine("\t" + customer); +} +Console.WriteLine(); + + +// Functions + +IEnumerable GetCustomers() +{ + Console.WriteLine("Inside GetCustomers()"); + var lines = File.ReadAllLines("../../../customers.csv"); //For some reason the program starts an extra 2 deep into bin/Debug + + foreach(var line in lines) + { + var splitLine = line.Split(","); + yield return new Customer(splitLine[0], int.Parse(splitLine[1])); + } +} + +IEnumerable GetCustomersList() +{ + Console.WriteLine("Inside GetCustomersList()"); + var list = new List(); + var lines = File.ReadAllLines("../../../customers.csv"); //For some reason the program starts an extra 2 deep into bin/Debug + + foreach (var line in lines) + { + var splitLine = line.Split(","); + list.Add(new Customer(splitLine[0], int.Parse(splitLine[1]))); + } + return list; +} + + + + +record Customer(string Fullname, int Age); \ No newline at end of file diff --git a/EnumerablePerformance/customers.csv b/EnumerablePerformance/customers.csv new file mode 100644 index 0000000..28551a4 --- /dev/null +++ b/EnumerablePerformance/customers.csv @@ -0,0 +1,3 @@ +Kira Jiroux,25 +Jira Kiroux,24 +Jairik Roux,59