Link to home
Start Free TrialLog in
Avatar of Wim ten Brink
Wim ten BrinkFlag for Netherlands

asked on

[Competition] Build the fastest StringReplace function with Delphi for .NET!

Well, I already asked this Q in https://www.experts-exchange.com/questions/21982965/Competition-Build-the-fastest-StringReplace-function.html but that would be a WIN32 solution. I want to know if the same thing can be archieved with Delphi for .NET so in this Q I want a similar solution, but it MUST run with Delphi 8, Delphi 2005 or Delphi 2006 as a .NET solution!
Yeah, that makes it more challenging too. :-)

Below just the copy&paste from my other question... :-)

To continue on what was asked in https://www.experts-exchange.com/questions/21980883/Performance-contest.html 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.
Avatar of tobjectpascal
tobjectpascal

I've been playing (i say playing because that's all i was doing with it) with Turbo Delphi .NET for the last 2  days now...

Var
 P: Pointer; //not allowed

Asm
  //Not allowed
End;


I'm presuming Pchar is not allowed either....

So how would one do optimizations in .NET?
Avatar of Wim ten Brink

ASKER

Yep. That's the challenge... :-)
ASKER CERTIFIED SOLUTION
Avatar of alkisg
alkisg
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Listening :)

A re patrida Alki :)
Nice try, alkisq. Just shows that .NET has some pretty fast code. Oh, well... The main question here is basically how we can optimize Delphi code for .NET, especially when dealing with strings. It's clear that Delphi's StringReplace is way too slow...
it also means all langauges if you use just MS .NET routines are on equal footing, C# is not faster than VB as it's all on the same back end compiler....

It literally comes down to which language do you prefer? they're all just as fast as each other...
Actually, I tend to disagree. While C#, VB.NET and Delphi for .NET code would be similar in speed after compilation, there will be some differences when you start using some of the language-specific functions. As alkisq already showed, using the Delphi StringReplace function in .NET would put you at a huge disadvantage compared to the standard StringBuilder.Replace method. While this function isn't really a standard Pascal statement, any experienced Delphi developer would quickly learn the faster alternatives. (Which is what this thread is about.) However, The Pascal standard has plenty of standard commands which will be implemented differently from e.g. similar C# methods. This tends to be compiler-dependant and even the compiler options might speed things up further too.
If you would write a simple algorithm in VB.NET, Delphi for .NET and C# and would compare the speeds of these three functions, I predict that their speeds will differ greatly. By how much? That's something we'll discover in the future, I think.
(Actually, I even think that Borland C# Builder and MS C# would compile the same sourcefiles but the result will show different performances.)
Seems StringBuilder.Replace is the fastest solution this far. :-) Well done, alkisg. But I'm keeping it open just a little bit longer. :-)
It seems that .NET coders don't care much about speed :~)

@CodedK: Geia sou patrida!
Sorry for the offtopic comment...

@Alkig geia sou file :)
To programma sou poli oraio kai xrisimo...
( Ena provlima pou tha prepei na koitakseis :
  Spaei eykola i prostasia pou exeis balei... Me Brute force thelei 10-20 kleidia na dokimaseis... Des to.)
Sorry by me too, last offtopic:

@CodedK: steile kana email bre na ta poume (des to profile mou)... Apokleietai na to espases, ta kleidia pou dexetai me brute force to kanoun na mhn trexei swsta... Kai exei akoma 6 epipeda prostasias, to perissotero pou exoun spasei einai mexri to 2o...
Well, alkisg... Congratulations. You made first place with the fastest solution. :-P
Wow... thanks! Not bad for my first attempt in Delphi.NET, heh? :~)
Seriously, what does the small participation mean? That programmers prefer C# over Delphi.NET or that they don't care about optimizations?