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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:19:53+00:00 2026-05-24T13:19:53+00:00

For very large strings (spanning multiple lines) is it faster to use Python’s built-in

  • 0

For very large strings (spanning multiple lines) is it faster to use Python’s built-in string search or to split the large string (perhaps on \n) and iteratively search the smaller strings?

E.g., for very large strings:

for l in get_mother_of_all_strings().split('\n'):
 if 'target' in l:
   return True
return False

or

return 'target' in get_mother_of_all_strings()
  • 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-24T13:19:55+00:00Added an answer on May 24, 2026 at 1:19 pm

    Probably Certainly the second, I don’t see any difference in doing a search in a big string or many in small strings. You may skip some chars thanks to the shorter lines, but the split operation has its costs too (searching for \n, creating n different strings, creating the list) and the loop is done in python.

    The string __contain__ method is implemented in C and so noticeably faster.

    Also consider that the second method aborts as soon as the first match is found, but the first one splits all the string before even starting to search inside it.

    This is rapidly proven with a simple benchmark:

    import timeit
    
    prepare = """
    with open('bible.txt') as fh:
        text = fh.read()
    """
    
    presplit_prepare = """
    with open('bible.txt') as fh:
        text = fh.read()
    lines = text.split('\\n')
    """
    
    longsearch = """
    'hello' in text
    """
    
    splitsearch = """
    for line in text.split('\\n'):
        if 'hello' in line:
            break
    """
    
    presplitsearch = """
    for line in lines:
        if 'hello' in line:
            break
    """
    
    
    benchmark = timeit.Timer(longsearch, prepare)
    print "IN on big string takes:", benchmark.timeit(1000), "seconds"
    
    benchmark = timeit.Timer(splitsearch, prepare)
    print "IN on splitted string takes:", benchmark.timeit(1000), "seconds"
    
    benchmark = timeit.Timer(presplitsearch, presplit_prepare)
    print "IN on pre-splitted string takes:", benchmark.timeit(1000), "seconds"
    

    The result is:

    IN on big string takes: 4.27126097679 seconds
    IN on splitted string takes: 35.9622690678 seconds
    IN on pre-splitted string takes: 11.815297842 seconds
    

    The bible.txt file actually is the bible, I found it here: http://patriot.net/~bmcgin/kjvpage.html (text version)

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

Sidebar

Related Questions

I have an app that needs to handle very large strings between a SQL
I have two very large strings and I am trying to find out their
I'm sending a very large string from one application to another on localhost using
I'm iterating over a very large set of strings, which iterates over a smaller
I am having a very large string, and when I read it in Java,
I have the need to compare very large file-based strings of equal length for
My game stores levels in large strings. Not very long, the length is about
I have multiple (many) files; each very large: file0.txt file1.txt file2.txt I do not
I have very large string lists and arrays and i found 2 issues that
I have a very large JSON string that I need to parse with in-browser

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.