.NET 6.0 is Slower than .NET Framework In Some String Operations
-
Some tests to check some functions not tested in link below: Performance Improvements in .NET 6 - .NET Blog (arrays-strings-spans) .NET 6.0 is (comparing its performance to .NET Framework 4.8): • 3x faster on String.Replace operations • 16x slower on String.IndexOf operations, • 1.4x faster on String.Substring operations • The same on String.Remove operations Copy paste the code below and compile with .NET 6.0 and .NET Framework 4.8 and see by yourself.
//Simple Benchmark test for working with Strings in different versions of .NET Framework
string test = "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. " +
"It has survived not only " +
"five centuries, but also the leap into electronic typesetting," +
" remaining essentially unchanged." +
" It was popularised in the 1960s with the release" +
" of Letraset sheets containing Lorem Ipsum passages," +
" and more recently with desktop publishing software like " +
"Aldus PageMaker including versions of Lorem Ipsum.";System.Diagnostics.Stopwatch K = new System.Diagnostics.Stopwatch();
K.Reset(); K.Start();
for (var v = 1; v <= 10000000; v++)
{
test = test.Replace("a", "bla bla bla bla");
test = test.Replace("bla bla bla bla", "a");
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.Replace]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 1000000; v++)
{
int i = test.IndexOf("including versions of Lorem Ipsum");
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.IndexOf]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 600000000; v++)
{
var s = test.Substring(25, 50);
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.SubString]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 90000000; v++)
{
var s = test.Remove(45, 60);
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.Remove]: {K.Elapsed.TotalSeconds} sec");System.Console.WriteLine("Press a key to exit...");
System.Console.ReadKey(); -
Some tests to check some functions not tested in link below: Performance Improvements in .NET 6 - .NET Blog (arrays-strings-spans) .NET 6.0 is (comparing its performance to .NET Framework 4.8): • 3x faster on String.Replace operations • 16x slower on String.IndexOf operations, • 1.4x faster on String.Substring operations • The same on String.Remove operations Copy paste the code below and compile with .NET 6.0 and .NET Framework 4.8 and see by yourself.
//Simple Benchmark test for working with Strings in different versions of .NET Framework
string test = "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. " +
"It has survived not only " +
"five centuries, but also the leap into electronic typesetting," +
" remaining essentially unchanged." +
" It was popularised in the 1960s with the release" +
" of Letraset sheets containing Lorem Ipsum passages," +
" and more recently with desktop publishing software like " +
"Aldus PageMaker including versions of Lorem Ipsum.";System.Diagnostics.Stopwatch K = new System.Diagnostics.Stopwatch();
K.Reset(); K.Start();
for (var v = 1; v <= 10000000; v++)
{
test = test.Replace("a", "bla bla bla bla");
test = test.Replace("bla bla bla bla", "a");
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.Replace]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 1000000; v++)
{
int i = test.IndexOf("including versions of Lorem Ipsum");
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.IndexOf]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 600000000; v++)
{
var s = test.Substring(25, 50);
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.SubString]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 90000000; v++)
{
var s = test.Remove(45, 60);
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.Remove]: {K.Elapsed.TotalSeconds} sec");System.Console.WriteLine("Press a key to exit...");
System.Console.ReadKey();I don't have .net 6 available. Might be better not to use a StopWatch for those tests. Is it a debug build? How long are those tests taking? Do they take "long enough" for a proper test?
-
I don't have .net 6 available. Might be better not to use a StopWatch for those tests. Is it a debug build? How long are those tests taking? Do they take "long enough" for a proper test?
-
Yes, so how long does it take?
-
Some tests to check some functions not tested in link below: Performance Improvements in .NET 6 - .NET Blog (arrays-strings-spans) .NET 6.0 is (comparing its performance to .NET Framework 4.8): • 3x faster on String.Replace operations • 16x slower on String.IndexOf operations, • 1.4x faster on String.Substring operations • The same on String.Remove operations Copy paste the code below and compile with .NET 6.0 and .NET Framework 4.8 and see by yourself.
//Simple Benchmark test for working with Strings in different versions of .NET Framework
string test = "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. " +
"It has survived not only " +
"five centuries, but also the leap into electronic typesetting," +
" remaining essentially unchanged." +
" It was popularised in the 1960s with the release" +
" of Letraset sheets containing Lorem Ipsum passages," +
" and more recently with desktop publishing software like " +
"Aldus PageMaker including versions of Lorem Ipsum.";System.Diagnostics.Stopwatch K = new System.Diagnostics.Stopwatch();
K.Reset(); K.Start();
for (var v = 1; v <= 10000000; v++)
{
test = test.Replace("a", "bla bla bla bla");
test = test.Replace("bla bla bla bla", "a");
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.Replace]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 1000000; v++)
{
int i = test.IndexOf("including versions of Lorem Ipsum");
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.IndexOf]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 600000000; v++)
{
var s = test.Substring(25, 50);
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.SubString]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 90000000; v++)
{
var s = test.Remove(45, 60);
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.Remove]: {K.Elapsed.TotalSeconds} sec");System.Console.WriteLine("Press a key to exit...");
System.Console.ReadKey();It's advisable to use a real benchmarking framework when you want to benchmark operations.
-
Some tests to check some functions not tested in link below: Performance Improvements in .NET 6 - .NET Blog (arrays-strings-spans) .NET 6.0 is (comparing its performance to .NET Framework 4.8): • 3x faster on String.Replace operations • 16x slower on String.IndexOf operations, • 1.4x faster on String.Substring operations • The same on String.Remove operations Copy paste the code below and compile with .NET 6.0 and .NET Framework 4.8 and see by yourself.
//Simple Benchmark test for working with Strings in different versions of .NET Framework
string test = "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. " +
"It has survived not only " +
"five centuries, but also the leap into electronic typesetting," +
" remaining essentially unchanged." +
" It was popularised in the 1960s with the release" +
" of Letraset sheets containing Lorem Ipsum passages," +
" and more recently with desktop publishing software like " +
"Aldus PageMaker including versions of Lorem Ipsum.";System.Diagnostics.Stopwatch K = new System.Diagnostics.Stopwatch();
K.Reset(); K.Start();
for (var v = 1; v <= 10000000; v++)
{
test = test.Replace("a", "bla bla bla bla");
test = test.Replace("bla bla bla bla", "a");
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.Replace]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 1000000; v++)
{
int i = test.IndexOf("including versions of Lorem Ipsum");
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.IndexOf]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 600000000; v++)
{
var s = test.Substring(25, 50);
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.SubString]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 90000000; v++)
{
var s = test.Remove(45, 60);
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.Remove]: {K.Elapsed.TotalSeconds} sec");System.Console.WriteLine("Press a key to exit...");
System.Console.ReadKey();Does it really matter in real world applications ? it never has in one's I've been involved in
Life should not be a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming “Wow! What a Ride!" - Hunter S Thompson - RIP
-
Does it really matter in real world applications ? it never has in one's I've been involved in
Life should not be a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming “Wow! What a Ride!" - Hunter S Thompson - RIP
-
It's advisable to use a real benchmarking framework when you want to benchmark operations.
-
Yes, so how long does it take?
PIEBALDconsult wrote:
Yes, so how long does it take?
Computer: Intel Core I5 .NET 6.0:
Elapsed Time for [String.Replace]: 17.1540338 sec
Elapsed Time for [String.IndexOf]: 32.473905 sec
Elapsed Time for [String.SubString]: 11.9497695 sec
Elapsed Time for [String.Remove]: 9.2207969 sec.NET 4.8
Elapsed Time for [String.Replace]: 47.532167 sec
Elapsed Time for [String.IndexOf]: 2.3447133 sec
Elapsed Time for [String.SubString]: 12.1748157 sec
Elapsed Time for [String.Remove]: 11.4768551 sec -
PIEBALDconsult wrote:
Yes, so how long does it take?
Computer: Intel Core I5 .NET 6.0:
Elapsed Time for [String.Replace]: 17.1540338 sec
Elapsed Time for [String.IndexOf]: 32.473905 sec
Elapsed Time for [String.SubString]: 11.9497695 sec
Elapsed Time for [String.Remove]: 9.2207969 sec.NET 4.8
Elapsed Time for [String.Replace]: 47.532167 sec
Elapsed Time for [String.IndexOf]: 2.3447133 sec
Elapsed Time for [String.SubString]: 12.1748157 sec
Elapsed Time for [String.Remove]: 11.4768551 sec -
pkfox wrote:
Does it really matter in real world applications ?
Yes, my application become unusable after porting it to .NET 6, it uses a lot of string.IndexOf functions.
Then fix your app.
-
Some tests to check some functions not tested in link below: Performance Improvements in .NET 6 - .NET Blog (arrays-strings-spans) .NET 6.0 is (comparing its performance to .NET Framework 4.8): • 3x faster on String.Replace operations • 16x slower on String.IndexOf operations, • 1.4x faster on String.Substring operations • The same on String.Remove operations Copy paste the code below and compile with .NET 6.0 and .NET Framework 4.8 and see by yourself.
//Simple Benchmark test for working with Strings in different versions of .NET Framework
string test = "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. " +
"It has survived not only " +
"five centuries, but also the leap into electronic typesetting," +
" remaining essentially unchanged." +
" It was popularised in the 1960s with the release" +
" of Letraset sheets containing Lorem Ipsum passages," +
" and more recently with desktop publishing software like " +
"Aldus PageMaker including versions of Lorem Ipsum.";System.Diagnostics.Stopwatch K = new System.Diagnostics.Stopwatch();
K.Reset(); K.Start();
for (var v = 1; v <= 10000000; v++)
{
test = test.Replace("a", "bla bla bla bla");
test = test.Replace("bla bla bla bla", "a");
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.Replace]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 1000000; v++)
{
int i = test.IndexOf("including versions of Lorem Ipsum");
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.IndexOf]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 600000000; v++)
{
var s = test.Substring(25, 50);
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.SubString]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 90000000; v++)
{
var s = test.Remove(45, 60);
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.Remove]: {K.Elapsed.TotalSeconds} sec");System.Console.WriteLine("Press a key to exit...");
System.Console.ReadKey();They probably fixed it make it theoretically faster to find a random string inside another random string which makes it worse for most common usage. Is it possible that they are creating more objects with the new library to enable simpler garbage collection across multiple/asynchronous threads? There might be different design goals at play. There was an interesting article I read years ago about some of the algorithms used for string functions in the early BASIC releases.
-
Some tests to check some functions not tested in link below: Performance Improvements in .NET 6 - .NET Blog (arrays-strings-spans) .NET 6.0 is (comparing its performance to .NET Framework 4.8): • 3x faster on String.Replace operations • 16x slower on String.IndexOf operations, • 1.4x faster on String.Substring operations • The same on String.Remove operations Copy paste the code below and compile with .NET 6.0 and .NET Framework 4.8 and see by yourself.
//Simple Benchmark test for working with Strings in different versions of .NET Framework
string test = "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. " +
"It has survived not only " +
"five centuries, but also the leap into electronic typesetting," +
" remaining essentially unchanged." +
" It was popularised in the 1960s with the release" +
" of Letraset sheets containing Lorem Ipsum passages," +
" and more recently with desktop publishing software like " +
"Aldus PageMaker including versions of Lorem Ipsum.";System.Diagnostics.Stopwatch K = new System.Diagnostics.Stopwatch();
K.Reset(); K.Start();
for (var v = 1; v <= 10000000; v++)
{
test = test.Replace("a", "bla bla bla bla");
test = test.Replace("bla bla bla bla", "a");
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.Replace]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 1000000; v++)
{
int i = test.IndexOf("including versions of Lorem Ipsum");
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.IndexOf]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 600000000; v++)
{
var s = test.Substring(25, 50);
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.SubString]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 90000000; v++)
{
var s = test.Remove(45, 60);
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.Remove]: {K.Elapsed.TotalSeconds} sec");System.Console.WriteLine("Press a key to exit...");
System.Console.ReadKey();You didn't / should be using / comparing StringBuilder.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
-
You didn't / should be using / comparing StringBuilder.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
-
Some tests to check some functions not tested in link below: Performance Improvements in .NET 6 - .NET Blog (arrays-strings-spans) .NET 6.0 is (comparing its performance to .NET Framework 4.8): • 3x faster on String.Replace operations • 16x slower on String.IndexOf operations, • 1.4x faster on String.Substring operations • The same on String.Remove operations Copy paste the code below and compile with .NET 6.0 and .NET Framework 4.8 and see by yourself.
//Simple Benchmark test for working with Strings in different versions of .NET Framework
string test = "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. " +
"It has survived not only " +
"five centuries, but also the leap into electronic typesetting," +
" remaining essentially unchanged." +
" It was popularised in the 1960s with the release" +
" of Letraset sheets containing Lorem Ipsum passages," +
" and more recently with desktop publishing software like " +
"Aldus PageMaker including versions of Lorem Ipsum.";System.Diagnostics.Stopwatch K = new System.Diagnostics.Stopwatch();
K.Reset(); K.Start();
for (var v = 1; v <= 10000000; v++)
{
test = test.Replace("a", "bla bla bla bla");
test = test.Replace("bla bla bla bla", "a");
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.Replace]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 1000000; v++)
{
int i = test.IndexOf("including versions of Lorem Ipsum");
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.IndexOf]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 600000000; v++)
{
var s = test.Substring(25, 50);
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.SubString]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 90000000; v++)
{
var s = test.Remove(45, 60);
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.Remove]: {K.Elapsed.TotalSeconds} sec");System.Console.WriteLine("Press a key to exit...");
System.Console.ReadKey();If you need it that much, hire someone to write an optimized C version that doesn't update? Some optimizations in C# exist, if you'd care to search; Note the #1 in all tests. C# .Net: Fastest Way to check if a string occurs within a string - The Curious Consultant[^] Now, get off my lawn.
Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
-
If you need it that much, hire someone to write an optimized C version that doesn't update? Some optimizations in C# exist, if you'd care to search; Note the #1 in all tests. C# .Net: Fastest Way to check if a string occurs within a string - The Curious Consultant[^] Now, get off my lawn.
Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
-
Gerry Schmitz wrote:
comparing StringBuilder
There is no IndexOf function in StringBuilder. See: .net - Why doesn't StringBuilder have IndexOf method? - Stack Overflow[^]
That doesn't mean you exclude it. For that matter, if the string is already loaded in a StringBuilder, then use (SB instance).ToString().IndexOf()
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
-
Eddy Vluggen wrote:
Note the #1 in all tests.
Thank you. I will test it it. And C Language seems to be a great choice. Or Golang. Or Rust.
C because it close to assembly. Golang or rust aren't competing there. If it is a core function of what you do, then it'd make sense; eliminate the dependency.
Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
-
Some tests to check some functions not tested in link below: Performance Improvements in .NET 6 - .NET Blog (arrays-strings-spans) .NET 6.0 is (comparing its performance to .NET Framework 4.8): • 3x faster on String.Replace operations • 16x slower on String.IndexOf operations, • 1.4x faster on String.Substring operations • The same on String.Remove operations Copy paste the code below and compile with .NET 6.0 and .NET Framework 4.8 and see by yourself.
//Simple Benchmark test for working with Strings in different versions of .NET Framework
string test = "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. " +
"It has survived not only " +
"five centuries, but also the leap into electronic typesetting," +
" remaining essentially unchanged." +
" It was popularised in the 1960s with the release" +
" of Letraset sheets containing Lorem Ipsum passages," +
" and more recently with desktop publishing software like " +
"Aldus PageMaker including versions of Lorem Ipsum.";System.Diagnostics.Stopwatch K = new System.Diagnostics.Stopwatch();
K.Reset(); K.Start();
for (var v = 1; v <= 10000000; v++)
{
test = test.Replace("a", "bla bla bla bla");
test = test.Replace("bla bla bla bla", "a");
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.Replace]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 1000000; v++)
{
int i = test.IndexOf("including versions of Lorem Ipsum");
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.IndexOf]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 600000000; v++)
{
var s = test.Substring(25, 50);
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.SubString]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 90000000; v++)
{
var s = test.Remove(45, 60);
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.Remove]: {K.Elapsed.TotalSeconds} sec");System.Console.WriteLine("Press a key to exit...");
System.Console.ReadKey();You got a really weird number at String.IndexOf. Here are my results:
Elapsed Time for [String.Replace]: 16,8695848 sec
Elapsed Time for [String.IndexOf]: 2,0153508 sec
Elapsed Time for [String.SubString]: 11,7445885 sec
Elapsed Time for [String.Remove]: 10,1579051 sec.NET 6 is 1.7x up to 2.9x faster. I think these numbers are pretty consistent.
-
Some tests to check some functions not tested in link below: Performance Improvements in .NET 6 - .NET Blog (arrays-strings-spans) .NET 6.0 is (comparing its performance to .NET Framework 4.8): • 3x faster on String.Replace operations • 16x slower on String.IndexOf operations, • 1.4x faster on String.Substring operations • The same on String.Remove operations Copy paste the code below and compile with .NET 6.0 and .NET Framework 4.8 and see by yourself.
//Simple Benchmark test for working with Strings in different versions of .NET Framework
string test = "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. " +
"It has survived not only " +
"five centuries, but also the leap into electronic typesetting," +
" remaining essentially unchanged." +
" It was popularised in the 1960s with the release" +
" of Letraset sheets containing Lorem Ipsum passages," +
" and more recently with desktop publishing software like " +
"Aldus PageMaker including versions of Lorem Ipsum.";System.Diagnostics.Stopwatch K = new System.Diagnostics.Stopwatch();
K.Reset(); K.Start();
for (var v = 1; v <= 10000000; v++)
{
test = test.Replace("a", "bla bla bla bla");
test = test.Replace("bla bla bla bla", "a");
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.Replace]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 1000000; v++)
{
int i = test.IndexOf("including versions of Lorem Ipsum");
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.IndexOf]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 600000000; v++)
{
var s = test.Substring(25, 50);
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.SubString]: {K.Elapsed.TotalSeconds} sec");K.Reset(); K.Start();
for (var v = 1; v <= 90000000; v++)
{
var s = test.Remove(45, 60);
}
K.Stop();
System.Console.WriteLine($"Elapsed Time for [String.Remove]: {K.Elapsed.TotalSeconds} sec");System.Console.WriteLine("Press a key to exit...");
System.Console.ReadKey();Prior to .NET 5, culture-specific comparisons used NLS[^] on Windows. Since .NET 5, they switched to using ICU[^] instead. Globalization and ICU | Microsoft Docs[^] There is a config switch to force .NET to use NLS instead, but it's not recommended: Breaking change with string.IndexOf(string) from .NET Core 3.0 -> .NET 5.0[^] If you change your
IndexOf
call to specifyStringComparison.Ordinal
orStringComparison.OrdinalIgnoreCase
, the .NET 6 code is roughly 1.5x faster than the .NET Framework 4.8 equivalent.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer