Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileWriter;
- import java.io.PrintWriter;
- import java.lang.reflect.Method;
- public class TestBench {
- public static final int SLEEP_TIME = 2;
- // public static void main(String [ ] args) throws Exception{
- // test("linear", 3, 1, 50);
- // test("quadratic", 3, 1, 50);
- // test("cubic", 3, 1, 20);
- // test("logarithmic", 3, 1, 50);
- // test("factorial", 3, 1, 50);
- // test("factorialRec", 3, 1, 50);
- // test("pow", 3, 1, 50);
- // test("powRec1", 3, 1, 12);
- // test("powRec2", 3, 1, 50);
- // test("powRec3", 3, 1, 50);
- // test("powRec4", 3, 1, 50);
- // }
- public static void testAlgorithm(String className, String methodName, long n){
- try {
- Class<?> myClass = Class.forName(className);
- Object myClassInstance = (Object) myClass.newInstance();
- Method method = myClass.getMethod(methodName, long.class);
- method.invoke(myClassInstance, n);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void test(String className, String outputFileName, int samples, int startN, int endN){
- FileWriter file = null;
- PrintWriter pw;
- long[] avg = new long[(endN-startN)+2];
- for(int i = 0; i<avg.length; i++){
- avg[i] = 0;
- }
- try{
- file = new FileWriter(outputFileName+".txt");
- pw = new PrintWriter(file);
- for(long i = 0; i<samples; i++){
- for(long n = startN; n<=endN; n++){
- long start = System.currentTimeMillis();
- testAlgorithm(className,outputFileName,n);
- long spent = System.currentTimeMillis() - start;
- avg[(int) n] += spent;
- //System.out.println((spent) + " milliseconds");
- }
- }
- for(int i = 0; i<avg.length; i++){
- long average = avg[i]/samples;
- pw.println(average);
- }
- }catch (Exception e){
- e.printStackTrace();
- }finally{
- try{
- if(file!=null){
- file.close();
- }
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- }
- // public static void test(String outputFileName, int startN, int endN){
- // FileWriter file = null;
- // PrintWriter pw;
- // try{
- // file = new FileWriter(outputFileName);
- // pw = new PrintWriter(file);
- // long total = 0;
- // long iterations = 0;
- // for(long n = startN; n<=endN; n++){
- // long start = System.currentTimeMillis();
- // Algorithms.logarithmic(n);
- // long spent = System.currentTimeMillis() - start;
- // pw.println(spent);
- // //System.out.println((spent) + " milliseconds");
- // }
- //
- // }catch (Exception e){
- // e.printStackTrace();
- // }finally{
- // try{
- // if(file!=null){
- // file.close();
- // }
- // }catch (Exception e){
- // e.printStackTrace();
- // }
- // }
- // }
- public static void doNothing(long i){
- System.out.println("Doing nothing at iterarion... ("+i+")");
- long endTime = System.currentTimeMillis() + SLEEP_TIME;
- while (System.currentTimeMillis() < endTime){
- //do nothing
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement