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

  • Home
  • SEARCH
  • 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 7922843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:06:39+00:00 2026-06-03T17:06:39+00:00

I have a very long string ( 60MB in size) in which I need

  • 0

I have a very long string (60MB in size) in which I need to find how many pairs of ‘<‘ and ‘>’ are in there.


I have first tried my own way:

        char pre = '!';
        int match1 = 0;
        for (int j = 0; j < html.Length; j++)
        {
            char c = html[j];
            if (pre == '<' && c == '>') //find a match
            {
                pre = '!';
                match1++;
            }
            else if (pre == '!' && c == '<')
                pre = '<';
        }

The above code runs on my string for roughly 1000 ms.


Then I tried using string.IndexOf

        int match2 = 0;
        int index = -1;
        do
        {
            index = html.IndexOf('<', index + 1);
            if (index != -1) // find a match
            {
                index = html.IndexOf('>', index + 1);
                if (index != -1)
                   match2++;
            }
        } while (index != -1);

The above code runs for only around 150 ms.


I am wondering what is the magic that makes string.IndexOf runs so fast?

Anyone can inspire me?


Edit

Ok, according to @BrokenGlass’s answer. I modified my code in the way that they don’t check the pairing, instead, they check how many ‘<‘ in the string.


        for (int j = 0; j < html.Length; j++)
        {
            char c = html[j];
            if (c == '>')
            {
                match1++;
            }
        }

the above code runs for around 760 ms.


Using IndexOf

        int index = -1;
        do
        {
            index = html.IndexOf('<', index + 1);
            if (index != -1)
            {
                match2++;
            }
        } while (index != -1);

The above code runs for about 132 ms. still very very fast.


Edit 2

After read @Jeffrey Sax comment, I realised that I was running in VS with Debug mode.

Then I built and ran in release mode, ok, IndexOf is still faster, but not that faster any more.

Here is the results:

For the pairing count: 207ms VS 144ms

For the normal one char count: 141ms VS 111ms.

My own codes’ performance was really improved.


Lesson learned: when you do the benchmark stuff, do it in release mode!

  • 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. Editorial Team
    Editorial Team
    2026-06-03T17:06:41+00:00Added an answer on June 3, 2026 at 5:06 pm

    Are you running your timings from within Visual Studio? If so, your code would run significantly slower for that reason alone.

    Aside from that, you are, to some degree, comparing apples and oranges. The two algorithms work in a different way.

    The IndexOf version alternates between looking for an opening bracket only and a closing bracket only. Your code goes through the whole string and keeps a status flag that indicates whether it is looking for an opening or a closing bracket. This takes more work and is expected to be slower.

    Here’s some code that does the comparison the same way as your IndexOf method.

    int match3 = 0;
    for (int j = 0; j < html.Length; j++) {
        if (html[j] == '<') {
            for (; j < html.Length; j++)
                if (html[j] == '>')
                    match3++;
        }
    }
    

    In my tests this is actually about 3 times faster than the IndexOf method. The reason? Strings are actually not quite as simple as sequences of individual characters. There are markers, accents, etc. String.IndexOf handles all that extra complexity properly, but it comes at a cost.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very long string which includes many new lines ( it's a
I have a const char* str with a very long string. I need to
I need to create a very long string in a program, and have been
I have this code: String s = A very long string containing + many
I have a string 'sSQL' that contains a very long tsql statement. I need
I have a very long String and I want to read character by character
I have a very long text and I need to hide everything except the
I have tried for a very long time now to figure out, why this
If I have a very long string (without spaces!). Can I force the browser
I have a variable x to which I want to assign a very long

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.