Skip to content

Instantly share code, notes, and snippets.

@VladVin
Last active September 20, 2016 10:33
Show Gist options
  • Save VladVin/5bc92839c110894588fc90247643f30a to your computer and use it in GitHub Desktop.
Save VladVin/5bc92839c110894588fc90247643f30a to your computer and use it in GitHub Desktop.
package becnhmark;
import com.google.caliper.BeforeExperiment;
import com.google.caliper.Param;
import sort_algs.Sorter;
import java.io.*;
import java.util.ArrayList;
public class Benchmark {
@Param
private String pathToData;
private double[] array;
private double[] arrayCopy;
private Sorter sorter;
@BeforeExperiment
public void setUp() throws IOException {
sorter = new Sorter();
loadData();
arrayCopy = new double[array.length];
}
@com.google.caliper.Benchmark
public void timeSortArray(int reps) {
for (int i = 0; i < reps; i++) {
System.arraycopy(array, 0, arrayCopy, 0, array.length);
sorter.sort(arrayCopy);
}
}
private void loadData() throws IOException {
BufferedReader br = new BufferedReader(new FileReader(pathToData));
ArrayList<Double> doubles = new ArrayList<Double>();
String line;
while ((line = br.readLine()) != null) {
double number;
try {
number = Double.parseDouble(line);
} catch (NumberFormatException ex){
continue;
}
doubles.add(number);
}
br.close();
array = new double[doubles.size()];
for (int i = 0; i < array.length; i++) {
array[i] = doubles.get(i);
}
}
}
package example;
import becnhmark.Benchmark;
import com.google.caliper.runner.CaliperMain;
public class StartBenchmark {
public static void main(String[] args) {
CaliperMain.main(Benchmark.class, args);
}
}
@VladVin
Copy link
Author

VladVin commented Sep 20, 2016

Command line arguments: -DpathToData="<path_to_txt1>[, path_to_txt2, ...]"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment