img
. .
Appendix B. Timings
Timings
Timings
The choice of which synchronization variable to use depends partially on its execution speed. This
is particularly applicable when choosing between using a mutex lock and a readers/writer lock.
The design of programs calling Java functions in tight loops will also depend upon these numbers
for optimizations. For the most part, however, all of these times are short enough that they may be
ignored.
Because of the dependence of these tests upon several unusual instructions (ldstub and stbar
on SPARC), machines with different cache or bus designs will exhibit nonuniform scaling
(meaning that a context switch may be twice as fast on a 20-MHz processor as it is on a 10-MHz
processor, but locking a mutex might take the same amount of time). Different releases of Java
may also exhibit different timings.
Execution times on other platforms may also differ significantly, but probably in roughly the same
ratios (e.g., creating a thread will be a couple of orders of magnitude faster than creating a
process). The one obvious exception to this is the semaphore, which should be almost as fast as
mutexes on machines with the more complex atomic instructions.
The major conclusions you should draw from these numbers are:
·
Synchronized sections are faster than RWlocks.
·
Testing for interruption is moderately fast. Disabling it is slower.
·
Processes are more expensive than threads.
·
TSD is slower than just using instance variables ("fake" TSD).
The programs we ran to get the numbers shown in Table C-1 are available on the Web.
The tests in C are in the PThreads directory, and those in Java are all in the TimeTests directory.
The Java tests are run by calling
%java Test.
Mutex Lock/Unlock
Acquire, then release, a POSIX mutex (Java Mutex) with no contention.
Table C-1. Timings of Various Thread-Related Functions on POSIX and Java (µs)
Function
PThreads
Java 1.1.5
Java 2
Mutex lock/unlock
1.8
30
30
Explicit synchronized
10
3
Implicit synchronized
13
4
Readers/writer lock/unlock
4.5
70
80
Semaphore post/wait
4.3
40
20
object.notify()
n/a
3
3
Search WWH :
Custom Search
Previous Page
Multithreaded Programming with JAVA - Topic Index
Next Page
Multithreaded Programming with JAVA - Bookmarks
Home