diff options
Diffstat (limited to 'tests/src/JIT/Performance/CodeQuality/BenchmarksGame/fannkuch-redux/fannkuch-redux-best.cs')
-rw-r--r-- | tests/src/JIT/Performance/CodeQuality/BenchmarksGame/fannkuch-redux/fannkuch-redux-best.cs | 173 |
1 files changed, 88 insertions, 85 deletions
diff --git a/tests/src/JIT/Performance/CodeQuality/BenchmarksGame/fannkuch-redux/fannkuch-redux-best.cs b/tests/src/JIT/Performance/CodeQuality/BenchmarksGame/fannkuch-redux/fannkuch-redux-best.cs index 470b67beaf..c11f6a8cac 100644 --- a/tests/src/JIT/Performance/CodeQuality/BenchmarksGame/fannkuch-redux/fannkuch-redux-best.cs +++ b/tests/src/JIT/Performance/CodeQuality/BenchmarksGame/fannkuch-redux/fannkuch-redux-best.cs @@ -18,110 +18,113 @@ using System; using System.Threading; using System.Runtime.CompilerServices; -public static class FannkuchRedux +namespace BenchmarksGame { - static int[] fact, chkSums, maxFlips; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - static void firstPermutation(int[] p, int[] pp, int[] count, int idx) + public static class FannkuchRedux { - for (int i = 0; i < p.Length; ++i) p[i] = i; - for (int i = count.Length - 1; i > 0; --i) + static int[] fact, chkSums, maxFlips; + [MethodImpl(MethodImplOptions.AggressiveInlining)] + static void firstPermutation(int[] p, int[] pp, int[] count, int idx) { - int d = idx / fact[i]; - count[i] = d; - if (d > 0) + for (int i = 0; i < p.Length; ++i) p[i] = i; + for (int i = count.Length - 1; i > 0; --i) { - idx = idx % fact[i]; - for (int j = i; j >= 0; --j) pp[j] = p[j]; - for (int j = 0; j <= i; ++j) p[j] = pp[(j + d) % (i + 1)]; + int d = idx / fact[i]; + count[i] = d; + if (d > 0) + { + idx = idx % fact[i]; + for (int j = i; j >= 0; --j) pp[j] = p[j]; + for (int j = 0; j <= i; ++j) p[j] = pp[(j + d) % (i + 1)]; + } } } - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - static int nextPermutation(int[] p, int[] count) - { - int first = p[1]; - p[1] = p[0]; - p[0] = first; - int i = 1; - while (++count[i] > i) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + static int nextPermutation(int[] p, int[] count) { - count[i++] = 0; - int next = p[1]; - p[0] = next; - for (int j = 1; j < i;) p[j] = p[++j]; - p[i] = first; - first = next; + int first = p[1]; + p[1] = p[0]; + p[0] = first; + int i = 1; + while (++count[i] > i) + { + count[i++] = 0; + int next = p[1]; + p[0] = next; + for (int j = 1; j < i;) p[j] = p[++j]; + p[i] = first; + first = next; + } + return first; } - return first; - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - static int countFlips(int first, int[] p, int[] pp) - { - if (first == 0) return 0; - if (p[first] == 0) return 1; - for (int i = 0; i < pp.Length; i++) pp[i] = p[i]; - int flips = 2; - while (true) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + static int countFlips(int first, int[] p, int[] pp) { - for (int lo = 1, hi = first - 1; lo < hi; lo++, hi--) + if (first == 0) return 0; + if (p[first] == 0) return 1; + for (int i = 0; i < pp.Length; i++) pp[i] = p[i]; + int flips = 2; + while (true) { - int t = pp[lo]; - pp[lo] = pp[hi]; - pp[hi] = t; + for (int lo = 1, hi = first - 1; lo < hi; lo++, hi--) + { + int t = pp[lo]; + pp[lo] = pp[hi]; + pp[hi] = t; + } + int tp = pp[first]; + if (pp[tp] == 0) return flips; + pp[first] = first; + first = tp; + flips++; } - int tp = pp[first]; - if (pp[tp] == 0) return flips; - pp[first] = first; - first = tp; - flips++; } - } - static void run(int n, int taskId, int taskSize) - { - int[] p = new int[n], pp = new int[n], count = new int[n]; - firstPermutation(p, pp, count, taskId * taskSize); - int chksum = countFlips(p[0], p, pp); - int maxflips = chksum; - while (--taskSize > 0) + static void run(int n, int taskId, int taskSize) { - var flips = countFlips(nextPermutation(p, count), p, pp); - chksum += (1 - (taskSize % 2) * 2) * flips; - if (flips > maxflips) maxflips = flips; + int[] p = new int[n], pp = new int[n], count = new int[n]; + firstPermutation(p, pp, count, taskId * taskSize); + int chksum = countFlips(p[0], p, pp); + int maxflips = chksum; + while (--taskSize > 0) + { + var flips = countFlips(nextPermutation(p, count), p, pp); + chksum += (1 - (taskSize % 2) * 2) * flips; + if (flips > maxflips) maxflips = flips; + } + chkSums[taskId] = chksum; + maxFlips[taskId] = maxflips; } - chkSums[taskId] = chksum; - maxFlips[taskId] = maxflips; - } - - public static void Main(string[] args) - { - int n = args.Length > 0 ? int.Parse(args[0]) : 7; - fact = new int[n + 1]; - fact[0] = 1; - var factn = 1; - for (int i = 1; i < fact.Length; i++) { fact[i] = factn *= i; } - int nTasks = Environment.ProcessorCount; - chkSums = new int[nTasks]; - maxFlips = new int[nTasks]; - int taskSize = factn / nTasks; - var threads = new Thread[nTasks]; - for (int i = 1; i < nTasks; i++) - { - int j = i; - (threads[j] = new Thread(() => run(n, j, taskSize))).Start(); - } - run(n, 0, taskSize); - int chksum = chkSums[0], maxflips = maxFlips[0]; - for (int i = 1; i < threads.Length; i++) + public static void Main(string[] args) { - threads[i].Join(); - chksum += chkSums[i]; - if (maxFlips[i] > maxflips) maxflips = maxFlips[i]; + int n = args.Length > 0 ? int.Parse(args[0]) : 7; + fact = new int[n + 1]; + fact[0] = 1; + var factn = 1; + for (int i = 1; i < fact.Length; i++) { fact[i] = factn *= i; } + + int nTasks = Environment.ProcessorCount; + chkSums = new int[nTasks]; + maxFlips = new int[nTasks]; + int taskSize = factn / nTasks; + var threads = new Thread[nTasks]; + for (int i = 1; i < nTasks; i++) + { + int j = i; + (threads[j] = new Thread(() => run(n, j, taskSize))).Start(); + } + run(n, 0, taskSize); + int chksum = chkSums[0], maxflips = maxFlips[0]; + for (int i = 1; i < threads.Length; i++) + { + threads[i].Join(); + chksum += chkSums[i]; + if (maxFlips[i] > maxflips) maxflips = maxFlips[i]; + } + Console.Out.WriteLineAsync(chksum + "\nPfannkuchen(" + n + ") = " + maxflips); } - Console.Out.WriteLineAsync(chksum + "\nPfannkuchen(" + n + ") = " + maxflips); } } |