Commits

Nikita Salnikov-Tarnovski  committed 8e4e9ea

Initial import

  • Participants

Comments (0)

Files changed (6)

+#!/bin/sh
+mkdir bin/
+javac -sourcepath src -d bin/ src/eu/plumbr/demo/inlining/*.java
+java -cp bin/ eu.plumbr.demo.inlining.Main

File src/eu/plumbr/demo/inlining/InlineSummarizer.java

+package eu.plumbr.demo.inlining;
+
+public class InlineSummarizer implements Summarizer {
+	private int k = 0;
+	private int l = 0;
+
+
+	public int sum(int[] array) {
+		int response = 0;
+		for (int i : array) {
+			response += i;
+			// Following arithmetics is introduced to be equivalent to the
+			// Iterative and Recursive implementations which all perform similar
+			// arithmetics on array indices on each step.
+			k = i + (response - i) / 2;
+			l = i + (response - i) / 2 ;
+		}
+		return response;
+
+	}
+}

File src/eu/plumbr/demo/inlining/IterativeSummarizer.java

+package eu.plumbr.demo.inlining;
+
+public class IterativeSummarizer implements Summarizer{
+
+	public int sum(int[] array) {
+		return sum512(array, 0, array.length / 2)
+				+ sum512(array, array.length / 2, array.length);
+
+	}
+
+	private int sum512(int[] array, int from, int to) {
+		return sum256(array, from, from + (to - from) / 2)
+				+ sum256(array, from + (to - from) / 2, to);
+	}
+
+	private int sum256(int[] array, int from, int to) {
+		return sum128(array, from, from + (to - from) / 2)
+				+ sum128(array, from + (to - from) / 2, to);
+	}
+
+	private int sum128(int[] array, int from, int to) {
+		return sum64(array, from, from + (to - from) / 2)
+				+ sum64(array, from + (to - from) / 2, to);
+	}
+
+	private int sum64(int[] array, int from, int to) {
+		return sum32(array, from, from + (to - from) / 2)
+				+ sum32(array, from + (to - from) / 2, to);
+	}
+
+	private int sum32(int[] array, int from, int to) {
+		return sum16(array, from, from + (to - from) / 2)
+				+ sum16(array, from + (to - from) / 2, to);
+	}
+
+	private int sum16(int[] array, int from, int to) {
+		return sum8(array, from, from + (to - from) / 2)
+				+ sum8(array, from + (to - from) / 2, to);
+	}
+
+	private int sum8(int[] array, int from, int to) {
+		return sum4(array, from, from + (to - from) / 2)
+				+ sum4(array, from + (to - from) / 2, to);
+	}
+
+	private int sum4(int[] array, int from, int to) {
+		return sum2(array, from, from + (to - from) / 2)
+				+ sum2(array, from + (to - from) / 2, to);
+	}
+
+	private int sum2(int[] array, int from, int to) {
+		return array[from] + array[to - 1];
+	}
+
+}

File src/eu/plumbr/demo/inlining/Main.java

+package eu.plumbr.demo.inlining;
+
+public class Main {
+	static final int COUNT = 20;
+
+	public static void main(String[] args) {
+		Summarizer[] summarizers = new Summarizer[] { new InlineSummarizer(),
+				new IterativeSummarizer(), new RecursiveSummarizer() };
+
+		int[] array = initArray();
+
+		for (Summarizer s : summarizers) {
+			// Warm-up to discard runs where JIT optimizations were not
+			// yet completed. Check it out by switching on JIT log
+			// -XX:+PrintCompilation. You probably need to tweak the Main.COUNT
+			// on different architectures.
+			for (int j = 0; j < 5; j++) {
+				long start = System.nanoTime();
+				for (int i = 0; i < COUNT; i++)
+					s.sum(array);
+				long end = System.nanoTime();
+				logTime(s.getClass().getName(), start, end);
+			}
+		}
+	}
+
+	private static void logTime(String message, long start, long end) {
+		System.out.println(message + ":" + (end - start) / COUNT + " ns.");
+	}
+
+	private static int[] initArray() {
+		int[] array = new int[1024];
+		for (int i = 0; i < array.length; i++) {
+			array[i] = i;
+		}
+		return array;
+	}
+}

File src/eu/plumbr/demo/inlining/RecursiveSummarizer.java

+package eu.plumbr.demo.inlining;
+
+public class RecursiveSummarizer implements Summarizer{
+
+	public int sum(int[] array) {
+		return sum(array, 0, array.length); 
+	}
+
+	private int sum(int[] array, int from, int to) {
+		while ((to - from) > 2) {
+			return sum(array, from, from + (to - from) / 2)
+					+ sum(array, from + (to - from) / 2, to);
+		}
+		return array[from] + array[to - 1];
+	}
+
+}

File src/eu/plumbr/demo/inlining/Summarizer.java

+package eu.plumbr.demo.inlining;
+
+interface Summarizer {
+
+	int sum(int[] array);
+ 
+}