@@ -18,16 +18,36 @@ minor improvements by Alex Yakunin
18
18
using System ;
19
19
using System . Runtime . CompilerServices ;
20
20
using System . Threading . Tasks ;
21
+ using Microsoft . Xunit . Performance ;
22
+
23
+ [ assembly: OptimizeForBenchmarks ]
24
+ [ assembly: MeasureGCCounts ]
21
25
22
26
namespace BenchmarksGame
23
27
{
24
- public sealed class BinaryTrees
28
+ public sealed class BinaryTrees_5
25
29
{
26
30
public const int MinDepth = 4 ;
27
31
28
- public static void Main ( string [ ] args )
32
+ public static int Main ( string [ ] args )
29
33
{
30
34
var n = args . Length == 0 ? 0 : int . Parse ( args [ 0 ] ) ;
35
+
36
+ int check = Bench ( n , true ) ;
37
+ int expected = 4398 ;
38
+
39
+ // Return 100 on success, anything else on failure.
40
+ return check - expected + 100 ;
41
+ }
42
+
43
+ [ Benchmark ( InnerIterationCount = 7 ) ]
44
+ public static void RunBench ( )
45
+ {
46
+ Benchmark . Iterate ( ( ) => Bench ( 16 , false ) ) ;
47
+ }
48
+
49
+ static int Bench ( int n , bool verbose )
50
+ {
31
51
var maxDepth = n < ( MinDepth + 2 ) ? MinDepth + 2 : n ;
32
52
var stretchDepth = maxDepth + 1 ;
33
53
@@ -44,8 +64,8 @@ public static void Main(string[] args)
44
64
var count = 0 ;
45
65
if ( depthCopy >= 17 )
46
66
{
47
- // Parallelized computation for relatively large tasks
48
- var miniTasks = new Task < int > [ iterationCount ] ;
67
+ // Parallelized computation for relatively large tasks
68
+ var miniTasks = new Task < int > [ iterationCount ] ;
49
69
for ( var i = 0 ; i < iterationCount ; i ++ )
50
70
miniTasks [ i ] = Task . Run ( ( ) => TreeNode . CreateTree ( depthCopy ) . CountNodes ( ) ) ;
51
71
Task . WaitAll ( miniTasks ) ;
@@ -54,21 +74,35 @@ public static void Main(string[] args)
54
74
}
55
75
else
56
76
{
57
- // Sequential computation for smaller tasks
58
- for ( var i = 0 ; i < iterationCount ; i ++ )
77
+ // Sequential computation for smaller tasks
78
+ for ( var i = 0 ; i < iterationCount ; i ++ )
59
79
count += TreeNode . CreateTree ( depthCopy ) . CountNodes ( ) ;
60
80
}
61
81
return $ "{ iterationCount } \t trees of depth { depthCopy } \t check: { count } ";
62
82
} ) ;
63
83
}
64
84
Task . WaitAll ( tasks ) ;
65
85
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 ) ;
86
+ if ( verbose )
87
+ {
88
+ int count = 0 ;
89
+ Action < string > printAndSum = ( string s ) =>
90
+ {
91
+ Console . WriteLine ( s ) ;
92
+ count += int . Parse ( s . Substring ( s . LastIndexOf ( ':' ) + 1 ) . TrimStart ( ) ) ;
93
+ } ;
94
+
95
+ printAndSum ( String . Format ( "stretch tree of depth {0}\t check: {1}" ,
96
+ stretchDepth , stretchDepthTask . Result ) ) ;
97
+ foreach ( var task in tasks )
98
+ printAndSum ( task . Result ) ;
99
+ printAndSum ( String . Format ( "long lived tree of depth {0}\t check: {1}" ,
100
+ maxDepth , maxDepthTask . Result ) ) ;
101
+
102
+ return count ;
103
+ }
104
+
105
+ return 0 ;
72
106
}
73
107
}
74
108
0 commit comments