-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBenchMarkCompareJoiningStringsWithFormatConcatJoin.cs
More file actions
41 lines (35 loc) · 1.21 KB
/
BenchMarkCompareJoiningStringsWithFormatConcatJoin.cs
File metadata and controls
41 lines (35 loc) · 1.21 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
/// <summary>
/// Quick test of the speed of joining two strings using Format, Concat, and Join
/// </summary>
class Program
{
static void Main(string[] args)
{
for (var i = 0; i < 5; i++)
{
System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
string str1 = null, str2 = null, str3 = null;
for (int j = 0; j < 1000000; j++)
{
str1 = null;
str1 = string.Format("{0}${1}", "ThisIsA", "Test");
}
var a = sw.ElapsedMilliseconds;
for (int j = 0; j < 1000000; j++)
{
str2 = null;
str2 = string.Concat("ThisIsA", "$", "Test");
}
var b = sw.ElapsedMilliseconds;
for (int j = 0; j < 1000000; j++)
{
str3 = null;
str3 = string.Join("&", "ThisIsA", "Test");
}
var c = sw.ElapsedMilliseconds;
System.Console.WriteLine("Format | Concat | Join ");
System.Console.WriteLine(a + " | " + (b - a) + " | " + (c - b));
System.Console.WriteLine(str1 + " | " + str2 + " | " + str3);
}
}
}