-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new perf benchmark for generic dictionary expansion (#836)
* Add new perf benchmark for generic dictionary expansion
- Loading branch information
1 parent
6ac5201
commit 91f0ca4
Showing
1 changed file
with
153 additions
and
0 deletions.
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
src/benchmarks/micro/coreclr/perflab/DictionaryExpansion.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Runtime.CompilerServices; | ||
using System.Collections.Generic; | ||
using BenchmarkDotNet.Attributes; | ||
using MicroBenchmarks; | ||
|
||
namespace PerfLabTests | ||
{ | ||
public class GenClass<T> | ||
{ | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
public Type FuncOnGenClass(int level) | ||
{ | ||
switch (level) | ||
{ | ||
case 0: return typeof(T); | ||
case 1: return typeof(List<T>); | ||
case 2: return typeof(List<List<T>>); | ||
case 3: return typeof(List<List<List<T>>>); | ||
case 4: return typeof(List<List<List<List<T>>>>); | ||
case 5: return typeof(List<List<List<List<List<T>>>>>); | ||
case 6: return typeof(List<List<List<List<List<List<T>>>>>>); | ||
case 7: return typeof(List<List<List<List<List<List<List<T>>>>>>>); | ||
case 8: return typeof(List<List<List<List<List<List<List<List<T>>>>>>>>); | ||
case 9: return typeof(List<List<List<List<List<List<List<List<List<T>>>>>>>>>); | ||
case 10: return typeof(List<List<List<List<List<List<List<List<List<List<T>>>>>>>>>>); | ||
case 11: return typeof(List<List<List<List<List<List<List<List<List<List<List<T>>>>>>>>>>>); | ||
case 12: return typeof(List<List<List<List<List<List<List<List<List<List<List<List<T>>>>>>>>>>>>); | ||
default: return typeof(List<List<List<List<List<List<List<List<List<List<List<List<List<T>>>>>>>>>>>>>); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
public Type FuncOnGenClass2(int level) | ||
{ | ||
switch (level) | ||
{ | ||
case 0: return typeof(T); | ||
case 1: return typeof(List<T>); | ||
case 2: return typeof(List<List<T>>); | ||
case 3: return typeof(List<List<List<T>>>); | ||
case 4: return typeof(List<List<List<List<T>>>>); | ||
case 5: return typeof(List<List<List<List<List<T>>>>>); | ||
case 6: return typeof(List<List<List<List<List<List<T>>>>>>); | ||
case 7: return typeof(List<List<List<List<List<List<List<T>>>>>>>); | ||
case 8: return typeof(List<List<List<List<List<List<List<List<T>>>>>>>>); | ||
case 9: return typeof(List<List<List<List<List<List<List<List<List<T>>>>>>>>>); | ||
case 10: return typeof(List<List<List<List<List<List<List<List<List<List<T>>>>>>>>>>); | ||
case 11: return typeof(List<List<List<List<List<List<List<List<List<List<List<T>>>>>>>>>>>); | ||
case 12: return typeof(List<List<List<List<List<List<List<List<List<List<List<List<T>>>>>>>>>>>>); | ||
default: return typeof(List<List<List<List<List<List<List<List<List<List<List<List<List<T>>>>>>>>>>>>>); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
public static void FuncOnGenClassTest() | ||
{ | ||
var o1 = new GenClass<string>(); | ||
var o2 = new GenClass<object>(); | ||
var o3 = new GenClass<DictionaryExpansion>(); | ||
|
||
for (int i = 0; i < 15; i++) | ||
o1.FuncOnGenClass(i); | ||
|
||
for (int i = 0; i < 15; i++) | ||
o2.FuncOnGenClass(i); | ||
|
||
for (int i = 0; i < 15; i++) | ||
o2.FuncOnGenClass2(i); | ||
|
||
for (int i = 0; i < 15; i++) | ||
o3.FuncOnGenClass(i); | ||
} | ||
} | ||
|
||
[BenchmarkCategory(Categories.CoreCLR, Categories.Perflab)] | ||
public class DictionaryExpansion | ||
{ | ||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
public static Type GenFunc<T>(int level) | ||
{ | ||
switch (level) | ||
{ | ||
case 0: return typeof(T); | ||
case 1: return typeof(List<T>); | ||
case 2: return typeof(List<List<T>>); | ||
case 3: return typeof(List<List<List<T>>>); | ||
case 4: return typeof(List<List<List<List<T>>>>); | ||
case 5: return typeof(List<List<List<List<List<T>>>>>); | ||
case 6: return typeof(List<List<List<List<List<List<T>>>>>>); | ||
case 7: return typeof(List<List<List<List<List<List<List<T>>>>>>>); | ||
case 8: return typeof(List<List<List<List<List<List<List<List<T>>>>>>>>); | ||
case 9: return typeof(List<List<List<List<List<List<List<List<List<T>>>>>>>>>); | ||
case 10: return typeof(List<List<List<List<List<List<List<List<List<List<T>>>>>>>>>>); | ||
case 11: return typeof(List<List<List<List<List<List<List<List<List<List<List<T>>>>>>>>>>>); | ||
case 12: return typeof(List<List<List<List<List<List<List<List<List<List<List<List<T>>>>>>>>>>>>); | ||
default: return typeof(List<List<List<List<List<List<List<List<List<List<List<List<List<T>>>>>>>>>>>>>); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
public static Type GenFunc2<T>(int level) | ||
{ | ||
switch (level) | ||
{ | ||
case 0: return typeof(T); | ||
case 1: return typeof(List<T>); | ||
case 2: return typeof(List<List<T>>); | ||
case 3: return typeof(List<List<List<T>>>); | ||
case 4: return typeof(List<List<List<List<T>>>>); | ||
case 5: return typeof(List<List<List<List<List<T>>>>>); | ||
case 6: return typeof(List<List<List<List<List<List<T>>>>>>); | ||
case 7: return typeof(List<List<List<List<List<List<List<T>>>>>>>); | ||
case 8: return typeof(List<List<List<List<List<List<List<List<T>>>>>>>>); | ||
case 9: return typeof(List<List<List<List<List<List<List<List<List<T>>>>>>>>>); | ||
case 10: return typeof(List<List<List<List<List<List<List<List<List<List<T>>>>>>>>>>); | ||
case 11: return typeof(List<List<List<List<List<List<List<List<List<List<List<T>>>>>>>>>>>); | ||
case 12: return typeof(List<List<List<List<List<List<List<List<List<List<List<List<T>>>>>>>>>>>>); | ||
default: return typeof(List<List<List<List<List<List<List<List<List<List<List<List<List<T>>>>>>>>>>>>>); | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
public static void GenFuncTest() | ||
{ | ||
for (int i = 0; i < 15; i++) | ||
GenFunc<string>(i); | ||
|
||
for (int i = 0; i < 15; i++) | ||
GenFunc<object>(i); | ||
|
||
for (int i = 0; i < 15; i++) | ||
GenFunc2<object>(i); | ||
|
||
for (int i = 0; i < 15; i++) | ||
GenFunc<DictionaryExpansion>(i); | ||
} | ||
|
||
// | ||
// This benchmark is used to measure the performance of generic dictionary lookups. | ||
// | ||
[Benchmark] | ||
public void ExpandDictionaries() | ||
{ | ||
GenClass<int>.FuncOnGenClassTest(); | ||
|
||
GenFuncTest(); | ||
} | ||
} | ||
} |