kristofer revised this gist . Go to revision
No changes
kristofer revised this gist . Go to revision
3 files changed, 110 insertions
millions.java(file created)
| @@ -0,0 +1,58 @@ | |||
| 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(file created)
| @@ -0,0 +1,14 @@ | |||
| 1 | + | # Millions | |
| 2 | + | ||
| 3 | + | run some loops in the millions arrays vs. arraylists | |
| 4 | + | ||
| 5 | + | Sometimes, you just gotta mess about with code. | |
| 6 | + | As in run experiments. | |
| 7 | + | To see how things work, and to realize that 1 million is really a small number in a modern computer. | |
| 8 | + | ||
| 9 | + | So, look at these examples, and maybe gen a project which copies this code and then run it an see what happens. | |
| 10 | + | ||
| 11 | + | ### And then, PLAY with it. | |
| 12 | + | ||
| 13 | + | ||
| 14 | + | ||
millionstext.java(file created)
| @@ -0,0 +1,38 @@ | |||
| 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 | + | } | |