A
Alexander Vasilevsky
U. StartWith works 10 times faster than String.StartsWith. What the hell?
U.StartWith: 00:00:00.0259870
String.StartsWith: 00:00:00.2118940
See code below
using System;
namespace ConsoleApplication1
{
class U
{
public static bool StartWith(string pattern, string number)
{
bool match = true;
for (int digitIndex = 0; digitIndex < pattern.Length && match;
digitIndex++)
{
match = match &&
number[digitIndex].Equals(pattern[digitIndex]);
}
return match;
}
}
class Program
{
static void Main(string[] args)
{
const int max = 1000000;
string[] myStrings = new string[max];
string[] patterns = new string[max];
var rnd = new Random();
for (int i = 0; i < max; i++)
{
myStrings = rnd.Next(1000000).ToString("{06}"); // 6
digits
patterns = rnd.Next(1000).ToString(); // 1-3 digits
}
DateTime tU = DateTime.Now;
for (int i = 0; i < max; i++)
{
U.StartWith(patterns, myStrings);
}
Console.WriteLine("U.StartWith: " + (DateTime.Now -
tU).ToString());
DateTime tString = DateTime.Now;
for (int i = 0; i < max; i++)
{
myStrings.StartsWith(patterns);
}
Console.WriteLine("String.StartsWith: " + (DateTime.Now -
tString).ToString());
}
}
}
http://www.alvas.net - Audio tools for C# and VB.Net developers + Christmas
Gift
U.StartWith: 00:00:00.0259870
String.StartsWith: 00:00:00.2118940
See code below
using System;
namespace ConsoleApplication1
{
class U
{
public static bool StartWith(string pattern, string number)
{
bool match = true;
for (int digitIndex = 0; digitIndex < pattern.Length && match;
digitIndex++)
{
match = match &&
number[digitIndex].Equals(pattern[digitIndex]);
}
return match;
}
}
class Program
{
static void Main(string[] args)
{
const int max = 1000000;
string[] myStrings = new string[max];
string[] patterns = new string[max];
var rnd = new Random();
for (int i = 0; i < max; i++)
{
myStrings = rnd.Next(1000000).ToString("{06}"); // 6
digits
patterns = rnd.Next(1000).ToString(); // 1-3 digits
}
DateTime tU = DateTime.Now;
for (int i = 0; i < max; i++)
{
U.StartWith(patterns, myStrings);
}
Console.WriteLine("U.StartWith: " + (DateTime.Now -
tU).ToString());
DateTime tString = DateTime.Now;
for (int i = 0; i < max; i++)
{
myStrings.StartsWith(patterns);
}
Console.WriteLine("String.StartsWith: " + (DateTime.Now -
tString).ToString());
}
}
}
http://www.alvas.net - Audio tools for C# and VB.Net developers + Christmas
Gift