views:

58

answers:

3

Hi,

I'd like to test which of two implementation in java of a problem is the fastest. I've 2 jar files with the two implementation I can execute from the terminal. I want to execute both about 100 times and analyse which one is the fastest to do that or that task. In the output one of the line is "executing time : xx", I need to catch this xx to put in an array or something like that to analyse it later

While I'm executing the jar, I've also to give some input commands (like a name to search or a number).

I don't with which language is it the easiest to do it. I know the basis in Bash and Python

thank you

+1  A: 

Excuse me, but Why you dont make a jar that call n-times any jar?

For Example:

public static void main(String[] args) {
    for(int i=0;i<1000;i++) {
        params1 = randomNumber();
        params2 = randomName();
        ...
        paramsN = randomTime();
        MYJAR1 test1 = new MYJAR1(params1,params2,...,paramsN);
        timerStart();
        test1.start();
        timerEnd();
        printTimer();
    }
}

and make the same for the second jar.

I hope that my idea can help you.

Bye

tommaso
I'm using linux but the problem is that I want to analyse not the execution of all the program but the different parts.Which of the two is fastest to : construct the data structure, search an element, add an element, delete and element,...When I execute the jar, I've to give some commands to say what I want to do and it produce output at each time.
Martin Trigaux
The above will mean that static initialisers etc. will only run once. If they are time significant and contribute to the stats, then they won't be what the OP wants.
Brian Agnew
You can profile the jar, for example, with netbeans profiler and show how time spent any single method.
tommaso
A: 

If you use (say) Perl, you can spawn the process off, capture the output via a redirection and filter the times. e.g.

if (open(PROCESS, "myproc |")) {
   while(<PROCESS>) {
     if (/executing time : (\d+)/) {
       # $1 has the time now
     }
   }
}

(not compiled or tested!)

Perhaps the simplest way of analysing the data is to redirect the above output to a file, and then load it into Excel. You can then use Excel to calculate averages/max/mins and std. devs (if you wish) trivially.

Brian Agnew
I don't know Perl but I guess it can works.I can also edit the jar file to have only the time as output.But I've still the problem how can I say : execute the jar, send 1 as input, send "something" as input, send "quit" and put the output in a file for exemple to analyse later (with excel for exemple)
Martin Trigaux
A: 

Ok I've found with 3 different scripts ^^

in the java code, for each function :

long time1 = System.currentTimeMillis();
// some code for function 1
long time2 = System.currentTimeMillis();

try {
    OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(new File("log.txt"),true));
    osw.write("function1,"+(time2 - time1)+"\n");
    osw.close();
} catch (FileNotFoundException ex) {
    ex.printStackTrace();
} catch (IOException ex) {
    ex.printStackTrace();
}

a bash code to run the 500 times the two algorithms

#!/bin/bash

i=0
while [ $i -lt 500 ]
do
    ./algo1.ex
    ./algo2.ex
    i=$(( $i+1 ))
done

an (or two actually, one for each algorithm) expect code to do the command during the execution

#!/usr/bin/expect -f 

spawn java -jar algo1.jar

expect "enter your choice" {send "1\n"}
expect "enter a name :" {send "Peju, M.\n"}
expect "enter your choice" {send "2\n"}
expect "enter a name :" {send "Creasy, R. J.\n"}
expect "enter your choice" {send "0\n"}

exit

As I didn't know how to do it in bash, to count I used a python code

#!/usr/bin/python
# -*- coding: utf8 -*-

import sys

if __name__ == "__main__":
    if sys.argv[1:]:
     arg = sys.argv[1]
     filin = open(arg, 'r')

     line = filin.readline()

     res= 0, 0, 0
     int n = 1
     while line !='':
      t = line.partition(',')

      if t[0] == "function1":
       res = (res[0] + int(t[2]), res[1], res[2])
      if t[0] == "function2":
       res = (res[0], res[1] + int(t[2]), res[2])
      if t[0] == "function3":
       res = (res[0], res[1], res[2] + int(t[2]))

      ligne = filin.readline()
         n = n+1
     print res
     print (res[0]/(n/3.0), res[1]/(n/3.0), res[2]/(n/3.0))

     filin.close()

and it works but thanks for your propositions

Martin Trigaux