Performance of Enumerable that tries to replicate LINQ OrderBy - c#

I was trying to implement an Enumerable that replicates what LINQ OrderBy does. To do so, I used two approaches:
I used Mono's Linq/Enumerable.cs as base and replicated the OrderBy code in my Enumerable.
I used a decompiler to get an impression of .NET's version and replicated that code.
When benchmarking the LINQ version versus both options (I used .Take(10) to print less output), the linq version is significantly faster (1900ms vs 3700ms). The source data was List<MyObject>, sorted by char member. Optimized build flag was on.
Could someone please explain me where this difference could come from?
Edit:
I will outline the code for 2. below:
Here, Buffer<TElement> is a copy of System.Linq.Buffer<TElement> (copied from ILSpy as it is internal) and Sort is (mostly) copied in the same way from System.Linq.EnumerableSorter<TElement>.
public class Query2F
{
private Func<Lineitem, char> keySelector;
private char[] keys;
private IComparer<char> comparer;
private bool descending;
public Query2F(Func<Lineitem, char> keySelector, bool descending)
{
this.keySelector = keySelector;
this.descending = descending;
this.comparer = Comparer<char>.Default;
}
public static IEnumerable<Lineitem> Execute(List<Lineitem> lineitem)
{
Buffer<Lineitem> buffer = new Buffer<Lineitem>(lineitem);
if (buffer.count > 0)
{
Query2F q2f = new Query2F((s => s.returnflag), false);
int[] array = q2f.Sort(buffer.items, buffer.count);
q2f = null;
for (int i = 0; i < buffer.count; i++)
{
yield return buffer.items[array[i]];
}
}
yield break;
}
private void ComputeKeys(Lineitem[] elements, int count)
{
this.keys = new char[count];
for (int i = 0; i < count; i++)
{
//this.keys[i] = this.keySelector(elements[i]);
this.keys[i] = elements[i].returnflag;
}
}
private int CompareKeys(int index1, int index2)
{
int num = this.comparer.Compare(this.keys[index1], this.keys[index2]);
if (num == 0)
{
return index1 - index2;
}
else
{
if (!this.descending)
{
return num;
}
return -num;
}
}
internal int[] Sort(Lineitem[] elements, int count)
{
this.ComputeKeys(elements, count);
int[] array = new int[count];
for (int i = 0; i < count; i++)
{
array[i] = i;
}
this.QuickSort(array, 0, count - 1);
return array;
}
private void QuickSort(int[] map, int left, int right)
{
do
{
int num = left;
int num2 = right;
int index = map[num + (num2 - num >> 1)];
do
{
if (num < map.Length)
{
if (this.CompareKeys(index, map[num]) > 0)
{
num++;
continue;
}
}
while (num2 >= 0 && this.CompareKeys(index, map[num2]) < 0)
{
num2--;
}
if (num > num2)
{
break;
}
if (num < num2)
{
int num3 = map[num];
map[num] = map[num2];
map[num2] = num3;
}
num++;
num2--;
}
while (num <= num2);
if (num2 - left <= right - num)
{
if (left < num2)
{
this.QuickSort(map, left, num2);
}
left = num;
}
else
{
if (num < right)
{
this.QuickSort(map, num, right);
}
right = num2;
}
}
while (left < right);
}
}
internal struct Buffer<TElement>
{
internal TElement[] items;
internal int count;
internal Buffer(IEnumerable<TElement> source)
{
TElement[] array = null;
int num = 0;
ICollection<TElement> collection = source as ICollection<TElement>;
if (collection != null)
{
num = collection.Count;
if (num > 0)
{
array = new TElement[num];
collection.CopyTo(array, 0);
}
}
else
{
foreach (TElement current in source)
{
if (array == null)
{
array = new TElement[4];
}
else
{
if (array.Length == num)
{
TElement[] array2 = new TElement[checked(num * 2)];
Array.Copy(array, 0, array2, 0, num);
array = array2;
}
}
array[num] = current;
num++;
}
}
this.items = array;
this.count = num;
}
internal TElement[] ToArray()
{
if (this.count == 0)
{
return new TElement[0];
}
if (this.items.Length == this.count)
{
return this.items;
}
TElement[] array = new TElement[this.count];
Array.Copy(this.items, 0, array, 0, this.count);
return array;
}
}

Problem solved.Thanks to Mike for pointing me to the solution.
The reason for the discrepancies in the execution time were merely that I executed the wrong build (which was not a Release build).

Related

Why is this yield break not breaking the loop?

I wrote a method that takes as input a sequence (it can also be an infinite sequence) and an integer count and returns a new sequence that contains count elements taken from the input sequence, that are in positions which are prime numbers.
For example, if I have a sequence {5,13,84,10,2,6} and count = 3, the result sequence is {84,10,6}.
The class I wrote:
public static class PrimeMethod
{
public static IEnumerable<T> TakePrime<T>(this IEnumerable<T> s, int count)
{
var result = new List<T>();
if (s == null)
throw new ArgumentNullException();
if (count <= 0)
throw new ArgumentOutOfRangeException();
int x = 0;
int y = 0;
foreach (var i in s)
{
if (checkIfPrime(x) && y < count)
{
result.Add(i);
y++;
}
x++;
}
return result;
}
public static bool checkIfPrime(int num)
{
if (num == 0 || num == 1)
return false;
bool flag = false;
for (int i = 2; i < num; i++)
if (num % i == 0)
{
flag = true;
break;
}
if (!flag)
return true;
return false;
}
}
Now I have to write a parameterized test that takes as input a sequence from 0 to infinite and a size integer. The test must make sure that the size of the resulting list is equal to the parameter size.
I wrote this:
[TestCase(7)]
[TestCase(182)]
public void Check_Size(int size)
{
var list = List_Integers(size);
var result = list.TakePrime(size);
Assert.That(()=>result.Count(),Is.EqualTo(size));
}
public IEnumerable<int> List_Integers(int size)
{
var i = 0;
var list = new List<int>();
while(true)
{
var j = i;
i++;
list.Add(j);
if (list.TakePrime(size).Count() > size)
yield break;
yield return j;
}
}
Although I used a yield break the test never stops. Why is this not working?
UPDATE:
I realized my mistake and I changed List_Integers to this:
public IEnumerable<int> List_Integers(int size)
{
var i = 0;
var list = new List<int>();
var incr = size+1;
while(true)
{
var j = i;
i++;
list.Add(j);
if (list.TakePrime(size).Count() >= incr)
yield break;
yield return j;
}
}
It's still not working and I don't understand why.
You call
list.TakePrime(size)
Which (as you described and implemented yourself) will return a maximum of size primes. Hence it will never be greater than size.
I found the solution which is this:
[TestCase(7)]
[TestCase(182)]
public void Check_Size(int size)
{
var incr = size + 1;
var list = List_Integers(incr);
var result = list.TakePrime(size);
Assert.That(()=>result.Count(),Is.EqualTo(size));
}
public IEnumerable<int> List_Integers(int size)
{
var i = 0;
var list = new List<int>();
while(true)
{
var j = i;
i++;
list.Add(j);
if (list.TakePrime(size).Count() >= size)
yield break;
yield return j;
}
}

How can I prevent my Quicksort Algorithm from throwing a StackOverflowException

