Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.59 KB | None | 0 0
  1. package p1Algoritmia;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.lang.reflect.Method;
  6.  
  7. public class AlgorithmsBenchmark {
  8.  
  9.     public void test0 (String output) {
  10.         FileWriter file = null;
  11.         PrintWriter pw;
  12.  
  13.         try
  14.         {
  15.             file = new FileWriter(output);
  16.             pw = new PrintWriter(file);
  17.  
  18.             writestuff(pw);
  19.  
  20.         } catch (Exception e) {
  21.             e.printStackTrace();
  22.         } finally {
  23.             if (file != null)
  24.                 try {
  25.                     file.close();
  26.                 } catch (IOException e) {
  27.                     e.printStackTrace();
  28.                 }
  29.         }
  30.     }
  31.    
  32.     private void writestuff (PrintWriter pw) {
  33.         pw.println("1, 10");
  34.         pw.println("2, 20");
  35.         pw.println("3, 30");
  36.         pw.println("4, 40");
  37.     }
  38.    
  39.     //Linear, quadratic, cubic
  40.     public void test1 (String output, int StartN,  int EndN) {
  41.         //
  42.         Algorithms alg = new Algorithms();
  43.         long tInicial=0, tFinal=0;
  44.         FileWriter file = null;
  45.         PrintWriter pw;
  46.        
  47.         //
  48.         try {
  49.             file = new FileWriter(output + "Linear.txt");
  50.             pw = new PrintWriter (file);
  51.            
  52.             //Linear
  53.             for (int i = StartN; i<=EndN; i++) {
  54.                 tInicial = System.currentTimeMillis();
  55.                 alg.linear(i);
  56.                 tFinal = System.currentTimeMillis();
  57.                 pw.println(i + ", " + (tFinal - tInicial));            
  58.             }
  59.             file.close();
  60.            
  61.             //Quadratic
  62.             file = new FileWriter(output + "Quadratic.txt");
  63.             pw = new PrintWriter(file);
  64.             for (int i = StartN; i <= EndN; i++){
  65.                 tInicial = System.currentTimeMillis();
  66.                 alg.quadratic(i);
  67.                 tFinal = System.currentTimeMillis();
  68.                 pw.println(i + ", " + (tFinal - tInicial));
  69.             }
  70.             file.close();
  71.            
  72.             //Cubic
  73.             file = new FileWriter(output + "Cubic.txt");
  74.             pw = new PrintWriter(file);
  75.             for (int i = StartN; i <= EndN; i++){
  76.                 tInicial = System.currentTimeMillis();
  77.                 alg.cubic(i);
  78.                 tFinal = System.currentTimeMillis();
  79.                 pw.println(i + ", " + (tFinal - tInicial));
  80.             }
  81. //         
  82.            
  83.         } catch (Exception e) {
  84.             e.printStackTrace();
  85.         } finally {
  86.             if (file != null)
  87.                 try {
  88.                     file.close();
  89.                 } catch (IOException e) {
  90.                     e.printStackTrace();
  91.                 }
  92.         }
  93.        
  94.     }  
  95.    
  96.     //HomeWork = tiempo linear, cuadrático y cúbico parámetro 100-200
  97.     // testpow2Iter 0-overflow
  98.     // gráficas en excel y todo eso
  99.    
  100.     //Potencia 2 iterativo
  101.    
  102.     public void testpow2 (String output, int StartN,  int EndN) {
  103.         Algorithms alg = new Algorithms();
  104.         long tInicial=0, tFinal=0;
  105.         FileWriter file = null;
  106.         PrintWriter pw;
  107.  
  108.         try
  109.         {
  110.             file = new FileWriter(output);
  111.             pw = new PrintWriter(file);
  112.  
  113.             for (int i = StartN; i<=EndN; i++) {
  114.                 tInicial = System.currentTimeMillis();
  115.                 alg.pow2Iter(i);
  116.                 tFinal = System.currentTimeMillis();
  117.                 pw.println(i + ", " + (tFinal - tInicial));
  118.             }
  119.  
  120.         } catch (Exception e) {
  121.             e.printStackTrace();
  122.         } finally {
  123.             if (file != null)
  124.                 try {
  125.                     file.close();
  126.                 } catch (IOException e) {
  127.                     e.printStackTrace();
  128.                 }
  129.         }
  130.     }
  131.    
  132.     public long testAlgorithm(String className, String methodName, int n) {
  133.         long tInicial=0, tFinal=0;
  134.  
  135.         Class<?> cl;
  136.         try {
  137.             cl = Class.forName(className);
  138.             Object o = cl.newInstance();
  139.             Method m = cl.getMethod(methodName, int.class);
  140.  
  141.             tInicial = System.currentTimeMillis();
  142.             m.invoke(o, n);
  143.             tFinal = System.currentTimeMillis();
  144.  
  145.         } catch (Exception e) {
  146.             e.printStackTrace();
  147.         }
  148.         return tFinal-tInicial;
  149.     }
  150.    
  151.     public void test3(String output, int StartN, int EndN, String nombreClase, String nombreMetodo) {
  152.         long tInicial=0, tFinal=0;
  153.         FileWriter file = null;
  154.         PrintWriter pw;
  155.         long total = 0;
  156.        
  157.         try {
  158.             file = new FileWriter(output);
  159.             pw = new PrintWriter(file);
  160.  
  161.             for (int i = StartN; i <= EndN; i++) {
  162.                     pw.println(i + ", " + testAlgorithm(nombreClase, nombreMetodo, i));
  163.             }
  164.         }catch (Exception e) {
  165.             e.printStackTrace();
  166.         } finally {
  167.             if (file != null)
  168.                 try {
  169.                     file.close();
  170.                 } catch (IOException e) {
  171.                     e.printStackTrace();
  172.                 }
  173.         }
  174.     }
  175.    
  176.     public void testFinal(String output, int StartN, int EndN, int times, String nombreClase, String nombreMetodo) {
  177.         long tInicial=0, tFinal=0;
  178.         FileWriter file = null;
  179.         PrintWriter pw;
  180.         long total = 0;
  181.        
  182.         try {
  183.             file = new FileWriter(output);
  184.             pw = new PrintWriter(file);
  185.  
  186.             for (int i = StartN; i <= EndN; i++) {
  187.                 for (int j = 0; j < times; j++) {
  188.                     total += testAlgorithm(nombreClase, nombreMetodo, i);
  189.             }
  190.                 pw.println(i + ", " + total/times);
  191.                 total = 0;
  192.             }
  193.         }catch (Exception e) {
  194.             e.printStackTrace();
  195.         } finally {
  196.             if (file != null)
  197.                 try {
  198.                     file.close();
  199.                 } catch (IOException e) {
  200.                     e.printStackTrace();
  201.                 }
  202.         }
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement