|
|
|
1000 Java Tips ebook
|
|
 

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java
programming articles and tips. Please take your copy here.
Take your copy of free "Java Technology Screensaver"!. |
|
Easy Learn Java: Programming Articles, Examples and Tips - Page 430
Previous
1060 Stories (530 Pages, 2 Per Page)
Next
Q: Can I get good advice from books on Java?
|
Q: Can I get good advice from
books on Java?
Answer:
There are a lot of Java books out there, falling into three classes:
Bad. Most Java books are written by people who couldn't get a
job as a Java programmer (since programming almost always pays more
than book writing; I know because I've done both). These books are
full of errors, bad advice, and bad programs. These books are
dangerous to the beginner, but are easily recognized and rejected by a
programmer with even a little experience in another language.
Excellent.
There are a small number of excellent Java books. I like the
official
specification and the books by
Arnold and
Gosling,
Marty Hall,
and Peter van
der Linden.
For reference I like the
Java in a Nutshell series and
the online references at Sun (I copy
the javadoc APIs and
the language
specification and its
amendments to my local disk and bookmark them in my browser so I'll always have fast access.)
Iffy. In between these two extremes is a collection of sloppy
writing by people who should know better, but either haven't taken the
time to really understand how Java works, or are just rushing to get
something published fast. One such example of half-truths is
Edward Yourdon's
Java
and the new Internet programming paradigm from Rise and
Resurrection of the American Programmer
[footnote on Yourdon]. Here's what Yourdon says
about how different Java is:
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
3108 bytes more | comments? | | Score: 0
|
Posted by jalex on Friday, January 20, 2006 (00:00:00) (2383 reads)
|
Q: What other operations are surprisingly slow?
|
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:
K/sec uSecs Code Operation
========= ======= ==================== ===========
147,058 0.007 a = a & 0x100; get element of int bits
314 3.180 bitset.get(3); get element of Bitset
20,000 0.050 obj = objs[1]; get element of Array
5,263 0.190 str.charAt(5); get element of String
361 2.770 buf.charAt(5); get element of StringBuffer
337 2.960 objs2.elementAt(1); get element of Vector
241 4.140 hash.get("a"); get element of Hashtable
336 2.970 bitset.set(3); set element of Bitset
5,555 0.180 objs[1] = obj; set element of Array
355 2.810 buf.setCharAt(5,' ') set element of StringBuffer
308 3.240 objs2.setElementAt(1 set element of Vector
237 4.210 hash.put("a", obj); set element of Hashtable |
- 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
1662 bytes more | | Score: 0
|
Posted by jalex on Wednesday, January 18, 2006 (00:00:00) (2389 reads)
|
|
|