Created
October 20, 2019 17:40
-
-
Save celsojr/704abc5a4765b8d57e28bcb4fd4dd481 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*-------------------------------------------------------------------------------------------- | |
| * Copyright (c) 2019 Celso Junior, All rights reserved. | |
| * Licence: Copy 'n paste freed! \o/ | |
| * Description: Csharp program benchmarking the best place to use .ToList() method | |
| * --------------------------------------------------------------------------------------------*/ | |
| using System; | |
| using System.Linq; | |
| using System.Collections.Generic; | |
| public class Program | |
| { | |
| const int length = 100000; | |
| public static void Main() | |
| { | |
| Primeiro(); | |
| Segundo(); | |
| } | |
| public static void Primeiro() | |
| { | |
| var before2 = GC.CollectionCount(2); | |
| var before1 = GC.CollectionCount(1); | |
| var before0 = GC.CollectionCount(0); | |
| var startTime = DateTime.Now; | |
| var bar = default(IEnumerable<int>); | |
| for (int i = 0; i < length; i++) | |
| { | |
| var foo = default(IEnumerable<int>); | |
| foo = Enumerable.Range(0, 1000); | |
| bar = foo.ToList().Skip(500).Take(10); | |
| } | |
| Console.WriteLine($"Resultado : {bar.Count()}"); | |
| Console.WriteLine(@$"Tempo total: {DateTime.Now.Subtract(startTime):hh\:mm\:ss\:fff}"); | |
| Console.WriteLine($"GC Gen #2 : {GC.CollectionCount(2) - before2} coletas"); | |
| Console.WriteLine($"GC Gen #1 : {GC.CollectionCount(1) - before1} coletas"); | |
| Console.WriteLine($"GC Gen #0 : {GC.CollectionCount(0) - before0} coletas\n"); | |
| } | |
| public static void Segundo() | |
| { | |
| var before2 = GC.CollectionCount(2); | |
| var before1 = GC.CollectionCount(1); | |
| var before0 = GC.CollectionCount(0); | |
| var startTime = DateTime.Now; | |
| var bar = Enumerable.Empty<int>(); | |
| for (int i = 0; i < length; i++) | |
| { | |
| var foo = Enumerable.Empty<int>(); | |
| foo = Enumerable.Range(0, 1000); | |
| bar = foo.Skip(500).Take(10).ToList(); | |
| } | |
| Console.WriteLine($"Resultado : {bar.Count()}"); | |
| Console.WriteLine(@$"Tempo total: {DateTime.Now.Subtract(startTime):hh\:mm\:ss\:fff}"); | |
| Console.WriteLine($"GC Gen #2 : {GC.CollectionCount(2) - before2} coletas"); | |
| Console.WriteLine($"GC Gen #1 : {GC.CollectionCount(1) - before1} coletas"); | |
| Console.WriteLine($"GC Gen #0 : {GC.CollectionCount(0) - before0} coletas\n"); | |
| } | |
| } | |
| /* output | |
| Resultado : 10 | |
| Tempo total: 00:00:00:720 | |
| GC Gen #2 : 0 coletas | |
| GC Gen #1 : 1 coletas | |
| GC Gen #0 : 40 coletas | |
| Resultado : 10 | |
| Tempo total: 00:00:00:021 | |
| GC Gen #2 : 0 coletas | |
| GC Gen #1 : 0 coletas | |
| GC Gen #0 : 2 coletas | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment