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 7504753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:40:34+00:00 2026-05-29T21:40:34+00:00

I am using this simple algorithm for searching some text in document and taging

  • 0

I am using this simple algorithm for searching some text in document and taging on which page I found it

for (int i = 1; i <= a.PageCount; i++)
{
    Buf.Append(a.Pages[i].Text);
    String contain = Buf.ToString();
    if (contain != "")
    {
        // Inside is dictionary of keys and value contain page where I found it
        foreach (KeyValuePair<string, List<string>> pair in inside)
        {
              if (contain.Contains(pair.Key))
                  inside[pair.Key].Add((i).ToString());
        }
    }

    Buf.Clear();
 }

I have no problem with it, but when I search in 700 pages document and I am looking for over 500 keys, its very slow, took about 1-2 minutes to pass, is there any way how to speed it up? I am using c#

Thanks!

  • 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-05-29T21:40:38+00:00Added an answer on May 29, 2026 at 9:40 pm

    A few points:

    • Get rid of Buf; just assign a.Pages[i].Text directly to contain:
    • inside[pair.Key] wastes time looking up the value associated with that key; the time is wasted because you have a much cheaper reference to that object in pair.Value.
    • if you have a list of integer values, why are you storing them as strings?

    Sample code:

    for (int i = 1; i <= a.PageCount; i++)
    {
        String contain = a.Pages[i].Text
        if (contain != "")
        {
            // Inside is dictionary of keys and value contain page where I found it
            foreach (KeyValuePair<string, List<int>> pair in inside)
            {
                if (contain.Contains(pair.Key))
                    pair.Value.Add(i);
            }
        }
    }
    

    Finally, make sure Pages does in fact use a one-based index. Collections are more commonly zero-indexed.

    EDIT since Pages is a dictionary:

    foreach (KeyValuePair<int, Page> kvp in a.Pages)
    {
        string contain = kvp.Value.Text;
        if (contain == "")
            continue;
        foreach (KeyValuePair<string, List<int>> pair in inside)
            if (contain.Contains(pair.Key))
                pair.Value.Add(kvp.Key);
    }
    

    How many times did you time the first code sample? The time could vary depending on many external factors; the fact that a single run of one approach is faster or slower than a single run of another doesn’t really tell you much, especially since the suggestions I made probably don’t address the bulk of the problem.

    As someone else pointed out, the main problem is that you’re calling contain.Contains(pair.Key) 350,000 times; that’s probably your bottleneck. You can profile the method to find out if that is true. If it is true, then something like the Rabin Karp algorithm as suggested by Miserable Variable is probably your best bet.

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

Sidebar

Related Questions

This is simple text detection video made using an opencv. Any ideas how was
I'm using this simple regular expression to validate a hex string: ^[A-Fa-f0-9]{16}$ As you
I'm using this simple function: def print_players(players): tot = 1 for p in players:
Ho to make this simple xml with php using DOM? Full code will be
I'm using this jQuery urlencode and urldecode plugin - very simple and easy to
Can anybody help me with this simple code?? #include <iostream> using namespace std; void
I'm trying to develop a simple kernel using TASM, using this code: ; beroset.asm
What's the fastest way using either DOS scripting or PowerShell to run this simple
After loading a PHP template (using jQuery's load function), this simple script won't make
Since I'm beginner in J I've decided to solve a simple task using this

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.