|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Globalization; |
5 | 7 | using Microsoft.Xunit.Performance; |
| 8 | +using Xunit; |
6 | 9 |
|
7 | 10 | namespace System.Tests |
8 | 11 | { |
9 | 12 | public class Perf_Char |
10 | 13 | { |
11 | | - [Benchmark] |
12 | | - public static char Char_ToLower() |
| 14 | + private const int InnerIterations = 1_000_000; |
| 15 | + |
| 16 | + public static IEnumerable<object[]> Char_ChangeCase_MemberData() |
| 17 | + { |
| 18 | + yield return new object[] { 'A', "en-US" }; // ASCII upper case |
| 19 | + yield return new object[] { 'a', "en-US" }; // ASCII lower case |
| 20 | + yield return new object[] { '\u0130', "en-US" }; // non-ASCII, English |
| 21 | + yield return new object[] { '\u4F60', "zh-Hans" }; // non-ASCII, Chinese |
| 22 | + } |
| 23 | + |
| 24 | + [Benchmark(InnerIterationCount = InnerIterations)] |
| 25 | + [MemberData(nameof(Char_ChangeCase_MemberData))] |
| 26 | + public static char Char_ToLower(char c, string cultureName) |
| 27 | + { |
| 28 | + char ret = default(char); |
| 29 | + CultureInfo culture = new CultureInfo(cultureName); |
| 30 | + |
| 31 | + foreach (var iteration in Benchmark.Iterations) |
| 32 | + { |
| 33 | + using (iteration.StartMeasurement()) |
| 34 | + { |
| 35 | + for (int i = 0; i < InnerIterations; i++) |
| 36 | + { |
| 37 | + ret = Char.ToLower(c, culture); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + return ret; |
| 43 | + } |
| 44 | + |
| 45 | + [Benchmark(InnerIterationCount = InnerIterations)] |
| 46 | + [MemberData(nameof(Char_ChangeCase_MemberData))] |
| 47 | + public static char Char_ToUpper(char c, string cultureName) |
13 | 48 | { |
14 | 49 | char ret = default(char); |
| 50 | + CultureInfo culture = new CultureInfo(cultureName); |
15 | 51 |
|
16 | 52 | foreach (var iteration in Benchmark.Iterations) |
| 53 | + { |
17 | 54 | using (iteration.StartMeasurement()) |
18 | | - ret = Char.ToLower('A'); |
| 55 | + { |
| 56 | + for (int i = 0; i < InnerIterations; i++) |
| 57 | + { |
| 58 | + ret = Char.ToUpper(c, culture); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
19 | 62 |
|
20 | 63 | return ret; |
21 | 64 | } |
|
0 commit comments