millions.java
· 1.6 KiB · Java
原始檔案
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
public class Millions {
private Integer[] aArray;
private ArrayList<Integer> aList;
public Millions(Integer limit) {
aArray = new Integer[limit];
aList = new ArrayList<Integer>();
System.out.printf("Limit is %,d\n", limit);
}
public void printDuration(String msg, Instant start, Instant finish) {
long timeElapsed = Duration.between(start, finish).toMillis();
System.out.printf("%s: Elapsed time (ms): %s (secs): %s \n", msg, String.valueOf(timeElapsed),
String.valueOf(timeElapsed/1000));
}
public void loops(Integer limit) {
Instant start;
Instant finish;
Integer tempInt = 0;
start = Instant.now();
for (int i = 0; i < limit; i++) {
aArray[i] = i;
}
finish = Instant.now();
printDuration("Array Set", start, finish);
start = Instant.now();
for (int i = 0; i < limit; i++) {
tempInt = aArray[i];
tempInt = i;
}
finish = Instant.now();
printDuration("Array Get", start, finish);
start = Instant.now();
for (int i = 0; i < limit; i++) {
aList.add(i);
}
finish = Instant.now();
printDuration("ArrayList Set", start, finish);
start = Instant.now();
for (int i = 0; i < limit; i++) {
tempInt = aList.get(i);
tempInt = i;
}
finish = Instant.now();
printDuration("ArrayList Get", start, finish);
}
}
1 | import java.time.Duration; |
2 | import java.time.Instant; |
3 | import java.util.ArrayList; |
4 | |
5 | public class Millions { |
6 | |
7 | |
8 | private Integer[] aArray; |
9 | private ArrayList<Integer> aList; |
10 | |
11 | public Millions(Integer limit) { |
12 | aArray = new Integer[limit]; |
13 | aList = new ArrayList<Integer>(); |
14 | System.out.printf("Limit is %,d\n", limit); |
15 | } |
16 | |
17 | public void printDuration(String msg, Instant start, Instant finish) { |
18 | long timeElapsed = Duration.between(start, finish).toMillis(); |
19 | System.out.printf("%s: Elapsed time (ms): %s (secs): %s \n", msg, String.valueOf(timeElapsed), |
20 | String.valueOf(timeElapsed/1000)); |
21 | } |
22 | |
23 | public void loops(Integer limit) { |
24 | Instant start; |
25 | Instant finish; |
26 | Integer tempInt = 0; |
27 | |
28 | start = Instant.now(); |
29 | for (int i = 0; i < limit; i++) { |
30 | aArray[i] = i; |
31 | } |
32 | finish = Instant.now(); |
33 | printDuration("Array Set", start, finish); |
34 | |
35 | start = Instant.now(); |
36 | for (int i = 0; i < limit; i++) { |
37 | tempInt = aArray[i]; |
38 | tempInt = i; |
39 | } |
40 | finish = Instant.now(); |
41 | printDuration("Array Get", start, finish); |
42 | |
43 | start = Instant.now(); |
44 | for (int i = 0; i < limit; i++) { |
45 | aList.add(i); |
46 | } |
47 | finish = Instant.now(); |
48 | printDuration("ArrayList Set", start, finish); |
49 | |
50 | start = Instant.now(); |
51 | for (int i = 0; i < limit; i++) { |
52 | tempInt = aList.get(i); |
53 | tempInt = i; |
54 | } |
55 | finish = Instant.now(); |
56 | printDuration("ArrayList Get", start, finish); |
57 | } |
58 | } |
millions.md
· 384 B · Markdown
原始檔案
# Millions
run some loops in the millions arrays vs. arraylists
Sometimes, you just gotta mess about with code.
As in run experiments.
To see how things work, and to realize that 1 million is really a small number in a modern computer.
So, look at these examples, and maybe gen a project which copies this code and then run it an see what happens.
### And then, PLAY with it.
Millions
run some loops in the millions arrays vs. arraylists
Sometimes, you just gotta mess about with code. As in run experiments. To see how things work, and to realize that 1 million is really a small number in a modern computer.
So, look at these examples, and maybe gen a project which copies this code and then run it an see what happens.
And then, PLAY with it.
millionstext.java
· 732 B · Java
原始檔案
import static org.junit.Assert.*;
public class MillionsTest {
@org.junit.Test
public void loops() {
Integer upperlimit = 1000000;
Millions m = new Millions(upperlimit);
m.loops(upperlimit);
upperlimit = 5000000;
m = new Millions(upperlimit);
m.loops(upperlimit);
upperlimit = 10000000;
m = new Millions(upperlimit);
m.loops(upperlimit);
upperlimit = 20000000;
m = new Millions(upperlimit);
m.loops(upperlimit);
upperlimit = 50000000;
m = new Millions(upperlimit);
m.loops(upperlimit);
upperlimit = 70000000;
m = new Millions(upperlimit);
m.loops(upperlimit);
}
}
1 | import static org.junit.Assert.*; |
2 | |
3 | public class MillionsTest { |
4 | |
5 | @org.junit.Test |
6 | public void loops() { |
7 | Integer upperlimit = 1000000; |
8 | Millions m = new Millions(upperlimit); |
9 | |
10 | m.loops(upperlimit); |
11 | |
12 | upperlimit = 5000000; |
13 | m = new Millions(upperlimit); |
14 | |
15 | m.loops(upperlimit); |
16 | |
17 | upperlimit = 10000000; |
18 | m = new Millions(upperlimit); |
19 | |
20 | m.loops(upperlimit); |
21 | |
22 | upperlimit = 20000000; |
23 | m = new Millions(upperlimit); |
24 | |
25 | m.loops(upperlimit); |
26 | |
27 | upperlimit = 50000000; |
28 | m = new Millions(upperlimit); |
29 | |
30 | m.loops(upperlimit); |
31 | |
32 | upperlimit = 70000000; |
33 | m = new Millions(upperlimit); |
34 | |
35 | m.loops(upperlimit); |
36 | |
37 | } |
38 | } |