1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- /// dotnet new console
- /// dotnet add package BenchmarkDotNet
- /// paste this code in Program.cs
- /// dotnet publish -r win10-x64 -c Release
- /// run an .exe file
- using System;
- using System.Collections.Generic;
- using System.Text;
- using BenchmarkDotNet;
- using BenchmarkDotNet.Attributes;
- using BenchmarkDotNet.Running;
- [MemoryDiagnoser]
- public class Program
- {
- const int Count = 10000;
- static void Main(string[] args)
- {
- var summary = BenchmarkRunner.Run<Program>();
- }
-
- [Benchmark]
- public void PlusOperatorConcatTest()
- {
- string str = "";
- for (int i = 0; i < Count; i++)
- {
- str += "A";
- }
- }
- [Benchmark]
- public void FixedListConcatTest()
- {
- var parts = new List<string>(Count);
- for (int i = 0; i < Count; i++)
- {
- parts.Add("A");
- }
- var str = String.Join(String.Empty, parts);
- }
- [Benchmark]
- public void UnfixedListConcatTest()
- {
- var parts = new List<string>();
- for (int i = 0; i < Count; i++)
- {
- parts.Add("A");
- }
- var str = String.Join(String.Empty, parts);
- }
- [Benchmark]
- public void FixedStringBuilderTest()
- {
- var sb = new StringBuilder(Count);
- for (int i = 0; i < Count; i++)
- {
- sb.Append("A");
- }
- var str = sb.ToString();
- }
- [Benchmark]
- public void UnfixedStringBuilderTest()
- {
- var sb = new StringBuilder();
- for (int i = 0; i < Count; i++)
- {
- sb.Append("A");
- }
- var str = sb.ToString();
- }
- }
|