commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Henning P. Schmiedehausen" <>
Subject Re: [lang] enhanced version of Class.forName
Date Tue, 06 Sep 2005 16:09:12 GMT
=?iso-8859-1?Q?J=F6rg_Schaible?= <> writes:

>Hi Henning,

Hi Jörg,

>Well, looking into the JDK source I see no real advantage of not using endsWith. x.endsWith("[]")
will exactly do 2 character compares (if the string matches), as well as your code above.
And it only has one bounds check, compared to 2 in your example (3 counting "nameLen>2").
endsWith has one indirect call more though. So why do you think, that endsWidth is not time


>- Jörg
>(who is not willing to accept myths just by word)

I admit that I got fooled by the loop and for constant search string
length, the running time is constant and does not depend on the search
string length.

However, the overall numbers are better for charAt. Care for some hard

JDK 1.5.0_04 on Fedora Core 3, 3 GHz x86:

not matching, endOf(): 19
Matching, endOf(): 44
Not matching, long endOf(): 19
Matching, long endOf(): 44
Short, endOf(): 19
Not matching, charAt(): 8
Matching, charAt(): 28
Not matching, long endOf(): 8
Matching, long charAt(): 28
Short, charAt(): 4

(everything in millis)

JDK 1.4.2_09 on Fedora Core 3, 3 GHz x86:

Not matching, endOf(): 26
Matching, endOf(): 40
Not matching, long endOf(): 26
Matching, long endOf(): 39
Short, endOf(): 24
Not matching, charAt(): 8
Matching, charAt(): 24
Not matching, long endOf(): 7
Matching, long charAt(): 24
Short, charAt(): 4

(everything in millis)

=> charAt is 3-4 times faster when not matching and almost twice as
   fast when matching. For the short case it is even faster.

Get Test Class Source code from

Another interesting point is that a method call seems to impose just a
very small overhead. Even though the charAt test needs three of them
in the worst case (length() and 2x charAt()), it is still faster than
the single endsWith() method call.

	Best regards
		Henning (Micro-Optimizer and general nit-picker. ;-) )

Dipl.-Inf. (Univ.) Henning P. Schmiedehausen          INTERMETA GmbH        +49 9131 50 654 0

RedHat Certified Engineer -- Jakarta Turbine Development  -- hero for hire
   Linux, Java, perl, Solaris -- Consulting, Training, Development

		      4 - 8 - 15 - 16 - 23 - 42

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message