troubleshooting Question

[Competition] Build the fastest StringReplace function!

Avatar of Wim ten Brink
Wim ten BrinkFlag for Netherlands asked on
42 Comments3 Solutions3632 ViewsLast Modified:
To continue on what was asked in I now want to pose a completely different challenge here. What I want is everyone to post a function EXACTLY like the StringReplace function in Delphi. Thus, your code must be something like this:

type TReplaceFlags = set of (rfReplaceAll, rfIgnoreCase);
function [Yourname]_StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string;

where [Yourname] is replaced by your name, of course. :-)

Now, the challenge is to write a version of this function which is (at least) twice as fast as the Delphi version. (I will test it with Delphi 2006 and will be measuring both the time required to get the result as the number of clockcycles.
Measurements will be done by using a huge file with text. This file will be \Borland\Delphi 2006\source\Win32\rtl\win|Windows.pas for Delphi 2006 which happens to be 1.21 megabytes in size. Thus quite large. The file will be read into a string and then passed to the StringReplace functions. The text which will be replaced will be chosen from a list with words, varying in length from 1 character to 20 characters or maybe even a bit more. The strings that they will be replaced with might have the same length or are either bigger or smaller. I will also do one run replacing a word that's not part of this sourcefile. (Which should result in no changes...)

I will have to write a simple console application which will run all the tests, which I will share here. Once that is done, I too will try to write a fast solution... :-)

There will be points for every solution which is twice as fast as the Delphi version. The fastest one will receive 100 points and will be accepted as answer. The second place will be rewarded with 75 points. third-fastest solution will receive 50 points and all other solutions will receive 20 points, if they are fast enough. This Q will be closed when there are 20 fast solutions here or when there haven't been any new solutions for about three weeks. Points will be divided over the top-20.
Feel free to provide as many solutions as you like. Just be clear if something is a new solution or a fix on an existing one.

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 3 Answers and 42 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 42 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros