Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const string str = @"91023m lkajsdfl;jkasdf;piou-09324\adf asdf45 65u 86 8\;";
- static int testIndexOf() { return str.IndexOf('\'); }
- static int testManualIndexOf() {
- string s = str;
- for (int i = 0; i < s.Length; ++i)
- if (s[i] == '\') return i;
- return -1;
- }
- - String. IndexOf : 00:00:07.6490042
- - MyString.PublicIndexOf : 00:00:05.6676471
- - MyString.InternIndexOf : 00:00:06.1191796
- - MyString.PublicIndexOf2: 00:00:09.1363687
- - MyString.InternIndexOf2: 00:00:09.1182569
- public static class MyString {
- public static int PublicIndexOf(string str, char value) {
- for (int i = 0; i < str.Length; ++i)
- if (str[i] == value) return i;
- return -1;
- }
- internal static int InternIndexOf(string str, char value) {
- for (int i = 0; i < str.Length; ++i)
- if (str[i] == value) return i;
- return -1;
- }
- public static int PublicIndexOf2(string str, char value, int startIndex) {
- if (startIndex < 0 || startIndex >= str.Length)
- throw new ArgumentOutOfRangeException("startIndex");
- for (; startIndex < str.Length; ++startIndex)
- if (str[startIndex] == value) return startIndex;
- return -1;
- }
- internal static int InternIndexOf2(string str, char value, int startIndex) {
- if (startIndex < 0 || startIndex >= str.Length)
- throw new ArgumentOutOfRangeException("startIndex");
- for (; startIndex < str.Length; ++startIndex)
- if (str[startIndex] == value) return startIndex;
- return -1;
- }
- }
- class Program {
- static void Main(string[] args) {
- int iterations = 100 * 1000 * 1000; // 100 millions
- char separator = '\';
- string str = @"91023m lkajsdfl;jkasdf;piou-09324\adf asdf45 65u 86 8\;";
- Stopwatch watch = new Stopwatch();
- // test String.IndexOf
- int sum = 0;
- watch.Start();
- for (int i = 0; i < iterations; ++i)
- sum += str.IndexOf(separator);
- watch.Stop();
- Console.WriteLine(" String. IndexOf : ({0}, {1})", watch.Elapsed, sum);
- // test MyString.PublicIndexOf
- sum = 0;
- watch.Reset(); watch.Start();
- for (int i = 0; i < iterations; ++i)
- sum += MyString.PublicIndexOf(str, separator);
- watch.Stop();
- Console.WriteLine("MyString.PublicIndexOf : ({0}, {1})", watch.Elapsed, sum);
- // test MyString.InternIndexOf
- sum = 0;
- watch.Reset(); watch.Start();
- for (int i = 0; i < iterations; ++i)
- sum += MyString.InternIndexOf(str, separator);
- watch.Stop();
- Console.WriteLine("MyString.InternIndexOf : ({0}, {1})", watch.Elapsed, sum);
- // test MyString.PublicIndexOf2
- sum = 0;
- watch.Reset(); watch.Start();
- for (int i = 0; i < iterations; ++i)
- sum += MyString.PublicIndexOf2(str, separator,0);
- watch.Stop();
- Console.WriteLine("MyString.PublicIndexOf2: ({0}, {1})", watch.Elapsed, sum);
- // test MyString.InternIndexOf2
- sum = 0;
- watch.Reset(); watch.Start();
- for (int i = 0; i < iterations; ++i)
- sum += MyString.InternIndexOf2(str, separator,0);
- watch.Stop();
- Console.WriteLine("MyString.InternIndexOf2: ({0}, {1})", watch.Elapsed, sum);
- }
- }
- String. IndexOf : (00:00:06.3134669, -994967296)
- MyString.PublicIndexOf : (00:00:07.0769368, -994967296)
- MyString.InternIndexOf : (00:00:08.3463652, -994967296)
- MyString.PublicIndexOf2: (00:00:12.0049268, -994967296)
- MyString.InternIndexOf2: (00:00:12.4344756, -994967296)
- static void Main()
- {
- const int LOOP = 25000000;
- int chk1 = 0, chk2 = 0;
- Stopwatch watch1 = Stopwatch.StartNew();
- for (int i = 0; i < LOOP; i++)
- {
- chk1 += testIndexOf();
- }
- watch1.Stop();
- Stopwatch watch2 = Stopwatch.StartNew();
- for (int i = 0; i < LOOP; i++)
- {
- chk2 += testManualIndexOf();
- }
- watch2.Stop();
- Console.WriteLine("{0}ms, ({1})", watch1.ElapsedMilliseconds, chk1);
- Console.WriteLine("{0}ms, ({1})", watch2.ElapsedMilliseconds, chk2);
- }
- static void Main(string[] args)
- {
- {
- Stopwatch sw = new Stopwatch();
- sw.Start();
- for (int i = 0; i < 25000000; i++)
- testIndexOf();
- sw.Stop();
- Console.WriteLine(sw.Elapsed); //2.27 s
- }
- {
- Stopwatch sw = new Stopwatch();
- sw.Start();
- for (int i = 0; i < 25000000; i++)
- testManualIndexOf();
- sw.Stop();
- Console.WriteLine(sw.Elapsed); //9.81 s
- }
- Console.ReadLine();
- }
- public static void Main(string[] args) {
- int nTrials = 10000000;
- Random rg = new Random(1);
- Stopwatch sw = new Stopwatch();
- for (int i = 0; i < nTrials; i++) {
- int len = 1 + rg.Next(250);
- char[] randomChar = new char[len];
- for(int j = 0; j < len; j++) {
- randomChar[j] = GetRandomLetterOrDigit(rg);
- }
- string s = new string(randomChar);
- char findChar = GetRandomLetterOrDigit(rg);
- sw.Start();
- int index = s.IndexOf(findChar);
- //int index = testManualIndexOf(s, findChar);
- sw.Stop();
- }
- Console.WriteLine(sw.ElapsedMilliseconds);
- }
- private static char GetRandomLetterOrDigit(Random rg) {
- char c;
- int rc = rg.Next(62);
- if (rc < 26) {
- c = (char)('A' + rc);
- }
- else if (rc < 52) {
- c = (char)('a' + rc - 26);
- }
- else {
- c = (char)('0' + rc - 52);
- }
- return c;
- }
- private static int testManualIndexOf(string s, char c) {
- if (s == null) {
- throw new ArgumentNullException("s");
- }
- for (int i = 0; i < s.Length; ++i) {
- if (s[i] == c) return i;
- }
- return -1;
- }
Add Comment
Please, Sign In to add comment