Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using BenchmarkDotNet.Attributes;
- using BenchmarkDotNet.Attributes.Columns;
- using BenchmarkDotNet.Attributes.Exporters;
- using BenchmarkDotNet.Attributes.Jobs;
- using BenchmarkDotNet.Running;
- namespace Sandbox
- {
- [ClrJob] [RPlotExporter, RankColumn]
- public class LinqVsFill
- {
- private string[] _values;
- [Params(1000, 10000, 50000, 100000, 150000, 200000)]
- public int N;
- [GlobalSetup]
- public void Setup()
- {
- _values = new string[N];
- var random = new Random(42);
- for (int i = 0; i < N; i++)
- {
- _values[i] = DateTime.Now.ToString() + random.Next();
- }
- }
- private IEnumerable<string> FilterLinq()
- {
- return from a in _values where a.Contains("7") select a;
- }
- private List<string> FilterFill()
- {
- var result = new List<string>();
- foreach (var a in _values)
- {
- if (a.Contains("7"))
- {
- result.Add(a);
- }
- }
- return result;
- }
- [Benchmark]
- public int Linq()
- {
- var count = 0;
- foreach (var value in FilterLinq())
- {
- count += value.Length;
- }
- return count;
- }
- [Benchmark]
- public int Fill()
- {
- int count = 0;
- foreach (var value in FilterFill())
- {
- count += value.Length;
- }
- return count;
- }
- }
- public class Program
- {
- public static void Main(string[] args)
- {
- var summary = BenchmarkRunner.Run<LinqVsFill>();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement