Skip to content

Instantly share code, notes, and snippets.

@VisualMelon VisualMelon/StringReversers.cs Secret
Last active Jan 21, 2019

Embed
What would you like to do?
String Reverse Benchmark
BenchmarkDotNet=v0.11.3, OS=Windows 10.0.17134.472 (1803/April2018Update/Redstone4)
Intel Core i7-3770 CPU 3.40GHz (Ivy Bridge), 1 CPU, 8 logical and 4 physical cores
Frequency=3312787 Hz, Resolution=301.8606 ns, Timer=TSC
.NET Core SDK=2.1.402
[Host] : .NET Core 2.1.4 (CoreCLR 4.6.26814.03, CoreFX 4.6.26814.02), 64bit RyuJIT
DefaultJob : .NET Core 2.1.4 (CoreCLR 4.6.26814.03, CoreFX 4.6.26814.02), 64bit RyuJIT
Method | TestString | Mean | Error | StdDev |
---------------------- |---------------------- |-------------:|-----------:|----------:|
ReverseLinq | | 81.472 ns | 0.1537 ns | 0.1284 ns |
ReverseChar1a | | 7.946 ns | 0.1156 ns | 0.1081 ns |
ReverseChar1b | | 7.518 ns | 0.0177 ns | 0.0157 ns |
ReverseChar2 | | 7.507 ns | 0.0232 ns | 0.0206 ns |
ReverseStringBuilders | | 12.894 ns | 0.1740 ns | 0.1542 ns |
ReverseLinq | It's (...)ow it [39] | 671.946 ns | 1.9982 ns | 1.8691 ns |
ReverseChar1a | It's (...)ow it [39] | 61.711 ns | 0.0774 ns | 0.0604 ns |
ReverseChar1b | It's (...)ow it [39] | 61.952 ns | 0.2241 ns | 0.1986 ns |
ReverseChar2 | It's (...)ow it [39] | 48.417 ns | 0.0877 ns | 0.0732 ns |
ReverseStringBuilders | It's (...)ow it [39] | 203.733 ns | 0.7540 ns | 0.6684 ns |
ReverseLinq | Magpies | 235.176 ns | 0.5324 ns | 0.4446 ns |
ReverseChar1a | Magpies | 23.412 ns | 0.0979 ns | 0.0916 ns |
ReverseChar1b | Magpies | 24.032 ns | 0.0582 ns | 0.0544 ns |
ReverseChar2 | Magpies | 22.401 ns | 0.1193 ns | 0.0996 ns |
ReverseStringBuilders | Magpies | 44.056 ns | 0.1313 ns | 0.1097 ns |
ReverseLinq | ifhia(...) oiha [432] | 4,102.307 ns | 10.4197 ns | 9.2368 ns |
ReverseChar1a | ifhia(...) oiha [432] | 454.764 ns | 1.0899 ns | 1.0195 ns |
ReverseChar1b | ifhia(...) oiha [432] | 453.764 ns | 2.3080 ns | 2.0460 ns |
ReverseChar2 | ifhia(...) oiha [432] | 400.077 ns | 1.0022 ns | 0.7824 ns |
ReverseStringBuilders | ifhia(...) oiha [432] | 1,630.961 ns | 6.1210 ns | 5.4261 ns |
using BenchmarkDotNet.Attributes;
using System;
using System.Linq;
using System.Text;
namespace StrReverse
{
public class Program
{
public static class StringReversers
{
public static string ReverseLinq(string str) => new String(str.Reverse().ToArray());
public static string ReverseChar1a(string str)
{
char[] buffer = new char[str.Length];
for (int i = 0; i < buffer.Length; i++)
{
buffer[i] = str[buffer.Length - i - 1];
}
return new string(buffer);
}
public static string ReverseChar1b(string str)
{
char[] buffer = new char[str.Length];
for (int i = 0; i < buffer.Length; i++)
{
buffer[buffer.Length - i - 1] = str[i];
}
return new string(buffer);
}
public static string ReverseChar2(string str)
{
char[] buffer = new char[str.Length];
for (int i = 0, j = buffer.Length - 1; i <= j; i++, j--)
{
buffer[i] = str[j];
buffer[j] = str[i];
}
return new string(buffer);
}
public static string ReverseStringBuilder(string str)
{
var reversedString = new StringBuilder();
for (int i = str.Length - 1; i >= 0; i--)
{
reversedString.Append(str[i]);
}
return reversedString.ToString();
}
}
public class Benchmarks
{
[Params("", "Magpies", "It's the end of the world as we know it",
"ifhiasdfhiad fhidafhiadfiadfhaodf uhaohuaouf hoad gh oa ghoaghaohoaf hoa fhiao ghoaghiao ghia oghoaghoa ghoa ghoahigoaihgoaih goai ghoaihgioaghoiahgoiahgoiahgoiahgo iahoia hgoiah goia hoaid hgoadihgioah g oahgoa fhigo ia hgo ahgoa hgaoif hgao fhgoa fhgoa ifhgoa fhgo ahgoa fhgo afhgoa ihfgoia fhgoa fhgoa fhgo ahfog haofg hao ghaofi ghaofighoifaghioafhgioafdhgoifdahgioafhgioafhiohfofdh oihoi hoiah ioahfoihdfiohdaoidfh oaidh oiha")]
public string TestString { get; set; }
[Benchmark]
public void ReverseLinq()
{
StringReversers.ReverseLinq(TestString);
}
[Benchmark]
public void ReverseChar1a()
{
StringReversers.ReverseChar1a(TestString);
}
[Benchmark]
public void ReverseChar1b()
{
StringReversers.ReverseChar1b(TestString);
}
[Benchmark]
public void ReverseChar2()
{
StringReversers.ReverseChar2(TestString);
}
[Benchmark]
public void ReverseStringBuilder()
{
StringReversers.ReverseStringBuilder(TestString);
}
}
static void Main(string[] args)
{
string str;
str = "EvenNumberOfLetters!";
Console.WriteLine(StringReversers.ReverseLinq(str));
Console.WriteLine(StringReversers.ReverseChar1a(str));
Console.WriteLine(StringReversers.ReverseChar1b(str));
Console.WriteLine(StringReversers.ReverseChar2(str));
Console.WriteLine(StringReversers.ReverseStringBuilder(str));
str = "OddNumberOfLetters!";
Console.WriteLine(StringReversers.ReverseLinq(str));
Console.WriteLine(StringReversers.ReverseChar1a(str));
Console.WriteLine(StringReversers.ReverseChar1b(str));
Console.WriteLine(StringReversers.ReverseChar2(str));
Console.WriteLine(StringReversers.ReverseStringBuilder(str));
var summary = BenchmarkDotNet.Running.BenchmarkRunner.Run<Benchmarks>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.