Skip to content

Instantly share code, notes, and snippets.

@thomasjungblut
Last active December 26, 2015 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasjungblut/7196135 to your computer and use it in GitHub Desktop.
Save thomasjungblut/7196135 to your computer and use it in GitHub Desktop.
package de.jungblut.benchmark;
import java.util.ArrayList;
import com.google.caliper.Param;
import com.google.caliper.Runner;
import com.google.caliper.SimpleBenchmark;
public class PerfectSquaresBenchmark extends SimpleBenchmark {
@Param
InitType type;
public enum InitType {
DEFAULT, INIT
};
ArrayList<Integer> result;
private int[] arr;
@Override
protected void setUp() throws Exception {
arr = new int[5000000];
for (int i = 0; i < arr.length; i++) {
arr[i] = i + 1;
}
newList();
}
public void newList() {
switch (type) {
case DEFAULT:
result = new ArrayList<>();
break;
case INIT:
result = new ArrayList<>(5_000_000);
break;
default:
break;
}
}
public void perfectSquares(int[] arr) {
for (int i = 0; i < arr.length; i++) {
double root = Math.sqrt(arr[i]);
int irt = (int) Math.floor(root);
if (irt * irt == arr[i]) {
result.add(arr[i]);
}
}
}
public void timeAppend(int reps) {
for (int rep = 0; rep < reps; rep++) {
newList();
perfectSquares(arr);
System.out.println(result.size());
}
}
public static void main(String[] args) {
Runner.main(PerfectSquaresBenchmark.class, args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment