PC Review


Reply
Thread Tools Rate Thread

Code efficiency tool

 
 
Alpha83
Guest
Posts: n/a
 
      30th Jun 2008
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.
 
Reply With Quote
 
 
 
 
Jeroen Mostert
Guest
Posts: n/a
 
      30th Jun 2008
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.
 
Reply With Quote
 
qglyirnyfgfo@mailinator.com
Guest
Posts: n/a
 
      30th Jun 2008
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.com> wrote:
> 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.


 
Reply With Quote
 
Jianwei Sun
Guest
Posts: n/a
 
      1st Jul 2008
Hello (E-Mail Removed),

> 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.com> wrote:
>
>> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
High-efficiency report design with Excel-like reporting tool akar Microsoft Access Reports 2 19th May 2009 07:01 AM
High-efficiency report design with Excel-like reporting tool akar Microsoft Access Reports 0 19th May 2009 03:53 AM
High-efficiency report design with Excel-like reporting tool akar Microsoft Dot NET 0 15th May 2009 12:23 PM
High-efficiency report design with Excel-like reporting tool akar Microsoft Dot NET 0 15th May 2009 12:23 PM
Free Tool (Add-In) for Improving Your Data Processing Efficiency =?Utf-8?B?RGFubmllbCBDaGVu?= Microsoft Excel Programming 0 23rd May 2005 08:48 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:59 PM.