473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Code efficiency tool

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
3 1967
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bruce Duncan | last post by:
I have been starting to use Javascript a lot lately and I wanted to check with the "group" to get your thoughts on code efficiency. First, is there a good site/book that talks about good and bad...
100
by: jacob navia | last post by:
As everybody knows, C uses a zero delimited unbounded pointer for its representation of strings. This is extremely inefficient because at each query of the length of the string, the computer...
11
by: hasadh | last post by:
Hi, is the assemly code for if..else and switch statements similar. I would like to know if switch also uses value comparison for each case internally or does it jump to the case directly at...
10
by: saraca means ashoka tree | last post by:
The following code is the heart of a program that I wrote to extract html tags from a webpage. How efficient is my code ?. Is there still possible way to optimize the code. Am I using everything as...
15
by: Fady Anwar | last post by:
Hi while browsing the net i noticed that there is sites publishing some software that claim that it can decompile .net applications i didn't bleave it in fact but after trying it i was surprised...
171
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
16
by: Pedro Graca | last post by:
I have a file with different ways to write numbers ---- 8< (cut) -------- 0: zero, zilch,, nada, ,,,, empty , void, oh 1: one 7: seven 2: two, too ---- >8 -------------- ...
239
by: Eigenvector | last post by:
My question is more generic, but it involves what I consider ANSI standard C and portability. I happen to be a system admin for multiple platforms and as such a lot of the applications that my...
5
by: vamsioracle | last post by:
Hi all, I have a problem with the ult_smtp package. Let me explain how the structure of my code is procedure------------ begin declarations of variables and cursors...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.