473,549 Members | 2,615 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1981
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**********@ma ilinator.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.Compa re” 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
3713
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 ways to code. The reason I ask is because I was just thinking about the following...which is better and/or why? document.forms.elements.value or...
100
3546
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 starts an unbounded memory scan searching for a zero that ends the string. A more efficient representation is: struct string {
11
3746
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 the assembly level ? for a performance critical application is it better to to use switch case or accomplish the same using fn pointers ?
10
1896
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 per the text book. I am just apprehensive whether this may break or may cause a memmory leak. Any chance for it. #define TOKN_SIZE 256 void...
15
5052
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 that i could retrieve my code from my applications after i compile it so i need to know to prevent this from happening to my applications Thanx in...
171
7625
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) like it because you can split the code from the design. That is logical. But if you are the only one working on the code, it seem a little...
16
2303
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 -------------- I wanted to read that file and put it into dynamic memory, like
239
10097
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 users request are a part of the OpenSource community. Many if not most of those applications strongly require the presence of the GNU compiling...
5
3852
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
7521
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7451
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7959
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7473
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7810
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6044
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1944
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.