The fastest appender in StringBuilder is the character appender. If you replace the char table with a flat string you will gain better performance depending on the size of the input up to 30%. Also defining a capacity for the StringBuilder may become a performance killer for really long input on heavy loaded systems.
public static string YesICan(IList<byte> input)
{
if (input == null || input.Count < 1)
return string.Empty;
int
x = 0,
i = 0;
var sb = new StringBuilder();
while (i < input.Count)
{
x = input[i++] << 1;
sb.Append(flat[x]);
sb.Append(flat[x + 1]);
}
return sb.ToString();
}
The fastest appender in StringBuilder is the character appender. If you replace the char table with a flat string you will gain better performance depending on the size of the input up to 30%. Also defining a capacity for the StringBuilder may become a performance killer for really long input on heavy loaded systems.
private static readonly string flat = string.Join("", _base16CharTable);
public static string YesICan(IList<byte> input)
{
if (input == null || input.Count < 1)
return string.Empty;
int
x = 0,
i = 0;
var sb = new StringBuilder();
while (i < input.Count)
{
x = input[i++] << 1;
sb.Append(flat[x]);
sb.Append(flat[x + 1]);
}
return sb.ToString();
}
For an unsafe code approach (beating all) check this:
http://www.codeproject.com/Tips/447938/High-performance-Csharp-byte-array-to-hex-string-t
Cheers