Skip to content

Commit

Permalink
More for jackmott#15: [Enhancement] Can these API's also work on IRea…
Browse files Browse the repository at this point in the history
…dOnlyList<T>

- Implement LastF's the same way as FirstF's
  • Loading branch information
Smurf-IV committed Aug 5, 2019
1 parent b762eff commit ac38801
Show file tree
Hide file tree
Showing 4 changed files with 643 additions and 248 deletions.
303 changes: 193 additions & 110 deletions Benchmarks/Benchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,47 +193,47 @@ public void Setup()
// return intArray.SumF(x => x / 2);
//}

[Benchmark]
public double IntReadOnlyArraySumWithSelectLinq()
{
return Array.AsReadOnly(intArray).Sum(x => x / 2);
}
//[Benchmark]
//public double IntReadOnlyArraySumWithSelectLinq()
//{
// return Array.AsReadOnly(intArray).Sum(x => x / 2);
//}

[Benchmark]
public double IntReadOnlyArraySumWithSelectFast()
{
return Array.AsReadOnly(intArray).SumF(x => x / 2);
}
//[Benchmark]
//public double IntReadOnlyArraySumWithSelectFast()
//{
// return Array.AsReadOnly(intArray).SumF(x => x / 2);
//}

//[Benchmark]
//public int IntArraySumFastSIMD()
//{
// return intArray.SumS();
//}

[Benchmark]
public int? IntNullArraySumLinq()
{
return intNullArray.Sum();
}
//[Benchmark]
//public int? IntNullArraySumLinq()
//{
// return intNullArray.Sum();
//}

[Benchmark]
public int? IntNullArraySumFast()
{
return intNullArray.SumF();
}
//[Benchmark]
//public int? IntNullArraySumFast()
//{
// return intNullArray.SumF();
//}

[Benchmark]
public int IntNullArraySumLinqSelect()
{
return intNullArray.Sum(x => x/2 ?? 0);
}
//[Benchmark]
//public int IntNullArraySumLinqSelect()
//{
// return intNullArray.Sum(x => x/2 ?? 0);
//}

[Benchmark]
public int IntNullArraySumFastSelect()
{
return intNullArray.SumF(x => x/2 ?? 0);
}
//[Benchmark]
//public int IntNullArraySumFastSelect()
//{
// return intNullArray.SumF(x => x/2 ?? 0);
//}

//[Benchmark]
//public float FloatArraySumLinq()
Expand Down Expand Up @@ -271,29 +271,29 @@ public int IntNullArraySumFastSelect()
// return floatArray.SumF(x => x / 2);
//}

[Benchmark]
public float? FloatNullArraySumLinq()
{
return floatNullArray.Sum();
}
//[Benchmark]
//public float? FloatNullArraySumLinq()
//{
// return floatNullArray.Sum();
//}

[Benchmark]
public float? FloatNullArraySumFast()
{
return floatNullArray.SumF();
}
//[Benchmark]
//public float? FloatNullArraySumFast()
//{
// return floatNullArray.SumF();
//}

[Benchmark]
public float FloatNullArraySumLinqSelect()
{
return floatNullArray.Sum(x => x / 2 ?? 0);
}
//[Benchmark]
//public float FloatNullArraySumLinqSelect()
//{
// return floatNullArray.Sum(x => x / 2 ?? 0);
//}

[Benchmark]
public float FloatNullArraySumFastSelect()
{
return floatNullArray.SumF(x => x / 2 ?? 0);
}
//[Benchmark]
//public float FloatNullArraySumFastSelect()
//{
// return floatNullArray.SumF(x => x / 2 ?? 0);
//}


//[Benchmark]
Expand Down Expand Up @@ -332,41 +332,41 @@ public float FloatNullArraySumFastSelect()
// return doubleArray.SumF(x => x / 2);
//}

[Benchmark]
public double? DoubleNullArraySumLinq()
{
return doubleNullArray.Sum();
}
//[Benchmark]
//public double? DoubleNullArraySumLinq()
//{
// return doubleNullArray.Sum();
//}

[Benchmark]
public double? DoubleNullArraySumFast()
{
return doubleNullArray.SumF();
}
//[Benchmark]
//public double? DoubleNullArraySumFast()
//{
// return doubleNullArray.SumF();
//}

[Benchmark]
public double? DoubleNullArraySumLinqSelect()
{
return doubleNullArray.Sum(x => x / 2 ?? 0);
}
//[Benchmark]
//public double? DoubleNullArraySumLinqSelect()
//{
// return doubleNullArray.Sum(x => x / 2 ?? 0);
//}

[Benchmark]
public double? DoubleNullArraySumFastSelect()
{
return doubleNullArray.SumF(x => x / 2 ?? 0);
}
//[Benchmark]
//public double? DoubleNullArraySumFastSelect()
//{
// return doubleNullArray.SumF(x => x / 2 ?? 0);
//}

