This is the code I ran in release mode. (.NET Core)
class Program
{
const string _str = "abcdefghijklmnopqrstuvwxyz";
static void Main(string\[\] args)
{
Foo("A", () => \_str.Contains("~"));
Foo ("B", () => \_str.Contains('~'));
Foo("C", () => \_str.IndexOf("~") >= 0);
Foo("D", () => \_str.IndexOf('~') >= 0);
Foo("E", () => \_str.IndexOf("~", StringComparison.Ordinal) >= 0);
}
static void Foo(string s, Func<bool> action)
{
var sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 1000000; i++)
{
action();
}
sw.Stop();
Console.WriteLine($"{s} - {sw.Elapsed.Milliseconds}");
}
}
Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com