Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package p1Algoritmia;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.lang.reflect.Method;
- public class AlgorithmsBenchmark {
- public void test0 (String output) {
- FileWriter file = null;
- PrintWriter pw;
- try
- {
- file = new FileWriter(output);
- pw = new PrintWriter(file);
- writestuff(pw);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (file != null)
- try {
- file.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- private void writestuff (PrintWriter pw) {
- pw.println("1, 10");
- pw.println("2, 20");
- pw.println("3, 30");
- pw.println("4, 40");
- }
- //Linear, quadratic, cubic
- public void test1 (String output, int StartN, int EndN) {
- //
- Algorithms alg = new Algorithms();
- long tInicial=0, tFinal=0;
- FileWriter file = null;
- PrintWriter pw;
- //
- try {
- file = new FileWriter(output + "Linear.txt");
- pw = new PrintWriter (file);
- //Linear
- for (int i = StartN; i<=EndN; i++) {
- tInicial = System.currentTimeMillis();
- alg.linear(i);
- tFinal = System.currentTimeMillis();
- pw.println(i + ", " + (tFinal - tInicial));
- }
- file.close();
- //Quadratic
- file = new FileWriter(output + "Quadratic.txt");
- pw = new PrintWriter(file);
- for (int i = StartN; i <= EndN; i++){
- tInicial = System.currentTimeMillis();
- alg.quadratic(i);
- tFinal = System.currentTimeMillis();
- pw.println(i + ", " + (tFinal - tInicial));
- }
- file.close();
- //Cubic
- file = new FileWriter(output + "Cubic.txt");
- pw = new PrintWriter(file);
- for (int i = StartN; i <= EndN; i++){
- tInicial = System.currentTimeMillis();
- alg.cubic(i);
- tFinal = System.currentTimeMillis();
- pw.println(i + ", " + (tFinal - tInicial));
- }
- //
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (file != null)
- try {
- file.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- //HomeWork = tiempo linear, cuadrático y cúbico parámetro 100-200
- // testpow2Iter 0-overflow
- // gráficas en excel y todo eso
- //Potencia 2 iterativo
- public void testpow2 (String output, int StartN, int EndN) {
- Algorithms alg = new Algorithms();
- long tInicial=0, tFinal=0;
- FileWriter file = null;
- PrintWriter pw;
- try
- {
- file = new FileWriter(output);
- pw = new PrintWriter(file);
- for (int i = StartN; i<=EndN; i++) {
- tInicial = System.currentTimeMillis();
- alg.pow2Iter(i);
- tFinal = System.currentTimeMillis();
- pw.println(i + ", " + (tFinal - tInicial));
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (file != null)
- try {
- file.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public long testAlgorithm(String className, String methodName, int n) {
- long tInicial=0, tFinal=0;
- Class<?> cl;
- try {
- cl = Class.forName(className);
- Object o = cl.newInstance();
- Method m = cl.getMethod(methodName, int.class);
- tInicial = System.currentTimeMillis();
- m.invoke(o, n);
- tFinal = System.currentTimeMillis();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return tFinal-tInicial;
- }
- public void test3(String output, int StartN, int EndN, String nombreClase, String nombreMetodo) {
- long tInicial=0, tFinal=0;
- FileWriter file = null;
- PrintWriter pw;
- long total = 0;
- try {
- file = new FileWriter(output);
- pw = new PrintWriter(file);
- for (int i = StartN; i <= EndN; i++) {
- pw.println(i + ", " + testAlgorithm(nombreClase, nombreMetodo, i));
- }
- }catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (file != null)
- try {
- file.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public void testFinal(String output, int StartN, int EndN, int times, String nombreClase, String nombreMetodo) {
- long tInicial=0, tFinal=0;
- FileWriter file = null;
- PrintWriter pw;
- long total = 0;
- try {
- file = new FileWriter(output);
- pw = new PrintWriter(file);
- for (int i = StartN; i <= EndN; i++) {
- for (int j = 0; j < times; j++) {
- total += testAlgorithm(nombreClase, nombreMetodo, i);
- }
- pw.println(i + ", " + total/times);
- total = 0;
- }
- }catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (file != null)
- try {
- file.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement