Q: What other operations are surprisingly slow?

Answer:
Where do I begin? Here are a few that are most useful to know
about. I wrote a timing utility that runs snippets of code in a loop,
reporting the results in terms of thousands of iterations per second
(K/sec) and microseconds per iteration (uSecs). Timing was done on a
Sparc 20 with the JDK 1.1.4 JIT compiler. I note the following:

These were all done in 1998. Compilers have changed since then.

Counting down (i.e. for (int i=n; i>0; i--)) is twice as
fast as counting up: my machine can count down to 144 million in a
second, but up to only 72 million.

Calling Math.max(a,b) is 7 times slower than (a > b)
? a : b. This is the cost of a method call.

Arrays are 15 to 30 times faster than Vectors. Hashtables are 2/3
as fast as Vectors.

Using bitset.get(i) is 60 times slower than bits
& 1 << i. This is the cost of a synchronized method
call, mostly. Of course, if you want more than 64 bits, you can't use
my bit-twiddling example. Here's a chart of times for getting and
setting elements of various data structures:

Java compilers are very poor at lifting constant expressions out
of loops. The C/Java for loop is a bad abstraction, because
it encourages re-computation of the end value in the most typical
case. So for(int i=0; i is three times
slower than int len = str.length(); for(int i=0; i

This tip is reprinted on JavaFAQ.nu by by courtesy of
Peter Norvig I am
thankful for his important contributions to my site - 21 Infrequently Answered
Java Questions. Alexandre Patchine