C# performance and memory optimization ๐Ÿ’ก
For a very long time, I used to compare strings in my codebase by doing stringA.ToLower() == stringB.ToLower(). I did not know that I was consuming a lot of memory. A thread ๐Ÿงตโฌ‡๏ธ
#csharp #dotnet
Even if computers are very powerful nowadays, it's recommended to build memory-efficient systems because low memory allocation increases performance of the system. So it's always a good idea to use less memory when possible.
Recently, I've tried to measure two ways of comparing strings:
1โƒฃ My old way of doing (see the first tweet of the thread)
2โƒฃ the use of string[.]Compare method.
To do so, I've created quite a huge list of alphabet letters (1024 items of each letter) and two methods that will count how many occurrences of a given letter we have in the list.
As we can see, the first method is comparing the letters by testing their lowercase values equality, and the another by using the string[.]Compare method. Here is the memory allocation results :
The memory allocated by the WithoutCompare method is HUGE ๐Ÿ˜ฏ๐Ÿ˜ฎ๐Ÿ˜ฑ
Also, the Garbage Collector (the "Gen" parameter) has a lot more work to perform with the first method. The difference is very significant. But why such a difference?
It's quite normal though : in C#, strings are immutable. So every time the ToLower method is called on a string, a brand new string is created to hold the lowercase value. As a result, a piece of memory is allocated for it. That is why the first method is so memory-consuming.
On a contrary, the String[.]Compare method does not create a new string. So memory is not wasted here.
In conclusion, when you need to make string comparison on an important amount of data, use the string .Compare method. It will help you avoid some performance issues and memory leaks. You can learn more about the string[.]Compare method here : docs.microsoft.com/en-us/dotnet/aโ€ฆ) ๐Ÿ‘๐Ÿ˜‰
I hope you liked this thread. Fore more tips about C# and .NET, do not hesitate to give a follow! ๐Ÿ˜‰
#dotnetlovesme
๐Ÿ”š
Thread reopened ๐Ÿ˜…
As you guys are asking in the replies, let's add some more information here.
As the string comparison with the ToLower() method is excluded (memory-inefficient), let's focus on the two remaining methods: the string .Compare() and the string.Equals()
I've compared the two methods with the Ordinal IgnoreCase comparison mode this time (read the comments if you want to know why)
And here are the results. The allocated memory is quite the same, but The Equals() method seems to be faster ๐Ÿš€.

โ€ข โ€ข โ€ข

Missing some Tweet in this thread? You can try to force a refresh
ใ€€

Keep Current with Daniel Lawson ๐Ÿ‡น๐Ÿ‡ฌ๐Ÿ‡ง๐Ÿ‡ซ๐Ÿ‡ฒ๐Ÿ‡ซ

Daniel Lawson ๐Ÿ‡น๐Ÿ‡ฌ๐Ÿ‡ง๐Ÿ‡ซ๐Ÿ‡ฒ๐Ÿ‡ซ Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(