C# puzzle....
-
Wrong Forum! Here are few right places to ask the QN or to discuss it. 1.C# Discussion Boards[^] 2.The Weird and The Wonderful[^]
No, it's ok. Read the sticky on top: The Lounge[^] Point2: Technical discussions are welcome...
Wrong is evil and must be defeated. - Jeff Ello
-
I'm just an old C++ dinosaur, so my guess is that B and D are the fastest, followed by A and C, and finally the indecipherable twaddle of E--unless it's some kind of secret code for "Hint: you're looking for a single character and therefore don't have to construct
"~"
, in which case it's probably faster than A and C but slower than B and D.Robust Services Core | Software Techniques for Lemmings | Articles
Greg Utas wrote:
finally the indecipherable twaddle of E--unless it's some kind of secret code for "Hint: you're looking for a single character and therefore don't have to construct
"~"
Nope: it's saying "Look for this using a binary comparison, rather than using a culture or shift specific sort order" - basically "compare strings as byte arrays" but using 16 bit values. In theory, this should be much quicker than a non-ordinal comparison as you don't have to deal with "special cases" or "'t' == 'T'", and most processors have machine code instructions for byte based searching and comparing.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
-
Wrong Forum! Here are few right places to ask the QN or to discuss it. 1.C# Discussion Boards[^] 2.The Weird and The Wonderful[^]
Jorgen is right - this isn't a question seeking help, it's a "Hey! look at this!" discussion.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
-
(I was torn between putting these here or in the C# forum, but it not really a serious question...) Given a simple string
const string \_str = "abcdefghijklmnopqrstuvwxyz";
put the following equivalent code blocks in order of their speed:
if ( \_str.Contains("~")) {....} // A if ( \_str.Contains('~')) {....} // B if ( \_str.IndexOf("~") >= 0) {....} // C if ( \_str.IndexOf('~') >= 0) {....} // D if ( \_str.IndexOf("~", StringComparison.Ordinal) >= 0) {....} //E
Hint: there are three tiers --- One is the clear winning. Two others are about the same, significantly behind, and the last two are about the same, *way* behind the others.
Truth, James
-
In my experience, the speed measurements at the start of an app are always polluted by the runtime initialization. That is, it might look that "A" takes longer than "B", where actually does not. Typically, I copy the same piece of code two or three times, then I take the last results. Have a try, maybe the results will change, maybe not.
Mario Vernari wrote:
Typically, I copy the same piece of code two or three times, then I take the last results.
I use sleep 5 seconds, then go for it.
M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.
-
Greg Utas wrote:
finally the indecipherable twaddle of E--unless it's some kind of secret code for "Hint: you're looking for a single character and therefore don't have to construct
"~"
Nope: it's saying "Look for this using a binary comparison, rather than using a culture or shift specific sort order" - basically "compare strings as byte arrays" but using 16 bit values. In theory, this should be much quicker than a non-ordinal comparison as you don't have to deal with "special cases" or "'t' == 'T'", and most processors have machine code instructions for byte based searching and comparing.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
Next time I can't decipher some twaddle, I'll know who to turn to. :laugh:
Robust Services Core | Software Techniques for Lemmings | Articles
-
(I was torn between putting these here or in the C# forum, but it not really a serious question...) Given a simple string
const string \_str = "abcdefghijklmnopqrstuvwxyz";
put the following equivalent code blocks in order of their speed:
if ( \_str.Contains("~")) {....} // A if ( \_str.Contains('~')) {....} // B if ( \_str.IndexOf("~") >= 0) {....} // C if ( \_str.IndexOf('~') >= 0) {....} // D if ( \_str.IndexOf("~", StringComparison.Ordinal) >= 0) {....} //E
Hint: there are three tiers --- One is the clear winning. Two others are about the same, significantly behind, and the last two are about the same, *way* behind the others.
Truth, James
It might well depend on the string being searched and what is being searched.
-
In my experience, the speed measurements at the start of an app are always polluted by the runtime initialization. That is, it might look that "A" takes longer than "B", where actually does not. Typically, I copy the same piece of code two or three times, then I take the last results. Have a try, maybe the results will change, maybe not.
Mario Vernari wrote:
In my experience, the speed measurements at the start of an app are always polluted by the runtime initialization.
Not just that, but also caching, task switching etc. There is a really good article here on how to get consistent test results: Performance Tests: Precise Run Time Measurements with System.Diagnostics.Stopwatch[^]
Wrong is evil and must be defeated. - Jeff Ello
-
Upvoted for 'cheating' being scientific - there's nothing wrong with empirical proof, its exactely what I thought when I saw the question, ie, 'why guess' ..
Thanks, good way of looking at it I suppose.
Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com
-
Jorgen is right - this isn't a question seeking help, it's a "Hey! look at this!" discussion.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
Also one of the more interesting posts here in recent times.
Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com