public
Created

Benchmarque Blog Post Code

  • Download Gist
AppendText.cs
C#
1 2 3 4
public interface AppendText
{
string Append(params string[] args);
}
ConcatAppendText.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
public class ConcatAppendText :
AppendText
{
public string Append(params string[] args)
{
string result = string.Empty;
 
for (int i = 0; i < args.Length; i++)
{
result += args[i];
}
 
return result;
}
}
JoinAppendText2.cs
C#
1 2 3 4 5 6 7 8
public class JoinAppendText :
AppendText
{
public string Append(params string[] args)
{
return string.Join("", args);
}
}
NameAppendBenchmark.cs
C#
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
public class NameAppendBenchmark :
Benchmark<AppendText>
{
static readonly string[] Names = new[]
{
"Adam", "Betty", "Charles", "David",
"Edward", "Frodo", "Gandalf", "Henry",
"Ida", "John", "King", "Larry", "Morpheus",
"Neo", "Peter", "Quinn", "Ralphie", "Samwise",
"Trinity", "Umma", "Vincent", "Wanda"
};
 
public void WarmUp(AppendText instance)
{
instance.Append(Names);
}
 
public void Shutdown(AppendText instance)
{
}
 
public void Run(AppendText instance, int iterationCount)
{
for (int i = 0; i < iterationCount; i++)
{
string result = instance.Append(Names);
}
}
 
public IEnumerable<int> Iterations
{
get { return new[] {1000, 10000}; }
}
}
StringBuilderAppendText.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14
public class StringBuilderAppendText :
AppendText
{
public string Append(params string[] args)
{
var builder = new StringBuilder();
 
for (int i = 0; i < args.Length; i++)
{
builder.Append(args[i]);
}
return builder.ToString();
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.