Im trying to write a quicksort algorithm in C#, and I've been getting System.StackOverflowExceptions for the last while and can't figure out why.
Here is my Class:
class quicksort : sortalgorithm
{
int pivotIndex = -1;
int pivotSwapped = 0;
Random rand = new Random();
public quicksort(int[] arr)
{
if (arr.Length < 5)
{
throw new Exception("Array has too few Entries");
}
toSort = arr;
}
protected override int sort()
{
if (pivotIndex == -1) getPivot();
int indexL = getIndexLeft();
int indexR = getIndexRight();
if (indexR != -1 && indexL != -1 && indexL != toSort.Length-1)
{
swap(indexL, indexR);
}
if (indexL > indexR)
{
Console.WriteLine("Index thingy");
swap(toSort.Length - 1, indexL);
pivotSwapped++;
if (pivotSwapped == toSort.Length - 1)
{
return 1;
}
else
{
Console.WriteLine("new piv");
pivotSwapped++;
getPivot();
sort();
return -1;
}
}
else
{
sort();
return -1;
}
}
private void swap(int i, int j)
{
int temp = toSort[i];
toSort[i] = toSort[j];
toSort[j] = temp;
}
private void getPivot()
{
pivotIndex = rand.Next(0, toSort.Length - 1);
swap(toSort.Length - 1, pivotIndex);
}
private int getIndexLeft() // Larger then Pivot Starting: Left
{ //Error Here
int itemFromLeft = -1;
for (int i = 0; i < toSort.Length - 1; i++)
{
if (toSort[i] >= toSort[toSort.Length - 1])
{
itemFromLeft = i;
i = toSort.Length + 1;
}
}
//Console.WriteLine(itemFromLeft);
return itemFromLeft;
}
private int getIndexRight()// Smaller then Pivot Starting: Right
{
int itemFromRight = -1;
for (int i = toSort.Length - 1; i >= 0; i--)
{
if (toSort[i] < toSort[toSort.Length - 1])
{
itemFromRight = i;
i = -1;
}
}
//Console.WriteLine(itemFromRight);
return itemFromRight;
}
}
I hope that someone can help me.
P.S. the class sortalgorithm in this case only has the the array toSort.
My Console output always looks a bit like this:
[4, 28, 62, 33, 11] // The unsorted array
Index thingy
new piv
Index thingy
new piv
Index thingy
new piv
Process is terminated due to `StackOverflowException`.
a stack overflow error usually happens when you have a error in your recursion , ie a function keeps calling itself until there is no room left in the stack to hold all the references,
the only obvious candidate is
**sort();**
return -1;
}
}
else
{
**sort();**
return -1;
}
so either one or the other recursive calls is probably incorrect and needs removing and i suspect the issue will be resolved
EDIT:
as you are unable to debug your code
this is what a quick sort should look like
public int sort()
{
qsort(0, toSort.Length - 1);
return 0;
}
private void qsort( int left, int right)
{
if (left < right)
{
int pivot = Partition( left, right);
if (pivot > 1)
{
qsort( left, pivot - 1);
}
if (pivot + 1 < right)
{
qsort( pivot + 1, right);
}
}
}
private int Partition( int left, int right)
{
int pivot = toSort[left];
while (true)
{
while (toSort[left] < pivot)
{
left++;
}
while (toSort[right] > pivot)
{
right--;
}
if (left < right)
{
if (toSort[left] == toSort[right]) return right;
int temp = toSort[left];
toSort[left] = toSort[right];
toSort[right] = temp;
}
else
{
return right;
}
}
}
Note that if left >= right do nothing
This simple implementation of Hoare partition scheme demonstrates how to avoid stack overflow by recursing on the smaller partition, and looping back for the larger partition, an idea used in the original quicksort. Stack space complexity is limited to O(log2(n)), but worst case time complexity is still O(n^2).
static public void Quicksort(int [] a, int lo, int hi)
{
while(lo < hi){ // while partition size > 1
int p = a[(lo + hi) / 2];
int i = lo, j = hi;
i--; // Hoare partition
j++;
while (true)
{
while (a[++i] < p) ;
while (a[--j] > p) ;
if (i >= j)
break;
int t = a[i];
a[i] = a[j];
a[j] = t;
}
i = j++; // i = last left, j = first right
if((i - lo) <= (hi - j)){ // if size(left) <= size(right)
Quicksort(a, lo, i); // recurse on smaller partition
lo = j; // and loop on larger partition
} else {
Quicksort(a, j, hi); // recurse on smaller partition
hi = i; // and loop on larger partition
}
}
}

Issues with MinHeaps

I'm trying to develop custom path-finding for the game I'm making. It's using the new 2D colliders so I can't use unity's built in navmesh. So instead of using an asset someone else created I figured I'd have a go at making my own implementation.
I've looked up a bunch of stuff about path-finding and A*, which I have no problem at all understanding. Where I'm having troubles though is the MinHeap. I've never looked into bit shifting and the like so I modified some code I found in a tutorial to suit my needs.
The problem that I'm having is that, from what I can tell in my debugging, it isn't properly sorting the nodes by cost value. From what I can see, and I'll post a picture below so you know what I'm talking about, every node that's being added to the open list is staying where it's added, so it's more like a Dijkstra's search then an A* one.
Could someone look at the code and tell me if I'm doing something very wrong.. because this is driving me mad.
This is the code:
using System;
public class NodeList<T> where T : IComparable<T>
{
private int count;
private int capacity;
private T temp;
private T mheap;
private T[] array;
private T[] tempArray;
public int Count
{
get { return this.count; }
}
public NodeList() : this(16) { }
public NodeList(int capacity)
{
this.count = 0;
this.capacity = capacity;
array = new T[capacity];
}
public void BuildHead()
{
int position;
for (position = (this.count - 1) >> 1; position >= 0; position--)
{
this.MinHeapify(position);
}
}
public void Add(T item)
{
this.count++;
if (this.count > this.capacity)
{
DoubleArray();
}
this.array[this.count - 1] = item;
int position = this.count - 1;
int parentPosition = ((position - 1) >> 1);
while (position > 0 && array[parentPosition].CompareTo(array[position]) > 0)
{
temp = this.array[position];
this.array[position] = this.array[parentPosition];
this.array[parentPosition] = temp;
position = parentPosition;
parentPosition = ((position - 1) >> 1);
}
}
private void DoubleArray()
{
this.capacity <<= 1;
tempArray = new T[this.capacity];
CopyArray(this.array, tempArray);
this.array = tempArray;
}
private static void CopyArray(T[] source, T[] destination)
{
int index;
for (index = 0; index < source.Length; index++)
{
destination[index] = source[index];
}
}
public T Peek()
{
if (this.count == 0)
{
throw new InvalidOperationException("Heap is empty");
}
return this.array[0];
}
public T ExtractFirst()
{
if (this.count == 0)
{
throw new InvalidOperationException("Heap is empty");
}
temp = this.array[0];
this.array[0] = this.array[this.count - 1];
this.count--;
this.MinHeapify(0);
return temp;
}
private void MinHeapify(int position)
{
do
{
int left = ((position << 1) + 1);
int right = left + 1;
int minPosition;
if (left < count && array[left].CompareTo(array[position]) < 0)
{
minPosition = left;
}
else
{
minPosition = position;
}
if (right < count && array[right].CompareTo(array[minPosition]) < 0)
{
minPosition = right;
}
if (minPosition != position)
{
mheap = this.array[position];
this.array[position] = this.array[minPosition];
this.array[minPosition] = mheap;
position = minPosition;
}
else
{
return;
}
} while (true);
}
}
My path searches from the end point to the start (From where the player clicks, to the player), so the "Starting point" is the grey node in the middle of the screen:
Pathfinding issue
This is why it's a problem:
Issue 2

permutation in c#

