Last active
December 26, 2015 18:39
-
-
Save thomasjungblut/7196135 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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