PHP – Slow String Manipulation

Posted by Simon Roberts on Stack Overflow See other posts from Stack Overflow or by Simon Roberts
Published on 2012-12-04T10:55:20Z Indexed on 2012/12/05 23:04 UTC
Read the original article Hit count: 180

Filed under:
|
|

I have some very large data files and for business reasons I have to do extensive string manipulation (replacing characters and strings). This is unavoidable. The number of replacements runs into hundreds of thousands.

It's taking longer than I would like. PHP is generally very quick but I'm doing so many of these string manipulations that it's slowing down and script execution is running into minutes. This is a pain because the script is run frequently.

I've done some testing and found that str_replace is fastest, followed by strstr, followed by preg_replace. I've also tried individual str_replace statements as well as constructing arrays of patterns and replacements.

I'm toying with the idea of isolating string manipulation operation and writing in a different language but I don't want to invest time in that option only to find that improvements are negligible. Plus, I only know Perl, PHP and COBOL so for any other language I would have to learn it first.

I'm wondering how other people have approached similar problems?

I have searched and I don't believe that this duplicates any existing questions.

© Stack Overflow or respective owner

Related posts about php

Related posts about string