summaryrefslogtreecommitdiff
path: root/tests/src/performance/perflab/EnumPerf.cs
blob: bf8d89b476c1125f75e2baaa0777e75f7191095e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.Xunit.Performance;
using System;
using System.Reflection;
using Xunit;

public enum Color
{
    Black,
    White,
    Red,
    Brown,
    Yellow,
    Purple,
    Orange
}

public class EnumPerf
{
    [Benchmark(InnerIterationCount=300000)]
    [InlineData(Color.Red)]
    public static void EnumCompareTo(Color color)
    {
        Color white = Color.White;

        foreach (var iteration in Benchmark.Iterations)
            using (iteration.StartMeasurement())
                for (int i = 0; i < Benchmark.InnerIterationCount; i++)
                    color.CompareTo(white);
    }

    [Benchmark(InnerIterationCount=300000)]
    public static Type ObjectGetType()
    {
        Type tmp = null;
        Color black = Color.Black;

        foreach (var iteration in Benchmark.Iterations)
            using (iteration.StartMeasurement())
                for (int i = 0; i < Benchmark.InnerIterationCount; i++)
                    tmp = black.GetType();

        return tmp;
    }

    [Benchmark(InnerIterationCount=300000)]
    public static Type ObjectGetTypeNoBoxing()
    {
        Type tmp = null;
        object black = Color.Black;

        foreach (var iteration in Benchmark.Iterations)
            using (iteration.StartMeasurement())
                for (int i = 0; i < Benchmark.InnerIterationCount; i++)
                    tmp = black.GetType();

        return tmp;
    }

    [Benchmark(InnerIterationCount=300000)]
    public static bool EnumEquals()
    {
        Color black = Color.Black;
        Color white = Color.White;
        bool tmp = false;

        foreach (var iteration in Benchmark.Iterations)
            using (iteration.StartMeasurement())
                for (int i = 0; i < Benchmark.InnerIterationCount; i++)
                    tmp = black.Equals(white);

        return tmp;
    }
}