Cutesy string code I wrote today
-
peterchen wrote:
m_string = s.ToLowerInvariant(); if (m_string == s) m_string = s;
Other way is
m_string = string.Intern(s.ToLowerInvariant());
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
That at least looks better than what I wrote! You are right, there are two reasons, both performance related: First, I expect string.operator== to do a "reference equals" comparison first. Second, the "outer" string might easily outlive the local one (belong to an older generation, referenced by multiple string instances) etc. But I wouldn't necessary want to add the string ot the intern pool. But both ways optimize for a specific case, so our milage may differ.
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
My first real C# project | Linkify!|FoldWithUs! | sighist -
If you can find a use for that, I'd be worried. Unless you're trying to reprogram Vista...:laugh: I see what you mean about the horror. Talk about pointless. If x=y, then set x=y. Why not set y=x at the same time? Just to make sure...:-P
Yet another spam post on yet another forum! I am the lazy one, who sleeps as it suits him, codes what he wishes, and has many years to look forward to. I love being a student.
It's faster ;) (at least, it may be, see dnh and my reply below)
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
My first real C# project | Linkify!|FoldWithUs! | sighist -
That at least looks better than what I wrote! You are right, there are two reasons, both performance related: First, I expect string.operator== to do a "reference equals" comparison first. Second, the "outer" string might easily outlive the local one (belong to an older generation, referenced by multiple string instances) etc. But I wouldn't necessary want to add the string ot the intern pool. But both ways optimize for a specific case, so our milage may differ.
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
My first real C# project | Linkify!|FoldWithUs! | sighiststring.operator== do a content comparision but I am not sure about object o1 = "unun"; object o2 = "un" + "un"; o1 == o2 .. result?
-
string.operator== do a content comparision but I am not sure about object o1 = "unun"; object o2 = "un" + "un"; o1 == o2 .. result?
I just meant that it would make sense to make a reference equality comparison first, before doing the contents comparison. (I'd actually be surprised if it wouldn't. since the reference comparison is so much cheaper). For your example: both objects are identical, since "un" + "un" gets evaluated at compile time, and so bot reference the interned instance of "unun". if you change that slightly, however: object o1 = "unun"; object o2 = "un"; o2 = (string)o2 + "un"; which forces the concatenation to be one at runtime. Since operator== isn't virtual, all it does here is a reference comparison - in which the objects are not equal.
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
My first real C# project | Linkify!|FoldWithUs! | sighist -
Strings are immutable.
String.ToLowerInvariant()
, just like everything else, creates new instance of string. You can try this: string s = "testme"; string s2 = s.ToLowerInvariant(); Console.Write(s==s2); Console.Write(string.ReferenceEquals(s,s2)); First is true, second is false. If you do what peterchen did, you will get true in both cases. By assigning original string to m_string, you'll get same references. In same cases this could be usefull, I think string.Equals(string) compares strings byte by byte, comparing references is much faster.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
-
If you can find a use for that, I'd be worried. Unless you're trying to reprogram Vista...:laugh: I see what you mean about the horror. Talk about pointless. If x=y, then set x=y. Why not set y=x at the same time? Just to make sure...:-P
Yet another spam post on yet another forum! I am the lazy one, who sleeps as it suits him, codes what he wishes, and has many years to look forward to. I love being a student.
thou shalt think about thy response before thou flameth. reference comparison is MUCH faster, as in O(1) vs O(n)...
-
That at least looks better than what I wrote! You are right, there are two reasons, both performance related: First, I expect string.operator== to do a "reference equals" comparison first. Second, the "outer" string might easily outlive the local one (belong to an older generation, referenced by multiple string instances) etc. But I wouldn't necessary want to add the string ot the intern pool. But both ways optimize for a specific case, so our milage may differ.
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
My first real C# project | Linkify!|FoldWithUs! | sighistNot to jump in with the usual anal programmer response or anything, but since everyone else seems to...why is this a string in the first place? It looks in context like the string is being used as a token of some kind which could be more efficiently done with an enum or int.
"110%" - it's the new 70%
-
Not to jump in with the usual anal programmer response or anything, but since everyone else seems to...why is this a string in the first place? It looks in context like the string is being used as a token of some kind which could be more efficiently done with an enum or int.
"110%" - it's the new 70%
It's a token to be replaced in a string, but tokens should be ignore case. (i.e. I have a map token-replacement, and a very suboptimal routine doing the replacement). So yes, even though I might end up with lots of tokens, the optimization is irrelevant for this use case, it probably was some mental acrobatics only :)
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
My first real C# project | Linkify!|FoldWithUs! | sighist -
It's not really a bug, just something weird that did flow out my fingers today. It looks so bad I want to declare it as a "coding horror", but it may arguably be useful.
m_string = s.ToLowerInvariant();
if (m_string == s)
m_string = s;The language is C#.
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
My first real C# project | Linkify!|FoldWithUs! | sighist*** Insufficent Caffeine Error *** ;P Any decent static analyser should spot that one, though! If it were C or C++ you'd see PC-Lint issue 774 ("Boolean within 'expression' always evaluates to [True/False]"); OTTOMH* I couldn't tell you what the corresponding issue in FxCop is though. * Guess
Anna :rose: Linting the day away :cool: Anna's Place | Tears and Laughter "If mushy peas are the food of the devil, the stotty cake is the frisbee of God"
-
*** Insufficent Caffeine Error *** ;P Any decent static analyser should spot that one, though! If it were C or C++ you'd see PC-Lint issue 774 ("Boolean within 'expression' always evaluates to [True/False]"); OTTOMH* I couldn't tell you what the corresponding issue in FxCop is though. * Guess
Anna :rose: Linting the day away :cool: Anna's Place | Tears and Laughter "If mushy peas are the food of the devil, the stotty cake is the frisbee of God"
Anna-Jayne Metcalfe wrote:
*** Insufficent Caffeine Error ***
Not really - it was intentional, it relies on a subtle difference between value equality (tested with ==) and reference equality ('enforced' with =)
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
My first real C# project | Linkify!|FoldWithUs! | sighist