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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:31:33+00:00 2026-05-28T13:31:33+00:00

There is a small part of our software that inserts a string before and

  • 0

There is a small part of our software that inserts a string before and after a certain matched string on a huge chunk of code (average length 900000 characters).

Example:

Lorem Ipsum is simply dummy text of the printing and typesetting
industry. Lorem Ipsum has been the industry’s standard dummy text ever
since the 1500s, when an unknown printer took a galley of type and
scrambled it to make a type specimen book.

Replaced with

Lorem Ipsum is simply dummy text of the <span class="class1 class2">printing</span> and typesetting industry. Lorem Ipsum has <span class="class1 class2 class3">been</span> the industry’s standard dummy text <span class="class1">ever since the 1500s</span>, when an unknown printer took a galley of type and scrambled it to make a type specimen book.


Ok, so far so good. We could just search and replace, but the contents are somewhat semantical relative so printing in that case was replaced, but might not be in some other place of the text.
What we did was index where we wanted to replace the text, so for every replacement we get a starting position and an end position.

Current code:

new_val = huge_string_goes_here
entities.each { |entity|
    add_before = "<span class=\"#{entity.getStuff}\">"
    add_after = '</span>'

    new_val.insert(entity.getStart+increment, add_before)
    increment = increment+add_before.length
    new_val.insert(entity.getEnd+increment, add_after)
    increment = increment+add_after.length
}

Analysing a 900000 character long string is taking around 15-20 seconds.

Does anyone have any suggestions on how we could optimize it?

Thank you

  • 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-28T13:31:34+00:00Added an answer on May 28, 2026 at 1:31 pm

    Consider writing a C extension module for Ruby which could find the match indices for you – this sort of operation should be much faster natively than with interpreted code. Once you’ve got the indices you could use Ruby to insert the before/after text, or if the performance still needs a boost then consider doing it all in C.

    Note that, as with any optimization, the critical thing is to make sure your “optimization” actually improves upon the non-optimized code. Write a benchmark for some sample cases and keep track of the amount of time it takes the pure Ruby code, then run the same benchmark using your native extension and see if the performance is in fact better.

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

Sidebar

Related Questions

I am working on small scale deployment system for our in-house software that manages
I'm part of a team that operates a portal for our company. There are
Hey there. I'm trying to write a small program that will read the four
For very small teams, or an individual developer, is there a source code control
We have a table in our system that stores resource key/value pairs. There are
I am developing a small part of a PHP application with some python code.
There is small system, where a database table as queue on MSSQL 2005. Several
is there any small working program for recieving from and sending data to client
In an MFC application there is a small rectangular region where the scroll bars
In Karl Seguin's Foundations of Programming there is a small section on using the

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.