[Benchmark]
public double IntArrayAverageLinq()
{
return intArray.Average();
}
//[Benchmark]
//public double IntArrayAverageLinq()
//{
// return intArray.Average();
//}

[Benchmark]
public double IntArrayAverageFast()
{
return intArray.AverageF();
}
//[Benchmark]
//public double IntArrayAverageFast()
//{
// return intArray.AverageF();
//}

//[Benchmark]
//public double IntArrayAverageFastSIMD()
Expand All @@ -375,47 +375,47 @@ public double IntArrayAverageFast()
//}


[Benchmark]
public double IntListAverageLinq()
{
return intList.Average();
}
//[Benchmark]
//public double IntListAverageLinq()
//{
// return intList.Average();
//}

[Benchmark]
public double IntListAverageFast()
{
return intList.AverageF();
}
//[Benchmark]
//public double IntListAverageFast()
//{
// return intList.AverageF();
//}

//[Benchmark]
//public double IntListAverageFastSIMD()
//{
// return intList.AverageS();
//}

[Benchmark]
public int IntListSumWithSelectLinq()
{
return intList.Sum(x => x / 2);
}
//[Benchmark]
//public int IntListSumWithSelectLinq()
//{
// return intList.Sum(x => x / 2);
//}

[Benchmark]
public int IntListSumWithSelectFast()
{
return intList.SumF(x => x / 2);
}
//[Benchmark]
//public int IntListSumWithSelectFast()
//{
// return intList.SumF(x => x / 2);
//}

[Benchmark]
public double IntReadOnlyListSumWithSelectLinq()
{
return intList.AsReadOnly().Sum(x => x / 2);
}
//[Benchmark]
//public double IntReadOnlyListSumWithSelectLinq()
//{
// return intList.AsReadOnly().Sum(x => x / 2);
//}

[Benchmark]
public double IntReadOnlyListSumWithSelectFast()
{
return intList.AsReadOnly().SumF(x => x / 2);
}
//[Benchmark]
//public double IntReadOnlyListSumWithSelectFast()
//{
// return intList.AsReadOnly().SumF(x => x / 2);
//}


//private static readonly Func<double, int, double> mulXInts = (acc, x) => acc += x * x;
Expand Down Expand Up @@ -581,6 +581,89 @@ public double IntReadOnlyListSumWithSelectFast()
// return Array.AsReadOnly(intArray).FirstF(firstInts);
//}

private static readonly Func<int, bool> LastInts = (x) => x > 0;

[Benchmark]
public double IntArrayLastLinq()
{
return intArray.Last(LastInts);
}

[Benchmark]
public double IntArrayLastFast()
{
return intArray.LastF(LastInts);
}

[Benchmark]
public double IntArrayLastFast1()
{
Predicate<int> predicate = new Predicate<int>(LastInts);
return Array.Find(intArray, predicate);
}


[Benchmark]
public double IntSpanLastForEach()
{
int[] localArray = intArray;
Span<int> asSpan = localArray.AsSpan();
foreach (int i in asSpan)
{
if (LastInts(i))
{
return i;
}
}

return 0;
}

[Benchmark]
public double IntSpanLastFast()
{
int[] localArray = intArray;
Span<int> asSpan = localArray.AsSpan();
return asSpan.LastF(LastInts);
}

[Benchmark]
public double IntListLastLinq()
{
return intList.Last(LastInts);
}

[Benchmark]
public double IntListLastFast()
{
return intList.LastF(LastInts);
}

[Benchmark]
public double IntAsListReadOnlyLastLinq()
{
return intList.AsReadOnly().Last(LastInts);
}

[Benchmark]
public double IntAsListReadOnlyLastFast()
{
return intList.AsReadOnly().LastF(LastInts);
}

[Benchmark]
public double IntArrayAsReadOnlyLastLinq()
{
return Array.AsReadOnly(intArray).Last(LastInts);
}

[Benchmark]
public double IntArrayAsReadOnlyLastFast()
{
return Array.AsReadOnly(intArray).LastF(LastInts);
}


//[Benchmark]
//public double IntArrayWhereAggregateLinq()
//{
Expand Down
2 changes: 1 addition & 1 deletion LinqFaster/First.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace JM.LinqFaster
{
public static partial class LinqFaster
{

// -------------------------- Arrays --------------------------------------------

/// <summary>
Expand Down Expand Up @@ -68,6 +67,7 @@ public static T FirstF<T>(this T[] source, Func<T, bool> predicate)
/// <param name="source">The array to return the first element of.</param>
/// <returns>default value if source is empty, otherwise, the first element
/// in source.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T FirstOrDefaultF<T>(this T[] source)
{
if (source == null)
Expand Down
Loading

0 comments on commit ac38801

Please sign in to comment.