Advertisement
polectron

Untitled

Nov 3rd, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. import java.io.FileWriter;
  2. import java.io.PrintWriter;
  3. import java.lang.reflect.Method;
  4.  
  5.  
  6. public class TestBench {
  7.  
  8. public static final int SLEEP_TIME = 2;
  9.  
  10.  
  11. // public static void main(String [ ] args) throws Exception{
  12. // test("linear", 3, 1, 50);
  13. // test("quadratic", 3, 1, 50);
  14. // test("cubic", 3, 1, 20);
  15. // test("logarithmic", 3, 1, 50);
  16. // test("factorial", 3, 1, 50);
  17. // test("factorialRec", 3, 1, 50);
  18. // test("pow", 3, 1, 50);
  19. // test("powRec1", 3, 1, 12);
  20. // test("powRec2", 3, 1, 50);
  21. // test("powRec3", 3, 1, 50);
  22. // test("powRec4", 3, 1, 50);
  23. // }
  24.  
  25. public static void testAlgorithm(String className, String methodName, long n){
  26. try {
  27. Class<?> myClass = Class.forName(className);
  28. Object myClassInstance = (Object) myClass.newInstance();
  29. Method method = myClass.getMethod(methodName, long.class);
  30. method.invoke(myClassInstance, n);
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. }
  35.  
  36. public static void test(String className, String outputFileName, int samples, int startN, int endN){
  37. FileWriter file = null;
  38. PrintWriter pw;
  39. long[] avg = new long[(endN-startN)+2];
  40. for(int i = 0; i<avg.length; i++){
  41. avg[i] = 0;
  42. }
  43. try{
  44. file = new FileWriter(outputFileName+".txt");
  45. pw = new PrintWriter(file);
  46. for(long i = 0; i<samples; i++){
  47. for(long n = startN; n<=endN; n++){
  48. long start = System.currentTimeMillis();
  49. testAlgorithm(className,outputFileName,n);
  50. long spent = System.currentTimeMillis() - start;
  51. avg[(int) n] += spent;
  52. //System.out.println((spent) + " milliseconds");
  53. }
  54. }
  55.  
  56. for(int i = 0; i<avg.length; i++){
  57. long average = avg[i]/samples;
  58. pw.println(average);
  59. }
  60.  
  61. }catch (Exception e){
  62. e.printStackTrace();
  63. }finally{
  64. try{
  65. if(file!=null){
  66. file.close();
  67. }
  68. }catch (Exception e){
  69. e.printStackTrace();
  70. }
  71. }
  72. }
  73.  
  74. // public static void test(String outputFileName, int startN, int endN){
  75. // FileWriter file = null;
  76. // PrintWriter pw;
  77. // try{
  78. // file = new FileWriter(outputFileName);
  79. // pw = new PrintWriter(file);
  80. // long total = 0;
  81. // long iterations = 0;
  82. // for(long n = startN; n<=endN; n++){
  83. // long start = System.currentTimeMillis();
  84. // Algorithms.logarithmic(n);
  85. // long spent = System.currentTimeMillis() - start;
  86. // pw.println(spent);
  87. // //System.out.println((spent) + " milliseconds");
  88. // }
  89. //
  90. // }catch (Exception e){
  91. // e.printStackTrace();
  92. // }finally{
  93. // try{
  94. // if(file!=null){
  95. // file.close();
  96. // }
  97. // }catch (Exception e){
  98. // e.printStackTrace();
  99. // }
  100. // }
  101. // }
  102.  
  103. public static void doNothing(long i){
  104. System.out.println("Doing nothing at iterarion... ("+i+")");
  105. long endTime = System.currentTimeMillis() + SLEEP_TIME;
  106. while (System.currentTimeMillis() < endTime){
  107. //do nothing
  108. }
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement