3dd897db0f68f0f5fedccda5f48f4b3204714332
[platform/upstream/coreclr.git] / tests / src / JIT / Performance / CodeQuality / BenchI / Fib / Fib.cs
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 //
5
6 using Microsoft.Xunit.Performance;
7 using System;
8 using System.Runtime.CompilerServices;
9 using Xunit;
10
11 [assembly: OptimizeForBenchmarks]
12
13 namespace Benchstone.BenchI
14 {
15 public static class Fib
16 {
17
18 #if DEBUG
19     public const int Iterations = 1;
20 #else
21     public const int Iterations = 3500;
22 #endif
23
24     const int Number = 24;
25
26     static int Fibonacci(int x) {
27         if (x > 2) {
28             return (Fibonacci(x - 1) + Fibonacci(x - 2));
29         }
30         else {
31             return 1;
32         }
33     }
34
35     [MethodImpl(MethodImplOptions.NoInlining)]
36     static bool Bench() {
37         int fib = Fibonacci(Number);
38         return (fib == 46368);
39     }
40
41     [Benchmark]
42     public static void Test() {
43         foreach (var iteration in Benchmark.Iterations) {
44             using (iteration.StartMeasurement()) {
45                 for (int i = 0; i < Iterations; i++) {
46                     Bench();
47                 }
48             }
49         }
50     }
51
52     static bool TestBase() {
53         bool result = true;
54         for (int i = 0; i < Iterations; i++) {
55             result &= Bench();
56         }
57         return result;
58     }
59
60     public static int Main() {
61         bool result = TestBase();
62         return (result ? 100 : -1);
63     }
64 }
65 }