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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:21:16+00:00 2026-05-27T05:21:16+00:00

I currently process sections of a string like this: for (i, j) in huge_list_of_indices:

  • 0

I currently process sections of a string like this:

for (i, j) in huge_list_of_indices:
    process(huge_text_block[i:j])

I want to avoid the overhead of generating these temporary substrings. Any ideas? Perhaps a wrapper that somehow uses index offsets? This is currently my bottleneck.

Note that process() is another python module that expects a string as input.

Edit:

A few people doubt there is a problem. Here are some sample results:

import time
import string
text = string.letters * 1000

def timeit(fn):
    t1 = time.time()
    for i in range(len(text)):
        fn(i)
    t2 = time.time()
    print '%s took %0.3f ms' % (fn.func_name, (t2-t1) * 1000)

def test_1(i):
    return text[i:]

def test_2(i):
    return text[:]

def test_3(i):
    return text

timeit(test_1)
timeit(test_2)
timeit(test_3)

Output:

test_1 took 972.046 ms
test_2 took 47.620 ms
test_3 took 43.457 ms
  • 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-27T05:21:16+00:00Added an answer on May 27, 2026 at 5:21 am

    I think what you are looking for are buffers.

    The characteristic of buffers is that they “slice” an object supporting the buffer interface without copying its content, but essentially opening a “window” on the sliced object content. Some more technical explanation is available here. An excerpt:

    Python objects implemented in C can export a group of functions called the “buffer interface.” These functions can be used by an object to expose its data in a raw, byte-oriented format. Clients of the object can use the buffer interface to access the object data directly, without needing to copy it first.

    In your case the code should look more or less like this:

    >>> s = 'Hugely_long_string_not_to_be_copied'
    >>> ij = [(0, 3), (6, 9), (12, 18)]
    >>> for i, j in ij:
    ...     print buffer(s, i, j-i)  # Should become process(...)
    Hug
    _lo
    string
    

    HTH!

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

Sidebar

Related Questions

We've got a process currently which causes ASP.NET websites to be redeployed. The code
We currently have a process where we receive an inventory file from a vendor
I'm currently in the process of messing about with Magento, and I'm just wondering
I am currently in the process of making an application that runs 24/7 and
I'm currently in the process of setting up a continuous integration environment at work.
I am currently in the process of creating my own blog and I have
We're currently in the process of setting up a source control/build/and more-server for .NET
I am currently in the process of restructuring my local Subversion repository by adding
I'm currently in the process of choosing a project for a grad-level compiler course
I am currently in the process of making a new ASP.net MVC website, and

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.