-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntParseVsTryParseTest.cs
43 lines (37 loc) · 1.01 KB
/
IntParseVsTryParseTest.cs
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
using BenchmarkDotNet.Attributes;
using System;
namespace csharp_benchmarks
{
public class IntParseVsTryParseTest
{
[Params(1000000)]
public int ItemsCount;
private string[] stringToParse;
[GlobalSetup]
public void Init()
{
stringToParse = new string[ItemsCount];
Random rand = new Random();
for (int i = 0; i < ItemsCount; i++)
stringToParse[i]= rand.Next(0, 100).ToString();
}
[Benchmark]
public void ParseTest()
{
for (int i = 0; i < ItemsCount; i++)
int.Parse(stringToParse[i]);
}
[Benchmark]
public void TryParseTest()
{
for (int i = 0; i < ItemsCount; i++)
int.TryParse(stringToParse[i], out _);
}
[Benchmark]
public void ConvertTest()
{
for (int i = 0; i < ItemsCount; i++)
Convert.ToInt32(stringToParse[i]);
}
}
}