By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,892 Members | 1,088 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,892 IT Pros & Developers. It's quick & easy.

Code efficiency tool

P: n/a
Hi,
Is there a code measuring tool that tells you which is more efficient
cost-wise. For example, if I were to compare the following two
identical code blocks, how do I know, which is more expensive CPU
wise :

//compare 2 strings
1. if (str1 == str2) ....

2. if (String.Compare(str1, str2, true) == 0) ....

Thanks.
Jun 30 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Alpha83 wrote:
Is there a code measuring tool that tells you which is more efficient
cost-wise. For example, if I were to compare the following two
identical code blocks, how do I know, which is more expensive CPU
wise :

//compare 2 strings
1. if (str1 == str2) ....

2. if (String.Compare(str1, str2, true) == 0) ....
Forget the efficiency, worry about correctness first. The code snippets
you've given are not equivalent, much less identical. The first does a
culture-insensitive ordinal comparison of the two strings (in other words,
they have to be byte-for-byte equal) while the second does a
culture-sensitive, case-insensitive comparison (in other words, they could
literally have no byte in common but still contain the same characters as
far as the comparison is concerned).

Even without a profiler (that's the kind of tool you're looking for) I can
tell you that the first snippet is faster (because, barring a very poor
implementation, a byte-for-byte comparison will always be faster than one
that takes culture and case into account), but that's pretty much
meaningless because the statements aren't comparable.

What you want to do is get the program correct first, and efficiency should
only come in as a broad design criterion here in the sense of not doing
anything obviously silly (like searching many times in a big unsorted array
as opposed to sorting it first) but not in worrying about what exact lines
of code are most efficient.

If after that you find that your program is doing its job fast enough, leave
it alone. Go do something else. Only if your program is not doing its job
fast enough should you get out a profiler (there are many such tools for
..NET available, some free) and find out where its spending the most of its
time. Focus on optimizing that part to the point where your program is
either fast enough (in which case you're done) or until the part you're
optimizing no longer dominates the runtime of your application (in which
case, profile again and repeat until done).

Micro-benchmarks (where you do some simple timing of a piece of code
executed many times, for example with the help of the Stopwatch class)
usually harm more than they help. They are somewhat useful for quickly
determining whether a particular piece of code is obviously faster or slower
than its alternative, but even then they're misleading, since they can't
take into account the actual workload.

In any case, you'll want to make sure that whatever you're optimizing still
does the same thing when you're done with it. Unit testing is indispensable
here, as is understanding the methods you're calling. If you actually
"optimized" string comparison in the manner above, you might be in for a
rude surprise.

--
J.
Jun 30 '08 #2

P: n/a
Below is the internal implementation of ==:

------
public static bool operator ==(string a, string b)
{
return Equals(a, b);
}
------

So you can see that you are really using string.Equals() when you do
==.

String.Compare() performs a culture-sensitive comparison while
String.Equals() performs an ordinal comparison.

So, since String.Compare() uses culture, I would guess that
String.Compare would be much slower than ==.

Nevertheless, both operations have different meaning so its no longer
a matter of what faster or slower, its a matter of what you need to
have done.

Rene

On Jun 30, 2:07*pm, Alpha83 <taps...@gmail.comwrote:
Hi,
Is there a code measuring tool that tells you which is more efficient
cost-wise. *For example, if I were to compare the following two
identical code blocks, how do I know, which is more expensive CPU
wise :

//compare 2 strings
1. *if (str1 == str2) ....

2. *if (String.Compare(str1, str2, true) == 0) ....

Thanks.
Jun 30 '08 #3

P: n/a
Hello qg**********@mailinator.com,
Below is the internal implementation of “==”:

------
public static bool operator ==(string a, string b)
{
return Equals(a, b);
}
------

So you can see that you are really using string.Equals() when you do
“==”.

String.Compare() performs a culture-sensitive comparison while
String.Equals() performs an ordinal comparison.

So, since String.Compare() uses culture, I would guess that
“String.Compare” would be much slower than “==”.

Nevertheless, both operations have different meaning so its no longer
a matter of what faster or slower, its a matter of what you need to
have done.

Rene

On Jun 30, 2:07 pm, Alpha83 <taps...@gmail.comwrote:
>Hi,
Is there a code measuring tool that tells you which is more efficient
cost-wise. For example, if I were to compare the following two
identical code blocks, how do I know, which is more expensive CPU
wise :
//compare 2 strings
1. if (str1 == str2) ....
2. if (String.Compare(str1, str2, true) == 0) ....

Thanks.
String.Compare also allows you to pass an option to do a culture insenstive
comparison.
Jul 1 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.