Information Technology Reference
In-Depth Information
A typical session for compiling and linking a C CC program in a file int.cpp
on a Unix machine, using the g++ compiler, goes like this:
unix> g++ -O3 -c int.cpp # compiling int.cpp to int.o
unix> g++ -o int int.o # linking; form executable int
These steps are the same as for Fortran 77 and will be similar for a C program; only
the name of the compiler differs. Executing the program follows the description for
the corresponding Fortran 77 program; just name the executable file.
The CPU time can be measured in C and C CC programs by calling the clock
function. We have not done this. Instead we rely on the Unix time command. The
performance test, involving calling the Trapezoidal function 10,000 times with
n D 1;000 , ran at the same speed as our Fortran 77 code. Normally, C and C CC
run at the same or slightly lower speed than Fortran 77.
6.3.4
Java
Java was constructed by Sun Microsystems in the beginning of the 1990s and can
be viewed as a simpler and more user-friendly version of C CC . At the time Java
was released, C CC had recently become a very popular and dominating language,
but Java appeared to be more convenient and productive, so many programmers
converted quickly to Java. Java is now the most popular programming language in
the world.
Java programs are not compiled in the classic sense of Fortran, C, and C CC so
performance was a significant negative feature for computational scientists. Despite
numerous efforts to improve performance (and it is now indeed possible to obtain
execution times close to those of C CC in some applications), the initial interest in
Java as a language for scientific computing has declined. However, Java has become
the dominant language for teaching introductory computer programming, so in the
early stages of student programs it makes great sense to use Java for numerical
computing. In the professional world of scientific computing, Fortran 77 dominates,
but C CC and Matlab have a significant share.
Let us look at a Java implementation of Algorithm 6.2 . At first sight, this imple-
mentation is significantly less straightforward than the implementations in Fortran
or C/C CC . The reason is because one has to work with classes, because all func-
tions in Java must belong to a class. The bottom line is, therefore, that we implement
the functions from our Fortran and C/C CC programs as functions in otherwise
empty Java classes. We also note that functions in Java are referred to as methods .
import java.lang. * ;
interface Func { // base class for functions f(x)
public double f (double x); // default (empty) impl.
}
class Trapezoidal {
public static double integrate (double a, double b,
Func f, int n)
Search WWH ::




Custom Search