@@ -19,87 +19,94 @@ minor improvements by Alex Yakunin
19
19
using System . Runtime . CompilerServices ;
20
20
using System . Threading . Tasks ;
21
21
22
- public sealed class BinaryTrees
22
+ namespace BenchmarksGame
23
23
{
24
- public const int MinDepth = 4 ;
25
-
26
- public static void Main ( string [ ] args )
24
+ public sealed class BinaryTrees
27
25
{
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 ;
31
33
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 ( ) ) ;
34
36
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
+ {
42
47
// Parallelized computation for relatively large tasks
43
48
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
+ {
51
57
// Sequential computation for smaller tasks
52
58
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 ) ;
59
65
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
+ }
66
73
}
67
- }
68
74
69
- public struct TreeNode
70
- {
71
- public sealed class NodeData
75
+ public struct TreeNode
72
76
{
73
- public TreeNode Left , Right ;
74
-
75
- public NodeData ( TreeNode left , TreeNode right )
77
+ public sealed class NodeData
76
78
{
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
+ }
79
86
}
80
- }
81
87
82
- public NodeData Data ;
88
+ public NodeData Data ;
83
89
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
+ }
89
95
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
+ }
97
103
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
+ }
104
111
}
105
112
}
0 commit comments