Skip to content

Instantly share code, notes, and snippets.

@phatboyg
Created July 18, 2012 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phatboyg/3137095 to your computer and use it in GitHub Desktop.
Save phatboyg/3137095 to your computer and use it in GitHub Desktop.
Benchmarque Blog Post Code
public interface AppendText
{
string Append(params string[] args);
}
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;
}
}
public class JoinAppendText :
AppendText
{
public string Append(params string[] args)
{
return string.Join("", args);
}
}
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}; }
}
}
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment