Java Reference
In-Depth Information
System
.
out
.
println
(
new
new
ArrayLst
().
run
());
}
public
public
int
int
run
() {
ArrayList
<
MutableInteger
>
list
=
new
new
ArrayList
<>();
for
for
(
int
int
i
=
0
;
i
<
MAX
;
i
++) {
list
.
add
(
new
new
MutableInteger
(
i
));
}
int
int
sum
=
0
;
for
for
(
int
int
i
=
0
;
i
<
MAX
;
i
++) {
sum
+= ((
MutableInteger
)
list
.
get
(
i
)).
getValue
();
}
return
return
sum
;
}
}
The
Vector
-based version,
ArrayVec
, is sufficiently similar that I don't feel the need to kill
a tree reprinting its codeāit's online.
How can we time this? As covered in
Performance Timing
, you can either use the operating
system's
time
command, if available, or just use a bit of Java that times a run of your main
program. To be portable, I chose to use the latter on an older, slower machine. Its exact speed
doesn't matter because the important thing is to compare only versions of this program run-
ning on the same machine.
Finally (drum roll, please), the results:
$
java performance.Time Array
Starting class class Array
1185103928
runTime=4.310
$
java performance.Time ArrayLst
Starting class class ArrayLst
1185103928
runTime=5.626
$
java performance.Time ArrayVec
Starting class class ArrayVec
1185103928
runTime=6.699
$