map = new HashMap<>(128, 0.75f);
22 | Key key = new Key(0);
23 | for (int i = 0; i <= 10; i++) {
24 | key.num = i;
25 | map.put(key, "" + i);
26 | }
27 | map.entrySet().forEach(System.out::println);
28 | String s = map.get(new Key(10));
29 | System.out.println("s: "+s);
30 | */
31 | }
32 |
33 | static final int hash(Object key) {
34 | int h;
35 | return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
36 | }
37 | }
38 |
39 | class Key {
40 | int num;
41 |
42 | public Key(int i) {
43 | num = i;
44 | }
45 |
46 | @Override
47 | public boolean equals(Object o) {
48 | if (this == o) return true;
49 | if (o == null || getClass() != o.getClass()) return false;
50 |
51 | Key key = (Key) o;
52 |
53 | return num == key.num;
54 |
55 | }
56 |
57 | @Override
58 | public int hashCode() {
59 | return num * 0x79216473;
60 | }
61 |
62 | @Override
63 | public String toString() {
64 | return "Key{" +
65 | "num=" + num +
66 | '}';
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/src/main/java/vanilla/java/math/NativeCompute.java:
--------------------------------------------------------------------------------
1 | package vanilla.java.math;
2 |
3 | /**
4 | * Created by peter.lawrey on 14/01/2016.
5 | */
6 | public class NativeCompute {
7 | public static double access(BytesMatrix matrix, int row, int column) {
8 | return access(matrix.address(), row, column);
9 | }
10 |
11 | public static native double access(long address, int row, int column);
12 | }
13 |
--------------------------------------------------------------------------------
/src/main/java/vanilla/java/math/NativeMatrix.java:
--------------------------------------------------------------------------------
1 | package vanilla.java.math;
2 |
3 | import sun.misc.Unsafe;
4 |
5 | import java.lang.reflect.Field;
6 |
7 | /**
8 | * Created by peter.lawrey on 14/01/2016.
9 | */
10 | public class NativeMatrix {
11 | // TODO use Bytes instead.
12 | static final Unsafe UNSAFE;
13 | private static final long ROWS_OFFSET = 0L;
14 | private static final long COLUMNS_OFFSET = 4L;
15 | private static final long HEADER_SIZE = 8L;
16 |
17 | static {
18 | try {
19 | Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
20 | theUnsafe.setAccessible(true);
21 | UNSAFE = (Unsafe) theUnsafe.get(null);
22 | } catch (Exception e) {
23 | throw new AssertionError(e);
24 | }
25 | }
26 |
27 | private final long address;
28 | private final int rows;
29 | private final int columns;
30 |
31 | public NativeMatrix(int rows, int columns) {
32 | this.rows = rows;
33 | this.columns = columns;
34 | long size = calcSize(rows, columns);
35 | address = UNSAFE.allocateMemory(size);
36 | UNSAFE.setMemory(address, size, (byte) 0);
37 | UNSAFE.putInt(address + ROWS_OFFSET, rows);
38 | UNSAFE.putInt(address + COLUMNS_OFFSET, columns);
39 | }
40 |
41 | private long calcSize(int rows, int columns) {
42 | return HEADER_SIZE + rows * columns * Double.BYTES;
43 | }
44 |
45 | @Override
46 | protected void finalize() throws Throwable {
47 | UNSAFE.freeMemory(address);
48 | }
49 |
50 | public void set(int row, int column, double value) {
51 | long index = index(row, column);
52 | UNSAFE.putDouble(index, value);
53 | }
54 |
55 | public double get(int row, int column) {
56 | long index = index(row, column);
57 | return UNSAFE.getDouble(index);
58 | }
59 |
60 | private long index(int row, int column) {
61 | if (row < 0 || row >= rows) throw new IllegalArgumentException();
62 | if (column < 0 || column >= columns) throw new IllegalArgumentException();
63 | return HEADER_SIZE + (row * columns + column) * Double.BYTES;
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/src/main/java/vanilla/java/math/Prime.java:
--------------------------------------------------------------------------------
1 | package vanilla.java.math;
2 |
3 | import java.util.stream.IntStream;
4 |
5 | /**
6 | * Created by peter_2 on 25/11/2014.
7 | */
8 | public class Prime {
9 | boolean isPrime(int n) {
10 | return (n & 1) != 0 &&
11 | IntStream.rangeClosed(1, (int) Math.sqrt(n) / 2)
12 | .noneMatch(i -> n % (i * 2 + 1) == 0);
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/main/java/vanilla/java/math/SqrtMain.java:
--------------------------------------------------------------------------------
1 | package vanilla.java.math;
2 |
3 | /**
4 | * Created by peter.lawrey on 26/01/2016.
5 | */
6 | public class SqrtMain {
7 | public static double sqrt(double ans) {
8 | if (ans < 1)
9 | return 1.0 / sqrt(1.0 / ans);
10 | double guess = 1;
11 | double add = ans / 2;
12 | while (add >= Math.ulp(guess)) {
13 | double guess2 = guess + add;
14 | double result = guess2 * guess2;
15 | if (result < ans)
16 | guess = guess2;
17 | else if (result == ans)
18 | return guess2;
19 | add /= 2;
20 | }
21 | return guess;
22 | }
23 |
24 | public static void main(String[] args) {
25 | for (int i = 0; i <= 10; i++)
26 | System.out.println(i / 10.0 + ": " + sqrt(i / 10.0) + " vs " + Math.sqrt(i / 10.0));
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/vanilla/java/memory/ArrayAllocationMain.java:
--------------------------------------------------------------------------------
1 | package vanilla.java.memory;
2 |
3 | /**
4 | * Created by peter on 23/02/16.
5 | */
6 | public class ArrayAllocationMain {
7 | public static void main(String[] args) {
8 |
9 | long used1 = memoryUsed();
10 | int[][] array = new int[200][2];
11 |
12 | long used2 = memoryUsed();
13 | int[][] array2 = new int[2][200];
14 |
15 | long used3 = memoryUsed();
16 | if (used1 == used2) {
17 | System.err.println("You need to turn off the TLAB with -XX:-UseTLAB");
18 | } else {
19 | System.out.printf("Space used by int[200][2] is " + (used2 - used1) + " bytes%n");
20 | System.out.printf("Space used by int[2][200] is " + (used3 - used2) + " bytes%n");
21 | }
22 | }
23 |
24 | public static long memoryUsed() {
25 | Runtime rt = Runtime.getRuntime();
26 | return rt.totalMemory() - rt.freeMemory();
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/vanilla/java/memory/VisualVMMain.java:
--------------------------------------------------------------------------------
1 | package vanilla.java.memory;
2 |
3 | import java.io.IOException;
4 | import java.util.stream.IntStream;
5 |
6 | /**
7 | * Created by peter.lawrey on 11/01/2016.
8 | */
9 | public class VisualVMMain {
10 | public static void main(String... args) throws IOException, InterruptedException {
11 | for (int i = 0; ; i++) {
12 | if (runTest(i) < 0)
13 | throw new AssertionError();
14 | }
15 | }
16 |
17 | private static long runTest(int i) {
18 | return IntStream.range(0, 10)
19 | .map(x -> x + i)
20 | .count();
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/vanilla/java/objects/CleanerMain.java:
--------------------------------------------------------------------------------
1 | package vanilla.java.objects;
2 |
3 | import sun.misc.Cleaner;
4 |
5 | import java.util.RandomAccess;
6 |
7 | /**
8 | * Created by peter on 11/10/2016.
9 | */
10 | public class CleanerMain {
11 | public static void main(String[] args) throws InterruptedException {
12 | new RandomAccess() {
13 | public void finalize() {
14 | new Throwable(Thread.currentThread().getName()).printStackTrace();
15 | }
16 | };
17 | Cleaner.create(new Integer(1), () ->
18 | new Throwable(Thread.currentThread().getName()).printStackTrace());
19 | System.gc();
20 | Thread.sleep(1000);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/vanilla/java/objects/HowManyObjects2Main.java:
--------------------------------------------------------------------------------
1 | package vanilla.java.objects;
2 |
3 | import java.io.IOException;
4 | import java.util.stream.IntStream;
5 |
6 | /**
7 | * Created by peter.lawrey on 23/11/2015.
8 | */
9 | public class HowManyObjects2Main {
10 | public static void main(String[] args) throws IOException {
11 | IntStream.range(0, 10)
12 | .distinct()
13 | .forEach(System.out::println);
14 | System.in.read();
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/vanilla/java/objects/HowManyObjectsMain.java:
--------------------------------------------------------------------------------
1 | package vanilla.java.objects;
2 |
3 | import java.io.IOException;
4 |
5 | /**
6 | * Created by peter.lawrey on 23/11/2015.
7 | */
8 | public class HowManyObjectsMain {
9 | public static void main(String[] args) throws IOException {
10 | String a = "hello";
11 | String b = "world";
12 | String c = a + ' ' + b;
13 | System.out.println(c);
14 |
15 | System.in.read();
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/vanilla/java/optimisation/MemoryOptimisation2Main.java:
--------------------------------------------------------------------------------
1 | package vanilla.java.optimisation;
2 |
3 | import java.util.*;
4 | import java.util.concurrent.ExecutionException;
5 | import java.util.concurrent.ExecutorService;
6 | import java.util.concurrent.Executors;
7 | import java.util.concurrent.Future;
8 | import java.util.function.Function;
9 | import java.util.logging.Logger;
10 |
11 | /**
12 | * git clone https://github.com/peter-lawrey/Performance-Examples
13 | * mvn install
14 | *
15 | * Memory optimisation sample program.
16 | *
17 | * This is the inefficient version.
18 | *
19 | * Created by peter.
20 | */
21 | public class MemoryOptimisation2Main {
22 | static final Logger LOGGER = Logger.getLogger(MemoryOptimisation2Main.class.getName());
23 |
24 | public static void main(String... args) throws ExecutionException, InterruptedException {
25 |
26 | for (int t = 0; t < 5; t++) {
27 | final int range = 1000, samples = 10000000, tasks = 4;
28 | long start = System.nanoTime();
29 | Map map = new HashMap<>();
30 | ExecutorService es = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
31 | List>> futures = new ArrayList<>();
32 | for (int i = 0; i < tasks; i++)
33 | futures.add(es.submit(() -> {
34 | Function mappingFunction = k -> new int[1];
35 | Random rand = new Random();
36 | Map map2 = new HashMap<>();
37 | for (int j = 0; j < samples / tasks; j++) {
38 | int next = rand.nextInt(range);
39 | int[] counter = map2.computeIfAbsent(next,
40 | mappingFunction);
41 | counter[0]++;
42 | // int count = counter.incrementAndGet();
43 | // LOGGER.finest(next + ": " + count);
44 | }
45 | return map2;
46 | }));
47 |
48 | for (Future