Is it possible to generate all permutations of a collection in c#?
char[] inputSet = { 'A','B','C' };
Permutations<char> permutations = new Permutations<char>(inputSet);
foreach (IList<char> p in permutations)
{
Console.WriteLine(String.Format("{{{0} {1} {2}}}", p[0], p[1], p[2]));
}
I've already faced the problem and I wrote these simple methods:
public static IList<T[]> GeneratePermutations<T>(T[] objs, long? limit)
{
var result = new List<T[]>();
long n = Factorial(objs.Length);
n = (!limit.HasValue || limit.Value > n) ? n : (limit.Value);
for (long k = 0; k < n; k++)
{
T[] kperm = GenerateKthPermutation<T>(k, objs);
result.Add(kperm);
}
return result;
}
public static T[] GenerateKthPermutation<T>(long k, T[] objs)
{
T[] permutedObjs = new T[objs.Length];
for (int i = 0; i < objs.Length; i++)
{
permutedObjs[i] = objs[i];
}
for (int j = 2; j < objs.Length + 1; j++)
{
k = k / (j - 1); // integer division cuts off the remainder
long i1 = (k % j);
long i2 = j - 1;
if (i1 != i2)
{
T tmpObj1 = permutedObjs[i1];
T tmpObj2 = permutedObjs[i2];
permutedObjs[i1] = tmpObj2;
permutedObjs[i2] = tmpObj1;
}
}
return permutedObjs;
}
public static long Factorial(int n)
{
if (n < 0) { throw new Exception("Unaccepted input for factorial"); } //error result - undefined
if (n > 256) { throw new Exception("Input too big for factorial"); } //error result - input is too big
if (n == 0) { return 1; }
// Calculate the factorial iteratively rather than recursively:
long tempResult = 1;
for (int i = 1; i <= n; i++)
{
tempResult *= i;
}
return tempResult;
}
Usage:
var perms = Utilities.GeneratePermutations<char>(new char[]{'A','B','C'}, null);
There's nothing built in.
I've found a a couple of Code Project articles here and here which might help you implement your own class.
public static void Recursion(char[] charList, int loBound, int upBound )
{
for (int i = loBound; i <= upBound; i++)
{
swap(ref charList[loBound], ref charList[i]);
if (loBound == upBound)
{
Console.Write(charList);
Console.WriteLine("");
}
Recursion(charList, loBound + 1, upBound);
swap(ref charList[loBound], ref charList[i]);
}
}
public static void swap(ref char a, ref char b)
{
if (a == b) return;
a ^= b;
b ^= a;
a ^= b;
}
public static void Main(string[] args)
{
string c = "123";
char[] c2 = c.ToCharArray();
Recursion(c2, 0, c2.Length-1);
Console.ReadKey();
}
I built these Extensions Methods for Enumerable<T>
The following Generics Methods can find Permutations as well as Combinations of an IEnumerable of any type.
Visit http://github.com/MathewSachin/Equamatics for more
using System;
using System.Collections.Generic;
using System.Linq;
namespace Equamatics
{
public static class Combinatorics
{
#region Permute
public static IEnumerable<IEnumerable<T>> Permute<T>(this IEnumerable<T> Input, int r = -1)
{
int n = Input.Count();
if (r == -1) foreach (var item in new Permutor<T>(Input).Recursion(0))
yield return item;
if (r > n) throw new ArgumentOutOfRangeException("r cannot be greater than no of elements");
foreach (var list in Input.Combinations(r))
foreach (var item in new Permutor<T>(list).Recursion(0))
yield return item;
}
class Permutor<T>
{
int ElementLevel = -1;
int[] PermutationValue;
T[] Elements;
public Permutor(IEnumerable<T> Input)
{
Elements = Input.ToArray();
PermutationValue = new int[Input.Count()];
}
public IEnumerable<IEnumerable<T>> Recursion(int k)
{
ElementLevel++;
PermutationValue[k] = ElementLevel;
if (ElementLevel == Elements.Length)
{
List<T> t = new List<T>();
foreach (int i in PermutationValue) t.Add(Elements[i - 1]);
yield return t;
}
else
for (int i = 0; i < Elements.Length; i++)
if (PermutationValue[i] == 0)
foreach (IEnumerable<T> e in Recursion(i))
yield return e;
ElementLevel--;
PermutationValue[k] = 0;
}
}
public static double P(int n, int r)
{
if (r < 0 | n < 0 | n < r) return Double.NaN;
else if (r == 0) return 1;
else if (n == r) return Factorial(n);
else
{
double Product = 1;
for (int i = n - r + 1; i <= n; ++i) Product *= i;
return Product;
}
}
#endregion
#region Combinations
public static IEnumerable<IEnumerable<T>> Combinations<T>(this IEnumerable<T> Input, int r = -1)
{
if (r == -1)
{
yield return Input;
yield break;
}
int n = Input.Count();
if (r > n) throw new ArgumentOutOfRangeException("r cannot be greater than no of elements");
int[] Indices = Enumerable.Range(0, r).ToArray();
yield return Indices.Select(k => Input.ElementAt(k));
while (true)
{
int i;
for (i = r - 1; i >= 0; --i)
if (Indices[i] != i + n - r)
break;
if (i < 0) break;
Indices[i] += 1;
for (int j = i + 1; j < r; ++j)
Indices[j] = Indices[j - 1] + 1;
yield return Indices.Select(k => Input.ElementAt(k));
}
}
public static double C(int n, int r)
{
if (r < 0 | n < 0 | n < r) return Double.NaN;
else if (n - r == 1 | r == 1) return n;
else if (n == r | r == 0) return 1;
else if (n - r > r) return (P(n, n - r) / Factorial(n - r));
else return (P(n, r) / Factorial(r));
}
#endregion
public static double Factorial(int n)
{
if (n < 0) return Double.NaN;
else if (n == 0) return 1;
else
{
double Product = 1;
for (int i = 1; i <= n; ++i) Product *= i;
return Product;
}
}
public static int Derangement(int n)
{
double x = 0;
for (int i = 2; i <= n; ++i)
{
if (i % 2 == 0) x += (1 / Factorial(i));
else x -= (1 / Factorial(i));
}
return (int)(Factorial(n) * x);
}
public static int Catalan(int n) { return (int)C(2 * n, n) / (n + 1); }
}
}
`
A simple implementation using recursion
static void Main(string[] args)
{
char[] inputSet = { 'A', 'B', 'C' };
var permutations = GetPermutations(new string(inputSet));
foreach (var p in permutations)
{
Console.WriteLine(String.Format("{{{0} {1} {2}}}", p[0], p[1], p[2]));
}
}
public static List<string> GetPermutations(string str)
{
List<string> result = new List<string>();
if (str == null)
return null;
if (str.Length == 0)
{
result.Add("");
return result;
}
char c = str.ElementAt(0);
var perms = GetPermutations(str.Substring(1));
foreach (var word in perms)
{
for (int i = 0; i <= word.Length; i++)
{
result.Add(word.Substring(0, i) + c + word.Substring(i));
}
}
return result;
}

Logic behind the Array.Reverse() method

what is the native logic work behind public static void Reverse(Array array, int index, int length);
You can use .NET Reflector for that:
[ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
public static void Reverse(Array array, int index, int length)
{
if (array == null)
{
throw new ArgumentNullException("array");
}
if ((index < array.GetLowerBound(0)) || (length < 0))
{
throw new ArgumentOutOfRangeException((index < 0) ? "index" : "length", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
}
if ((array.Length - (index - array.GetLowerBound(0))) < length)
{
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
}
if (array.Rank != 1)
{
throw new RankException(Environment.GetResourceString("Rank_MultiDimNotSupported"));
}
if (!TrySZReverse(array, index, length))
{
int num = index;
int num2 = (index + length) - 1;
object[] objArray = array as object[];
if (objArray == null)
{
while (num < num2)
{
object obj3 = array.GetValue(num);
array.SetValue(array.GetValue(num2), num);
array.SetValue(obj3, num2);
num++;
num2--;
}
}
else
{
while (num < num2)
{
object obj2 = objArray[num];
objArray[num] = objArray[num2];
objArray[num2] = obj2;
num++;
num2--;
}
}
}
}
TrySZReverse is a native method that can sometimes do the same thing only faster.
Loop from the starting point, index, to the middle of the range, index + length/2, swapping each array[i] with array[index + length - i - 1].
Some details about the native method "TrySZReverse"
from the related codes from coreclr([1][2]), TrySZReverse handles the array of primitive types, and the reverse algorithm is the same as Array.Reverse :
codes quotation
static void Reverse(KIND array[], UINT32 index, UINT32 count) {
LIMITED_METHOD_CONTRACT;
_ASSERTE(array != NULL);
if (count == 0) {
return;
}
UINT32 i = index;
UINT32 j = index + count - 1;
while(i < j) {
KIND temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
}
and the prefix "SZ" seems stands for "single-dimension zero-terminated".

Categories