Just to prove once again that .NET IS slower than Delphi generated code in LOTS of scenarios:
In this post I created a few implementations for a StringReplace() replacement and found that my custom FastStringReplace() implementation was the fastest, correct?
Now, I want to check if it is REALLY fast. Why not compare with something like .NET super-duper-optimized-code?
In .NET, they told me somewhere that I could not do much more than string.Replace() method. So I did a simple WinForms application in C# using VS 2013 + .NET 4.5, using build configuration. The code is really simple:
for (int i=1; i <= 1000000; i++) {
sourceString = originalString;
s = sourceString.Replace(searchPattern, replacePattern);
}
You can test all the 6 test cases I did in my previous Delphi project. I won't give you all the numbers, just one:
Test case 1, 1000000 iterations:
C# code: 6.2 seconds
Delphi code: 0.98 seconds. Let's round it: 1.0 second.
Then, my Delphi native code runs 600% faster than C# code, correct?
So... There is no algorithm involved, no hidden compiler setting, no tricky cache thing, nothing. I just have one string and want to replace some parts of it and copy the new string to another variable.
But C# programmers will still notice that in .NET strings are immutable and this is creating a new string and blah, blah, blah. But Delphi code also creates a new string, because the whole content of the old string is copied to a new string and returned as the result. Of course the Delphi string is not immutable and there is a clear advantage here, but don't blame me! Blame Microsoft, Anders Hejlsberg, Obama, financial crisis...
Both C# and Delphi code are pretty basic and can be found in any software out there written in those languages.
Then another C# programmer I know told me: "Oh, but you didn't compare Delphi's standard StringReplace(), you used a custom code". Yes that's true. And that's the beauty behind non-managed, native languages like Delphi. I can use whatever I want, including assembly code. I'm not limited to classes and methods available in some framework. In fact I patched my Delphi program at runtime, and I don't even need to replace the standard StringReplace() calls with the FastStringReplace().
Also, in StackOverflow I found some "tips" to make the C# code faster: use StringBuilder class instead of string.Replace() method. But, creating a StringBuilder instance inside the loop makes no big difference.
No comments:
Post a Comment