// {Args: 500} import java...
Serwis znalezionych hasełOdnośniki
- Smutek to uczucie, jak gdyby się tonęło, jak gdyby grzebano cię w ziemi.
- public static void main(String[] args) { analyze("I am happy about this"); analyze("I am not happy about this"); analyze("I am not! I am happy");...
- Some Java ORB products offer proprietary solutions to this problem...
- Appendix B: Comparing C++ and Java 823 47...
- - Czy jej matka wie, co się stało?Ertekin potrząsnął głowę...
- KSA100ET4AF BARIERA OGNIOWA 100A 178900KSA100EV4203 ELEMENT PIONOWY 100A 2M 70590KSA100EV4254 ELEMENT PIONOWY 100A...
- Godzina 20...
- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxc
- Katalog domowy użytkownika root to /root...
- W podobny sposób nawet całe wehikuły mogą też zmieniać wygląd...
- Śledzili oni również kilka czynników, z którymi ich nastrój mógł być skorelowany, na przykład ilość snu ostatniej nocy oraz pogodę...
Smutek to uczucie, jak gdyby się tonęło, jak gdyby grzebano cię w ziemi.
util.*;
import com.bruceeckel.util.*;
public class ListPerformance {
private static int reps = 10000;
private static int quantity = reps / 10;
private abstract static class Tester {
private String name;
Tester(String name) { this.name = name; }
abstract void test(List a);
}
582
Thinking in Java
www.BruceEckel.com
private static Tester[] tests = {
new Tester("get") {
void test(List a) {
for(int i = 0; i < reps; i++) {
for(int j = 0; j < quantity; j++)
a.get(j);
}
}
},
new Tester("iteration") {
void test(List a) {
for(int i = 0; i < reps; i++) {
Iterator it = a.iterator();
while(it.hasNext())
it.next();
}
}
},
new Tester("insert") {
void test(List a) {
int half = a.size()/2;
String s = "test";
ListIterator it = a.listIterator(half);
for(int i = 0; i < reps * 10; i++)
it.add(s);
}
},
new Tester("remove") {
void test(List a) {
ListIterator it = a.listIterator(3);
while(it.hasNext()) {
it.next();
it.remove();
}
}
},
};
public static void test(List a) {
// Strip qualifiers from class name:
System.out.println("Testing " +
a.getClass().getName().replaceAll("\\w+\\.", ""));
for(int i = 0; i < tests.length; i++) {
Collections2.fill(a, Collections2.countries.reset(),
quantity);
Chapter 11: Collections of Objects
583
System.out.print(tests[i].name);
long t1 = System.currentTimeMillis();
tests[i].test(a);
long t2 = System.currentTimeMillis();
System.out.println(": " + (t2 - t1));
}
}
public static void testArrayAsList(int reps) {
System.out.println("Testing array as List");
// Can only do first two tests on an array:
for(int i = 0; i < 2; i++) {
String[] sa = new String[quantity];
Arrays2.fill(sa, Collections2.countries.reset());
List a = Arrays.asList(sa);
System.out.print(tests[i].name);
long t1 = System.currentTimeMillis();
tests[i].test(a);
long t2 = System.currentTimeMillis();
System.out.println(": " + (t2 - t1));
}
}
public static void main(String[] args) {
// Choose a different number of
// repetitions via the command line:
if(args.length > 0)
reps = Integer.parseInt(args[0]);
System.out.println(reps + " repetitions");
testArrayAsList(reps);
test(new ArrayList());
test(new LinkedList());
test(new Vector());
}
} ///:~
The inner class Tester is abstract, to provide a base class for the specific tests. It contains a String to be printed when the test starts, and an
abstract method test( ) that does the work. All the different types of
tests are collected in one place, the array tests, which is initialized with
different anonymous inner classes that inherit from Tester. To add or
remove tests, simply add or remove an inner class definition from the
array, and everything else happens automatically. Feedback
To compare array access to container access (primarily against
ArrayList), a special test is created for arrays by wrapping one as a List
584
Thinking in Java
www.BruceEckel.com
using Arrays.asList( ). Note that only the first two tests can be
performed in this case, because you cannot insert or remove elements
from an array. Feedback
The List that’s handed to test( ) is first filled with elements, then each
test in the tests array is timed. The results will vary from machine to
machine; they are intended to give only an order of magnitude
comparison between the performance of the different containers. Here is
a summary of one run: Feedback
Type Get
Iteration
Insert
Remove
array 172
516 na
na
ArrayList
281 1375 328 30484
LinkedList 5828 1047
109 16
Vector
422 1890 360 30781
As expected, arrays are faster than any container for random-access
lookups and iteration. You can see that random accesses (get( )) are
cheap for ArrayLists and expensive for LinkedLists. (Oddly, iteration
is faster for a LinkedList than an ArrayList, which is a bit
counterintuitive.) On the other hand, insertions and removals from the
middle of a list are dramatically cheaper for a LinkedList than for an
ArrayList— especially removals. Vector is generally not as fast as
ArrayList, and it should be avoided; it’s only in the library for legacy
code support (the only reason it works in this program is because it was
adapted to be a List in Java 2). The best approach is probably to choose
an ArrayList as your default, and to change to a LinkedList if you
discover performance problems due to many insertions and removals
from the middle of the list. And of course, if you are working with a fixed-
sized group of elements, use an array. Feedback
Choosing between Sets
You can choose between a TreeSet, a HashSet, and a LinkedHashSet,
depending on the behavior you desire. The following test program gives
an indication of the performance trade-off between the implementations:
Feedback
//: c11:SetPerformance.java
Chapter 11: Collections of Objects
585
// {Args: 500}
import java.util.*;
import com.bruceeckel.util.*;
public class SetPerformance {
private static int reps = 50000;
private abstract static class Tester {
String name;
Tester(String name) { this.name = name; }