Last active
September 26, 2018 12:29
-
-
Save Szer/02a469be536ea96307cd9da226885f11 to your computer and use it in GitHub Desktop.
CSharpBench
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
| private static async Task<T> Reduce<T>(Func<T, T, T> f, T[] ie, int start, int finish) | |
| { | |
| var len = finish - start + 1; | |
| if (len == 0) | |
| { | |
| throw new Exception("Sequence contains no elements"); | |
| } | |
| else if (len == 1) | |
| { | |
| return ie[start]; | |
| } | |
| else if (len == 2) | |
| { | |
| return f(ie[start], ie[start+1]); | |
| } | |
| else | |
| { | |
| var h = len / 2 + start; | |
| var o1Task = Reduce(f, ie, start, h-1); | |
| var o2Task = Reduce(f, ie, h, finish); | |
| var o1 = await o1Task; | |
| var o2 = await o2Task; | |
| return f(o1, o2); | |
| } | |
| } | |
| public static T ReduceParallelTasks<T>(Func<T, T, T> f, T[] ie) | |
| { | |
| return Reduce(f, ie, 0, ie.Length-1).Result; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment