473,395 Members | 2,192 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,395 software developers and data experts.

String comparison efficiency

I'm giving maintenance to someone elses sloppy code, on a servlet´s doPost it compares an input to determine the action that must be done, like


if (action.equals("delete")) {
...
}

so was wondering if doing

if (action.hashCode == new String("delete").hashCode())

was faster, or which method is better for comparing strings.
Aug 9 '06 #1
1 7815
Banfa
9,065 Expert Mod 8TB
I would be very surpised if using hashcodes was faster, you have so many more operations to perform

if (action.equals("delete")) {
...
}

Access each character for a comparison


if (action.hashCode == new String("delete").hashCode())

access each character to create a hashcode
allocate the memory for a new string
compare hashcodes

Ultimately it depends onb how fast your platform is at integer maths compared with how fast it is at integer comparison (leaving theString allocation out of the equation). I would thing the total operations to create and compare the hashcodes is greater than the total operations just to compare the string in the first place.

On the other hand if you are going to be comparing the string to "delete" several times it may be faster to use the has code. Also (of course) you can speed up the hashcode version by creating hashcodes for the strings when the program starts and storing it. Do this and the number of operations between the hashcode version and the straight comparison would be similar and they will go at about the same speed.

Of course you will run into trouble if a string ever occurs that is not "delete" but creates the same hash code.
Aug 9 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Wolfgang Keller | last post by:
Hello, has anyone done a comparison of the different RPC protocols & implementations available for Python that is more extensive and exhaustive than the one on...
6
by: rakesh | last post by:
Hi, I have a requirement in which I need to search a string in a file stored on a harddisk. String which needs to be searched would be stored in a file with delimiter as new line character. some...
9
by: David | last post by:
Hi all, I have an array of strings. For example... = "entry1 first entry"; = "entry2 second entry"; = "entry3 third entry"; etc... How can I sort this array by the number after "entry"?
9
by: C.K. | last post by:
Given an array of strings (i.e char *strings), write C code to sort the list in increasing order of string length. Run time efficiency is a primary concern. Thanks a lot!
46
by: yadurajj | last post by:
Hello i am newbie trying to learn C..I need to know about string comparisons in C, without using a library function,...recently I was asked this in an interview..I can write a small program but I...
19
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not...
15
by: Kapil Jain | last post by:
Dear All, What i need to achieve is : I am generating dynamic text boxes thru dhtml coding, i need onChange event of oragnistation text box i.e dynamically generated on click of "More" button in...
47
by: sudharsan | last post by:
could any one please give me a code to reverse a string of more than 1MB .??? Thanks in advance
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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...
0
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...

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.