polectron

Untitled

Nov 4th, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 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 methodName, 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 n = startN; n<=endN; n++){
  47. long start = System.currentTimeMillis();
  48. testAlgorithm(className,methodName,n);
  49. long spent = 0;
  50. for(long i = 0; i<samples; i++){
  51. spent = System.currentTimeMillis() - start;
  52. }
  53. pw.println(spent/samples);
  54. System.out.println((spent) + " milliseconds");
  55. }
  56.  
  57. // for(int i = 0; i<avg.length; i++){
  58. // long average = avg[i]/samples;
  59. // pw.println(average);
  60. // }
  61.  
  62. }catch (Exception e){
  63. e.printStackTrace();
  64. }finally{
  65. try{
  66. if(file!=null){
  67. file.close();
  68. }
  69. }catch (Exception e){
  70. e.printStackTrace();
  71. }
  72. }
  73. }
  74.  
  75. // public static void test(String outputFileName, int startN, int endN){
  76. // FileWriter file = null;
  77. // PrintWriter pw;
  78. // try{
  79. // file = new FileWriter(outputFileName);
  80. // pw = new PrintWriter(file);
  81. // long total = 0;
  82. // long iterations = 0;
  83. // for(long n = startN; n<=endN; n++){
  84. // long start = System.currentTimeMillis();
  85. // Algorithms.logarithmic(n);
  86. // long spent = System.currentTimeMillis() - start;
  87. // pw.println(spent);
  88. // //System.out.println((spent) + " milliseconds");
  89. // }
  90. //
  91. // }catch (Exception e){
  92. // e.printStackTrace();
  93. // }finally{
  94. // try{
  95. // if(file!=null){
  96. // file.close();
  97. // }
  98. // }catch (Exception e){
  99. // e.printStackTrace();
  100. // }
  101. // }
  102. // }
  103.  
  104. public static void doNothing(long i){
  105. System.out.println("Doing nothing at iterarion... ("+i+")");
  106. long endTime = System.currentTimeMillis() + SLEEP_TIME;
  107. while (System.currentTimeMillis() < endTime){
  108. //do nothing
  109. }
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment