forked from dotnet/coreclr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update BenchmarksGame benchmarks to latest
For each benchmark, grab the current best C# .NET entry, and also grab the current best serial implementation (since these are easier to work with from the benchmarking perspective).
- Loading branch information
1 parent
19fc1cd
commit 4d9e8b5
Showing
29 changed files
with
2,453 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
105 changes: 105 additions & 0 deletions
105
tests/src/JIT/Performance/CodeQuality/BenchmarksGame/binarytrees/binarytrees-best.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
// Adapted from binary-trees C# .NET Core #5 program | ||
// http://benchmarksgame.alioth.debian.org/u64q/program.php?test=binarytrees&lang=csharpcore&id=5 | ||
// Best-scoring C# .NET Core version as of 2017-09-01 | ||
|
||
/* The Computer Language Benchmarks Game | ||
http://benchmarksgame.alioth.debian.org/ | ||
contributed by Marek Safar | ||
*reset* | ||
concurrency added by Peperud | ||
minor improvements by Alex Yakunin | ||
*/ | ||
|
||
using System; | ||
using System.Runtime.CompilerServices; | ||
using System.Threading.Tasks; | ||
|
||
public sealed class BinaryTrees | ||
{ | ||
public const int MinDepth = 4; | ||
|
||
public static void Main(string[] args) | ||
{ | ||
var n = args.Length == 0 ? 0 : int.Parse(args[0]); | ||
var maxDepth = n < (MinDepth + 2) ? MinDepth + 2 : n; | ||
var stretchDepth = maxDepth + 1; | ||
|
||
var stretchDepthTask = Task.Run(() => TreeNode.CreateTree(stretchDepth).CountNodes()); | ||
var maxDepthTask = Task.Run(() => TreeNode.CreateTree(maxDepth).CountNodes()); | ||
|
||
var tasks = new Task<string>[(maxDepth - MinDepth) / 2 + 1]; | ||
for (int depth = MinDepth, ti = 0; depth <= maxDepth; depth += 2, ti++) { | ||
var iterationCount = 1 << (maxDepth - depth + MinDepth); | ||
var depthCopy = depth; // To make sure closure value doesn't change | ||
tasks[ti] = Task.Run(() => { | ||
var count = 0; | ||
if (depthCopy >= 17) { | ||
// Parallelized computation for relatively large tasks | ||
var miniTasks = new Task<int>[iterationCount]; | ||
for (var i = 0; i < iterationCount; i++) | ||
miniTasks[i] = Task.Run(() => TreeNode.CreateTree(depthCopy).CountNodes()); | ||
Task.WaitAll(miniTasks); | ||
for (var i = 0; i < iterationCount; i++) | ||
count += miniTasks[i].Result; | ||
} | ||
else { | ||
// Sequential computation for smaller tasks | ||
for (var i = 0; i < iterationCount; i++) | ||
count += TreeNode.CreateTree(depthCopy).CountNodes(); | ||
} | ||
return $"{iterationCount}\t trees of depth {depthCopy}\t check: {count}"; | ||
}); | ||
} | ||
Task.WaitAll(tasks); | ||
|
||
Console.WriteLine("stretch tree of depth {0}\t check: {1}", | ||
stretchDepth, stretchDepthTask.Result); | ||
foreach (var task in tasks) | ||
Console.WriteLine(task.Result); | ||
Console.WriteLine("long lived tree of depth {0}\t check: {1}", | ||
maxDepth, maxDepthTask.Result); | ||
} | ||
} | ||
|
||
public struct TreeNode | ||
{ | ||
public sealed class NodeData | ||
{ | ||
public TreeNode Left, Right; | ||
|
||
public NodeData(TreeNode left, TreeNode right) | ||
{ | ||
Left = left; | ||
Right = right; | ||
} | ||
} | ||
|
||
public NodeData Data; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public TreeNode(TreeNode left, TreeNode right) | ||
{ | ||
Data = new NodeData(left, right); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static TreeNode CreateTree(int depth) | ||
{ | ||
return depth <= 0 | ||
? default(TreeNode) | ||
: new TreeNode(CreateTree(depth - 1), CreateTree(depth - 1)); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public int CountNodes() | ||
{ | ||
if (ReferenceEquals(Data, null)) | ||
return 1; | ||
return 1 + Data.Left.CountNodes() + Data.Right.CountNodes(); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
tests/src/JIT/Performance/CodeQuality/BenchmarksGame/binarytrees/binarytrees-serial.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
// Adapted from binary-trees C# .NET Core #2 program | ||
// http://benchmarksgame.alioth.debian.org/u64q/program.php?test=binarytrees&lang=csharpcore&id=2 | ||
// Best-scoring single-threaded C# .NET Core version as of 2017-09-01 | ||
|
||
/* The Computer Language Benchmarks Game | ||
http://benchmarksgame.alioth.debian.org/ | ||
contributed by Marek Safar | ||
*reset* | ||
*/ | ||
|
||
using System; | ||
|
||
class BinaryTrees | ||
{ | ||
const int minDepth = 4; | ||
|
||
public static void Main(String[] args) | ||
{ | ||
int n = 0; | ||
if (args.Length > 0) n = Int32.Parse(args[0]); | ||
|
||
int maxDepth = Math.Max(minDepth + 2, n); | ||
int stretchDepth = maxDepth + 1; | ||
|
||
int check = (TreeNode.bottomUpTree(stretchDepth)).itemCheck(); | ||
Console.WriteLine("stretch tree of depth {0}\t check: {1}", stretchDepth, check); | ||
|
||
TreeNode longLivedTree = TreeNode.bottomUpTree(maxDepth); | ||
|
||
for (int depth=minDepth; depth<=maxDepth; depth+=2){ | ||
int iterations = 1 << (maxDepth - depth + minDepth); | ||
|
||
check = 0; | ||
for (int i=1; i<=iterations; i++) | ||
{ | ||
check += (TreeNode.bottomUpTree(depth)).itemCheck(); | ||
} | ||
|
||
Console.WriteLine("{0}\t trees of depth {1}\t check: {2}", | ||
iterations, depth, check); | ||
} | ||
|
||
Console.WriteLine("long lived tree of depth {0}\t check: {1}", | ||
maxDepth, longLivedTree.itemCheck()); | ||
} | ||
|
||
|
||
struct TreeNode | ||
{ | ||
class Next | ||
{ | ||
public TreeNode left, right; | ||
} | ||
|
||
private Next next; | ||
|
||
internal static TreeNode bottomUpTree(int depth){ | ||
if (depth>0){ | ||
return new TreeNode( | ||
bottomUpTree(depth-1) | ||
, bottomUpTree(depth-1) | ||
); | ||
} | ||
else { | ||
return new TreeNode(); | ||
} | ||
} | ||
|
||
TreeNode(TreeNode left, TreeNode right){ | ||
this.next = new Next (); | ||
this.next.left = left; | ||
this.next.right = right; | ||
} | ||
|
||
internal int itemCheck(){ | ||
// if necessary deallocate here | ||
if (next==null) return 1; | ||
else return 1 + next.left.itemCheck() + next.right.itemCheck(); | ||
} | ||
} | ||
} |
127 changes: 127 additions & 0 deletions
127
tests/src/JIT/Performance/CodeQuality/BenchmarksGame/fannkuch-redux/fannkuch-redux-best.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
// Adapted from fannkuch-redux C# .NET Core #5 program | ||
// http://benchmarksgame.alioth.debian.org/u64q/program.php?test=fannkuchredux&lang=csharpcore&id=5 | ||
// Best-scoring C# .NET Core version as of 2017-09-01 | ||
|
||
/* The Computer Language Benchmarks Game | ||
http://benchmarksgame.alioth.debian.org/ | ||
contributed by Isaac Gouy, transliterated from Oleg Mazurov's Java program | ||
concurrency fix and minor improvements by Peperud | ||
parallel and small optimisations by Anthony Lloyd | ||
*/ | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Runtime.CompilerServices; | ||
|
||
public static class FannkuchRedux | ||
{ | ||
static int[] fact, chkSums, maxFlips; | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
static void firstPermutation(int[] p, int[] pp, int[] count, int idx) | ||
{ | ||
for (int i = 0; i < p.Length; ++i) p[i] = i; | ||
for (int i = count.Length - 1; i > 0; --i) | ||
{ | ||
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) | ||
{ | ||
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; | ||
} | ||
|
||
[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) | ||
{ | ||
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++; | ||
} | ||
} | ||
|
||
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) | ||
{ | ||
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; | ||
} | ||
|
||
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++) | ||
{ | ||
threads[i].Join(); | ||
chksum += chkSums[i]; | ||
if (maxFlips[i] > maxflips) maxflips = maxFlips[i]; | ||
} | ||
Console.Out.WriteLineAsync(chksum + "\nPfannkuchen(" + n + ") = " + maxflips); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
tests/src/JIT/Performance/CodeQuality/BenchmarksGame/fannkuch-redux/fannkuch-redux-serial.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
// Adapted from fannkuch-redux C# .NET Core #2 program | ||
// http://benchmarksgame.alioth.debian.org/u64q/program.php?test=fannkuchredux&lang=csharpcore&id=2 | ||
// Best-scoring single-threaded C# .NET Core version as of 2017-09-01 | ||
|
||
/* The Computer Language Benchmarks Game | ||
http://benchmarksgame.alioth.debian.org/ | ||
contributed by Isaac Gouy, transliterated from Mike Pall's Lua program | ||
*/ | ||
|
||
using System; | ||
|
||
class FannkuchRedux | ||
{ | ||
public static int[] fannkuch(int n) { | ||
int[] p = new int[n], q = new int[n], s = new int[n]; | ||
int sign = 1, maxflips = 0, sum = 0, m = n-1; | ||
for(int i=0; i<n; i++){ p[i] = i; q[i] = i; s[i] = i; } | ||
do { | ||
// Copy and flip. | ||
var q0 = p[0]; // Cache 0th element. | ||
if (q0 != 0){ | ||
for(int i=1; i<n; i++) q[i] = p[i]; // Work on a copy. | ||
var flips = 1; | ||
do { | ||
var qq = q[q0]; | ||
if (qq == 0){ // ... until 0th element is 0. | ||
sum += sign*flips; | ||
if (flips > maxflips) maxflips = flips; // New maximum? | ||
break; | ||
} | ||
q[q0] = q0; | ||
if (q0 >= 3){ | ||
int i = 1, j = q0 - 1, t; | ||
do { t = q[i]; q[i] = q[j]; q[j] = t; i++; j--; } while (i < j); | ||
} | ||
q0 = qq; flips++; | ||
} while (true); | ||
} | ||
// Permute. | ||
if (sign == 1){ | ||
var t = p[1]; p[1] = p[0]; p[0] = t; sign = -1; // Rotate 0<-1. | ||
} else { | ||
var t = p[1]; p[1] = p[2]; p[2] = t; sign = 1; // Rotate 0<-1 and 0<-1<-2. | ||
for(int i=2; i<n; i++){ | ||
var sx = s[i]; | ||
if (sx != 0){ s[i] = sx-1; break; } | ||
if (i == m) return new int[]{sum,maxflips}; // Out of permutations. | ||
s[i] = i; | ||
// Rotate 0<-...<-i+1. | ||
t = p[0]; for(int j=0; j<=i; j++){ p[j] = p[j+1]; } p[i+1] = t; | ||
} | ||
} | ||
} while (true); | ||
} | ||
|
||
static void Main(string[] args){ | ||
int n = (args.Length > 0) ? Int32.Parse(args[0]) : 7; | ||
var pf = fannkuch(n); | ||
Console.Write("{0}\nPfannkuchen({1}) = {2}\n", pf[0], n, pf[1]); | ||
} | ||
} |
Oops, something went wrong.