Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 95521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:40:16+00:00 2026-05-10T23:40:16+00:00

When doing a simple performance measurement, I was astonished to see that calling String.IndexOf(char)

  • 0

When doing a simple performance measurement, I was astonished to see that calling String.IndexOf(char) was actually slower than doing it manually! Is this really true?! Here is my test code:

const string str = @'91023m lkajsdfl;jkasdf;piou-09324\\adf \asdf\45\ 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; } 

I ran each method 25 million times, and measured the time using a Stopwatch. The manual version was consistently 25% faster than the other one.

Any thoughts?!


Edit 2: Running the tests on a different machine with .NET 4.0 yields results very similar to those in Marc Gravell’s answer. String.IndexOf(char) is faster than manual searching.


Edit: Today (Jan 4 ’09) I wanted to check this issue in a more comprehensive way, so I created a new project, and wrote the code you’ll find below. I got the following results when running release mode from cmd for 100 million iterations:

  -   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 

I ran these tests at least 20 times, getting almost the same results every time. My machine is XP SP3, VS 2008 SP1, P4 3.0 GHz with no hyper-threading and 1 GB RAM. I find the results really strange. As you can see, String.IndexOf was about 33% slower than my PublicIndexOf. Even stranger, I wrote the same method as internal, and it was about 8% slower than the public one! I do not understand what’s happening, and I hope you could help me understand!

The testing code is below. (Sorry for the repeated code, but I found that using a delegate showed different timings, with the public and internal methods taking the same time.)

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 \asdf\45\ 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);     } } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T23:40:17+00:00Added an answer on May 10, 2026 at 11:40 pm

    (updated)

    With your newly posted rig, I get the numbers below, which are in line with what I would expect:

    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) 

    (original)

    I cannot reproduce your numbers in release at the console. I did 25M iterations, with results:

    • 1556ms, (825000000) (testIndexOf)
    • 1798ms, (825000000) (testManualIndexOf)

    So IndexOf is quicker. I suspect your test rig is not being run in release mode at the command line (you can’t run performance tests with the debugger attached; even the IDE adds too much overhead).

    Here is my rig:

    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);  } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 233k
  • Answers 233k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think it is a bug introduced in one of… May 13, 2026 at 5:46 am
  • Editorial Team
    Editorial Team added an answer It is the link step in the build process that… May 13, 2026 at 5:46 am
  • Editorial Team
    Editorial Team added an answer That sounds like you need a client side solution then,… May 13, 2026 at 5:46 am

Related Questions

I read somewhere that when rendering a lot of 3D objects one could merge
Is it possible to do a simple count(*) query in a PHP script while
I'm not getting any errors as such just a minor performance issue. EXPLAIN SELECT
Why would someone use numeric(12, 0) datatype for a simple integer ID column? If

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.