In a message dated 11/6/2004 3:48:49 PM Eastern Standard Time, [EMAIL PROTECTED] writes:
In the Java code I am looking at these days (JMX for example) I am
seeing many places where branches are made based upon String equality.
For example:

   void doSomething(String s){
     if (s.equals("some thing"))
       doA();
     else if (s.equals("another thing"))
       doB();
     else
       punt();
   }

Compare that with:

   void doSomething(int i){
     if (i == 0)
       doA();
     else if (i == 1)
       doB();
     else
       punt();
   }

I would guess that the int equality can be calculated much more rapidly
than the String equality.  String equality might be so slow in
comparison that developers should be wary of this practice in code where
speed matters.

I am aware that sometimes there are big advantages to using Strings as
branch-deciding parameters.  I often favor this practice, but mostly in
code that runs infrequently.

But I wonder if anybody knows better.  I'm just guessing.  Has anybody
seen evidence that String equality can become a time hog?

Rich Hammer
one would think, but it might depend on how the underlying code is written. When a string object is created--if I were writing the object--it'd do some kind of check sum with xor or something like that to offer some kind of numeric identity for the string which would make string compare as fast as int compare because it would be an int compare.
 
I do a ton of string compares in my code and they seem to go very quickly. Might be fun to write a benchmark to compare the time it takes to compare a hundred million pregenerated random integers as opposed to a hundred million compares of pregenerated strings of one character, two characters, three characters, and so on. If the java guys did what I would have done, the compares might differ a bit between ints and strings, but shouldn't vary according to the length of the strings. If the compare gets slower according to the length of the string, bad on java guys.
 
is it more fun figuring it out by testing it or by going into the java code and reading how they actually did it? We could do a spinal tap before and after each and measure the relative amounts of serotonin ...
_______________________________________________
Juglist mailing list
[EMAIL PROTECTED]
http://trijug.org/mailman/listinfo/juglist_trijug.org

Reply via email to