-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBenchMarkRemoveCharArrayVsRegex.cs
More file actions
58 lines (48 loc) · 1.31 KB
/
BenchMarkRemoveCharArrayVsRegex.cs
File metadata and controls
58 lines (48 loc) · 1.31 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
// Compare removing a character using Array.FindAll vs. a Regex Replace
//
// Compiled: C# Visual Studio 2013
namespace TernarySearchTree
{
public static class Program
{
private const int SAMPLES = 500000;
public static void ArrayRemoveCharTest()
{
var results = new List<String>(SAMPLES);
var rnd = new Random();
var sp = Stopwatch.StartNew();
for (int i = 0; i < SAMPLES ; i++)
{
var str = rnd.Next(0, int.MaxValue).ToString();
var result = new string(Array.FindAll(str.ToArray(), x => x != '1'));
results.Add(result);
}
Console.WriteLine(sp.Elapsed);
}
private static Regex RemoveOneRgx = new Regex("1");
public static void RegexRemoveCharTest()
{
var results = new List<String>(SAMPLES);
var rnd = new Random();
var sp = Stopwatch.StartNew();
for (int i = 0; i < SAMPLES ; i++)
{
var str = rnd.Next(0, int.MaxValue).ToString();
var result = RemoveOneRgx.Replace(str, "");
results.Add(result);
}
Console.WriteLine(sp.Elapsed);
}
public static void Main()
{
GC.Collect(3, GCCollectionMode.Forced, true);
GC.WaitForPendingFinalizers();
ArrayRemoveCharTest();
RegexRemoveCharTest();
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}