Skip to content

Instantly share code, notes, and snippets.

@usausa
Created November 2, 2018 14:04
Show Gist options
  • Save usausa/a77d23a4ab1957781dd38cd15f65538a to your computer and use it in GitHub Desktop.
Save usausa/a77d23a4ab1957781dd38cd15f65538a to your computer and use it in GitHub Desktop.
int[100] to List<int> with convert(NOP)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
[Config(typeof(BenchmarkConfig))]
public class ConvertEnumerableBenchmark
{
private readonly Func<object, object> converter = x => x;
private readonly int[] array = new int[100];
[Benchmark]
public List<int> ConstructorLinq()
{
return new List<int>(array.Select(x => (int)converter(x)));
}
[Benchmark]
public List<int> ForLoopAdd()
{
var list = new List<int>();
for (var i = 0; i < array.Length; i++)
{
list.Add((int)converter(array[i]));
}
return list;
}
[Benchmark]
public List<int> ForLoopAddWithCapacity()
{
var list = new List<int>(array.Length);
for (var i = 0; i < array.Length; i++)
{
list.Add((int)converter(array[i]));
}
return list;
}
[Benchmark]
public List<int> ForEachAdd()
{
var list = new List<int>();
foreach (var value in array)
{
list.Add((int)converter(value));
}
return list;
}
[Benchmark]
public List<int> ForEachAddWithCapacity()
{
var list = new List<int>(array.Length);
foreach (var value in array)
{
list.Add((int)converter(value));
}
return list;
}
[Benchmark]
public List<int> ConstructorStructEnumerable()
{
return new List<int>(new ArrayConvertStructEnumerable<int>(array, converter));
}
[Benchmark]
public List<int> ConstructorStructCollection()
{
return new List<int>(new ArrayConvertStructCollection<int>(array, converter));
}
}
public readonly struct ArrayConvertStructEnumerable<T> : IEnumerable<T>
{
private readonly T[] array;
private readonly Func<object, object> converter;
public ArrayConvertStructEnumerable(T[] array, Func<object, object> converter)
{
this.array = array;
this.converter = converter;
}
public IEnumerator<T> GetEnumerator()
{
return new ArrayConvertStructEnumerator<T>(array, converter);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public struct ArrayConvertStructEnumerator<T> : IEnumerator<T>
{
private readonly T[] source;
private readonly Func<object, object> converter;
private int index;
public ArrayConvertStructEnumerator(T[] source, Func<object, object> converter)
{
this.source = source;
this.converter = converter;
index = -1;
}
public bool MoveNext()
{
index++;
return index < source.Length;
}
public void Reset()
{
index = -1;
}
public T Current => (T)converter(source[index]);
object IEnumerator.Current => Current;
public void Dispose()
{
}
}
public readonly struct ArrayConvertStructCollection<T> : ICollection<T>
{
private readonly T[] source;
private readonly Func<object, object> converter;
public ArrayConvertStructCollection(T[] source, Func<object, object> converter)
{
this.source = source;
this.converter = converter;
}
public IEnumerator<T> GetEnumerator()
{
return new ArrayConvertStructEnumerator<T>(source, converter);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(T item) => throw new NotSupportedException();
public void Clear() => throw new NotSupportedException();
public bool Contains(T item) => throw new NotSupportedException();
public void CopyTo(T[] array, int arrayIndex)
{
for (var i = 0; i < source.Length; i++)
{
array[arrayIndex + i] = (T)converter(source[i]);
}
}
public bool Remove(T item) => throw new NotSupportedException();
public int Count => source.Length;
public bool IsReadOnly => true;
}
BenchmarkDotNet=v0.11.1, OS=Windows 10.0.17763
Intel Core i7-4771 CPU 3.50GHz (Haswell), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=2.1.402
  [Host]    : .NET Core 2.1.4 (CoreCLR 4.6.26814.03, CoreFX 4.6.26814.02), 64bit RyuJIT
  MediumRun : .NET Core 2.1.4 (CoreCLR 4.6.26814.03, CoreFX 4.6.26814.02), 64bit RyuJIT

Job=MediumRun  IterationCount=15  LaunchCount=2  
WarmupCount=10  
Method Mean Error StdDev Gen 0 Allocated
ConstructorLinq 2,203.0 ns 17.095 ns 25.59 ns 0.8812 3.62 KB
ForLoopAdd 1,210.9 ns 8.191 ns 12.01 ns 0.8545 3.51 KB
ForLoopAddWithCapacity 892.7 ns 7.397 ns 11.07 ns 0.6819 2.8 KB
ForEachAdd 1,215.7 ns 8.905 ns 12.77 ns 0.8545 3.51 KB
ForEachAddWithCapacity 938.5 ns 9.087 ns 12.44 ns 0.6819 2.8 KB
ConstructorStructEnumerable 2,317.5 ns 72.760 ns 108.90 ns 0.8698 3.58 KB
ConstructorStructCollection 884.9 ns 12.641 ns 17.72 ns 0.6895 2.83 KB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment