Skip to content

Commit d0099ff

Browse files
Apply default VS formatting
Also insert namespace BenchmarksGame.
1 parent 4d9e8b5 commit d0099ff

18 files changed

+1755
-1583
lines changed

tests/src/JIT/Performance/CodeQuality/BenchmarksGame/binarytrees/binarytrees-best.cs

+71-64
Original file line numberDiff line numberDiff line change
@@ -19,87 +19,94 @@ minor improvements by Alex Yakunin
1919
using System.Runtime.CompilerServices;
2020
using System.Threading.Tasks;
2121

22-
public sealed class BinaryTrees
22+
namespace BenchmarksGame
2323
{
24-
public const int MinDepth = 4;
25-
26-
public static void Main(string[] args)
24+
public sealed class BinaryTrees
2725
{
28-
var n = args.Length == 0 ? 0 : int.Parse(args[0]);
29-
var maxDepth = n < (MinDepth + 2) ? MinDepth + 2 : n;
30-
var stretchDepth = maxDepth + 1;
26+
public const int MinDepth = 4;
27+
28+
public static void Main(string[] args)
29+
{
30+
var n = args.Length == 0 ? 0 : int.Parse(args[0]);
31+
var maxDepth = n < (MinDepth + 2) ? MinDepth + 2 : n;
32+
var stretchDepth = maxDepth + 1;
3133

32-
var stretchDepthTask = Task.Run(() => TreeNode.CreateTree(stretchDepth).CountNodes());
33-
var maxDepthTask = Task.Run(() => TreeNode.CreateTree(maxDepth).CountNodes());
34+
var stretchDepthTask = Task.Run(() => TreeNode.CreateTree(stretchDepth).CountNodes());
35+
var maxDepthTask = Task.Run(() => TreeNode.CreateTree(maxDepth).CountNodes());
3436

35-
var tasks = new Task<string>[(maxDepth - MinDepth) / 2 + 1];
36-
for (int depth = MinDepth, ti = 0; depth <= maxDepth; depth += 2, ti++) {
37-
var iterationCount = 1 << (maxDepth - depth + MinDepth);
38-
var depthCopy = depth; // To make sure closure value doesn't change
39-
tasks[ti] = Task.Run(() => {
40-
var count = 0;
41-
if (depthCopy >= 17) {
37+
var tasks = new Task<string>[(maxDepth - MinDepth) / 2 + 1];
38+
for (int depth = MinDepth, ti = 0; depth <= maxDepth; depth += 2, ti++)
39+
{
40+
var iterationCount = 1 << (maxDepth - depth + MinDepth);
41+
var depthCopy = depth; // To make sure closure value doesn't change
42+
tasks[ti] = Task.Run(() =>
43+
{
44+
var count = 0;
45+
if (depthCopy >= 17)
46+
{
4247
// Parallelized computation for relatively large tasks
4348
var miniTasks = new Task<int>[iterationCount];
44-
for (var i = 0; i < iterationCount; i++)
45-
miniTasks[i] = Task.Run(() => TreeNode.CreateTree(depthCopy).CountNodes());
46-
Task.WaitAll(miniTasks);
47-
for (var i = 0; i < iterationCount; i++)
48-
count += miniTasks[i].Result;
49-
}
50-
else {
49+
for (var i = 0; i < iterationCount; i++)
50+
miniTasks[i] = Task.Run(() => TreeNode.CreateTree(depthCopy).CountNodes());
51+
Task.WaitAll(miniTasks);
52+
for (var i = 0; i < iterationCount; i++)
53+
count += miniTasks[i].Result;
54+
}
55+
else
56+
{
5157
// Sequential computation for smaller tasks
5258
for (var i = 0; i < iterationCount; i++)
53-
count += TreeNode.CreateTree(depthCopy).CountNodes();
54-
}
55-
return $"{iterationCount}\t trees of depth {depthCopy}\t check: {count}";
56-
});
57-
}
58-
Task.WaitAll(tasks);
59+
count += TreeNode.CreateTree(depthCopy).CountNodes();
60+
}
61+
return $"{iterationCount}\t trees of depth {depthCopy}\t check: {count}";
62+
});
63+
}
64+
Task.WaitAll(tasks);
5965

60-
Console.WriteLine("stretch tree of depth {0}\t check: {1}",
61-
stretchDepth, stretchDepthTask.Result);
62-
foreach (var task in tasks)
63-
Console.WriteLine(task.Result);
64-
Console.WriteLine("long lived tree of depth {0}\t check: {1}",
65-
maxDepth, maxDepthTask.Result);
66+
Console.WriteLine("stretch tree of depth {0}\t check: {1}",
67+
stretchDepth, stretchDepthTask.Result);
68+
foreach (var task in tasks)
69+
Console.WriteLine(task.Result);
70+
Console.WriteLine("long lived tree of depth {0}\t check: {1}",
71+
maxDepth, maxDepthTask.Result);
72+
}
6673
}
67-
}
6874

69-
public struct TreeNode
70-
{
71-
public sealed class NodeData
75+
public struct TreeNode
7276
{
73-
public TreeNode Left, Right;
74-
75-
public NodeData(TreeNode left, TreeNode right)
77+
public sealed class NodeData
7678
{
77-
Left = left;
78-
Right = right;
79+
public TreeNode Left, Right;
80+
81+
public NodeData(TreeNode left, TreeNode right)
82+
{
83+
Left = left;
84+
Right = right;
85+
}
7986
}
80-
}
8187

82-
public NodeData Data;
88+
public NodeData Data;
8389

84-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
85-
public TreeNode(TreeNode left, TreeNode right)
86-
{
87-
Data = new NodeData(left, right);
88-
}
90+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
91+
public TreeNode(TreeNode left, TreeNode right)
92+
{
93+
Data = new NodeData(left, right);
94+
}
8995

90-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
91-
public static TreeNode CreateTree(int depth)
92-
{
93-
return depth <= 0
94-
? default(TreeNode)
95-
: new TreeNode(CreateTree(depth - 1), CreateTree(depth - 1));
96-
}
96+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
97+
public static TreeNode CreateTree(int depth)
98+
{
99+
return depth <= 0
100+
? default(TreeNode)
101+
: new TreeNode(CreateTree(depth - 1), CreateTree(depth - 1));
102+
}
97103

98-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
99-
public int CountNodes()
100-
{
101-
if (ReferenceEquals(Data, null))
102-
return 1;
103-
return 1 + Data.Left.CountNodes() + Data.Right.CountNodes();
104+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
105+
public int CountNodes()
106+
{
107+
if (ReferenceEquals(Data, null))
108+
return 1;
109+
return 1 + Data.Left.CountNodes() + Data.Right.CountNodes();
110+
}
104111
}
105112
}

tests/src/JIT/Performance/CodeQuality/BenchmarksGame/binarytrees/binarytrees-serial.cs

+76-67
Original file line numberDiff line numberDiff line change
@@ -15,72 +15,81 @@ contributed by Marek Safar
1515

1616
using System;
1717

18-
class BinaryTrees
18+
namespace BenchmarksGame
1919
{
20-
const int minDepth = 4;
21-
22-
public static void Main(String[] args)
23-
{
24-
int n = 0;
25-
if (args.Length > 0) n = Int32.Parse(args[0]);
26-
27-
int maxDepth = Math.Max(minDepth + 2, n);
28-
int stretchDepth = maxDepth + 1;
29-
30-
int check = (TreeNode.bottomUpTree(stretchDepth)).itemCheck();
31-
Console.WriteLine("stretch tree of depth {0}\t check: {1}", stretchDepth, check);
32-
33-
TreeNode longLivedTree = TreeNode.bottomUpTree(maxDepth);
34-
35-
for (int depth=minDepth; depth<=maxDepth; depth+=2){
36-
int iterations = 1 << (maxDepth - depth + minDepth);
37-
38-
check = 0;
39-
for (int i=1; i<=iterations; i++)
40-
{
41-
check += (TreeNode.bottomUpTree(depth)).itemCheck();
42-
}
43-
44-
Console.WriteLine("{0}\t trees of depth {1}\t check: {2}",
45-
iterations, depth, check);
46-
}
47-
48-
Console.WriteLine("long lived tree of depth {0}\t check: {1}",
49-
maxDepth, longLivedTree.itemCheck());
50-
}
51-
52-
53-
struct TreeNode
54-
{
55-
class Next
56-
{
57-
public TreeNode left, right;
58-
}
59-
60-
private Next next;
61-
62-
internal static TreeNode bottomUpTree(int depth){
63-
if (depth>0){
64-
return new TreeNode(
65-
bottomUpTree(depth-1)
66-
, bottomUpTree(depth-1)
67-
);
68-
}
69-
else {
70-
return new TreeNode();
71-
}
72-
}
73-
74-
TreeNode(TreeNode left, TreeNode right){
75-
this.next = new Next ();
76-
this.next.left = left;
77-
this.next.right = right;
78-
}
79-
80-
internal int itemCheck(){
81-
// if necessary deallocate here
82-
if (next==null) return 1;
83-
else return 1 + next.left.itemCheck() + next.right.itemCheck();
84-
}
85-
}
20+
class BinaryTrees
21+
{
22+
const int minDepth = 4;
23+
24+
public static void Main(String[] args)
25+
{
26+
int n = 0;
27+
if (args.Length > 0) n = Int32.Parse(args[0]);
28+
29+
int maxDepth = Math.Max(minDepth + 2, n);
30+
int stretchDepth = maxDepth + 1;
31+
32+
int check = (TreeNode.bottomUpTree(stretchDepth)).itemCheck();
33+
Console.WriteLine("stretch tree of depth {0}\t check: {1}", stretchDepth, check);
34+
35+
TreeNode longLivedTree = TreeNode.bottomUpTree(maxDepth);
36+
37+
for (int depth = minDepth; depth <= maxDepth; depth += 2)
38+
{
39+
int iterations = 1 << (maxDepth - depth + minDepth);
40+
41+
check = 0;
42+
for (int i = 1; i <= iterations; i++)
43+
{
44+
check += (TreeNode.bottomUpTree(depth)).itemCheck();
45+
}
46+
47+
Console.WriteLine("{0}\t trees of depth {1}\t check: {2}",
48+
iterations, depth, check);
49+
}
50+
51+
Console.WriteLine("long lived tree of depth {0}\t check: {1}",
52+
maxDepth, longLivedTree.itemCheck());
53+
}
54+
55+
56+
struct TreeNode
57+
{
58+
class Next
59+
{
60+
public TreeNode left, right;
61+
}
62+
63+
private Next next;
64+
65+
internal static TreeNode bottomUpTree(int depth)
66+
{
67+
if (depth > 0)
68+
{
69+
return new TreeNode(
70+
bottomUpTree(depth - 1)
71+
, bottomUpTree(depth - 1)
72+
);
73+
}
74+
else
75+
{
76+
return new TreeNode();
77+
}
78+
}
79+
80+
TreeNode(TreeNode left, TreeNode right)
81+
{
82+
this.next = new Next();
83+
this.next.left = left;
84+
this.next.right = right;
85+
}
86+
87+
internal int itemCheck()
88+
{
89+
// if necessary deallocate here
90+
if (next == null) return 1;
91+
else return 1 + next.left.itemCheck() + next.right.itemCheck();
92+
}
93+
}
94+
}
8695
}

0 commit comments

Comments
 (0)