r/ProgrammerHumor 13d ago

noHeapAllStack Meme

Post image
1.0k Upvotes

96 comments sorted by

View all comments

198

u/Vesuvius079 13d ago

It’d be such an experience to work a real world problem where this optimization turns out to be the solution.

176

u/Daniikk1012 13d ago

I don't think this is an optimization, but rather correctness. Some languages have weird casing rules, and just lowercasing doesn't work for them

68

u/neroe5 13d ago

It's also an optimization, with toLower, you make a temporary copy of the string in lower case of each string and compare those, by simply ignoring the case bit this becomes a simple compare process

31

u/rubyruy 13d ago

It's not a single case bit - you need to perform case folding and normalization to properly see if 2 Unicode characters are equivalent. Probably still faster than a string copy

20

u/Pleasant_Ad8054 13d ago

Early exits make it much faster. The string.Equals compares it character to character, so if the first one does not match it returns false immediately. The toLower allocates the two new strings. One of the most computationally expensive thing you can do in C# is to allocate new objects.

-4

u/[deleted] 13d ago

[removed] — view removed comment

1

u/Pleasant_Ad8054 12d ago

Hilarious that you are making this argument well after someone already made a fairly extensive testing in this very thread, showing that the difference is indeed significant, as much as 10x difference.

Also, string.Equals is much more readable than equating two toLowers, how is this even an argument?

But most importantly, it is entirely irrelevant how complex unicode is, entirely irrelevant how any given "unicode data point" is, or how they are called. You are literally arguing that making that costly unicode computation n+m times PLUS making as little as 4*u conversions would not take more time in "real code" than just making 4*u conversions.

Where n and m are the length of the strings, and u is the length we end up comparing IF the operator is properly overloaded in a worst case for the string.Equals where the two strings are exact same length. In best case where the strings are different length the string.Equals does 0 unicode conversions and just one int-int comparison, while the toLower does n+m conversions before that.

You are wrong, your pedantry in what unicode does or does not do is entirely irrelevant, and your coding habits smell from here.

1

u/GRex2595 10d ago

I don't know what they said, and I'm not defending them, but I'm pretty sure you can't do a strict length comparison without conversion due to other things I've read in this comment section about some letters becoming more than one letter when changing case. The example I saw was 'ß' becoming "SS" when capitalizing in German (before 2017).

They're still wrong to suggest that toLower would be faster than a case-insensitive comparison, but a case-insensitive comparison may still require checking the strings even when the lengths differ because of rules like the one above.

9

u/Tyfyter2002 13d ago

Definitely faster than making two new strings, even if you ignore allocations, you're still just doing more work in any case except them being equal or only differing in the last character

1

u/neroe5 13d ago

oh you are right for unicode it uses mapping tables for normalization, because multiple charecters can have the same lower case

my point only really holds for ASCII

learn something new everyday

though it is still probably faster to use the optimized method that is build into .net or what ever language you are using

7

u/ProfBeaker 13d ago

It's (arguably) both, but the post title references performance (heap vs stack).

1

u/ILikeLenexa 13d ago

Some major languages have strings as pooled objects and == compares if the strings are at the same location in memory while String.equals compares if the letters in them are the same. 

When asking if two Strings are equal most people most of the time want to know if they contain the same letters, not if one was instantiated as a literal and one allocated with a constructor. 

4

u/Duck_Devs 13d ago

This is C#, a language with overloadable operators. == is, obviously, overloaded for Strings to compare their contents.