私の教授は、私のプログラムの時間を計り、結果を表示するために、このシェル スクリプトを書きました。何らかの理由で、プログラムで 0 を出力するだけです。彼は次のファイルを提供しました。
timeit.csh
sequence
ecoli2500.txt
ecoli3000.txt
ecoli5000.txt
ecoli7000.txt
ecoli8000.txt
ecoli9000.txt
ecoli10000.txt
シークエンスの内容はこちら
java EditDistance
timeit.csh の内容はさらに下にあります。
java EditDistance < ecoli2500.txt は期待どおりに動作します
実際、プログラムは、シーケンス以外の上記の各ファイルで問題なく実行されます。
私が理解していないのは、その理由です
./timeit.csh sequence
すべてのゼロを生成します
これがtimeit.cshです...(さらに下はEditDistance.javaです):
#!/bin/csh
#
# A Unix script to time programs.
#
# Command line: timeit sequence
# the array of programs from the commandline
set program = $argv[1]
# adjust as needed
set CPULIMIT = 120
limit cpu $CPULIMIT seconds
limit core 0
# input files
set input = ( stx1230.txt \
ecoli2500.txt \
ecoli3000.txt \
ecoli5000.txt \
ecoli7000.txt \
ecoli8000.txt \
ecoli9000.txt \
ecoli10000.txt)
# adjust as needed
set inputpath = `pwd`
# print header
printf "CPU limit = %d seconds\n\n" $CPULIMIT
printf "%-25s" "Data File"
foreach program ($argv)
printf "%16s" $program
end
printf "\n"
# print right number of = for table
@ i = 25 + 16 * $#argv
while ($i > 0)
printf "="
@ i = $i - 1
end
printf "\n"
# time it and print out row for each data file and column for each program
foreach datafile ($input)
printf "%-25s" $datafile
if (-f $inputpath/$datafile) then
foreach program ($argv)
# printing running time of program on datafile
# -p flag with time to ensure its output is measured in seconds and not minutes
nice /usr/bin/time -p $program < \
$inputpath/$datafile |& \
egrep '^user[ ]*[0-9]' | \
awk '{ if ($2 >= '$CPULIMIT') printf " CPU limit"; else printf("%16.2f", $2) }'
# egrep, awk commands extract second column of row corresponding to user time
end
else printf "could not open" $datafile
endif
printf "\n"
end
ここに EditDistance.java があります
import java.util.*;
class EditDistance {
public static int min(int a, int b, int c) {
return Math.min(a,Math.min(b,c));
}
public static int distance(String one, String two) {
if (one.length()>two.length()) {
String temp1 = one;
String temp2 = two;
one = temp2;
two = temp1;
}
int[][] d = new int[one.length()+1][two.length()+1];
d[0][0] = 0;
int top, left, topleft, cost;
for (int i = 1; i <= one.length(); i++) {
d[0][i] = 2*i;
d[i][0] = 2*i;
}
for (int i = 1; i <= one.length(); i++) {
for (int j = 1; j <= two.length(); j++) {
if (one.charAt(i-1) == two.charAt(j-1))
cost = 0;
else
cost = 1;
top = d[i][j-1];
left = d[i-1][j];
topleft = d[i-1][j-1];
d[i][j] = min(top+2,left+2,topleft+cost);
}
}
return d[one.length()][two.length()];
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String one = scanner.next();
String two = scanner.next();
System.out.println(distance(one,two));
}
}
物事がうまくいかない理由はありますか?シェル スクリプトについてはよくわかりませんが、シェル スクリプトのこのセクションは次のとおりです。
nice /usr/bin/time -p $program < \
$inputpath/$datafile |& \
egrep '^user[ ]*[0-9]' | \
awk '{ if ($2 >= '$CPULIMIT') printf " CPU limit"; else printf("%16.2f", $2) }'
私のプログラムがこのコマンドを期待しているはずであることを私の心の中で確認します:
java EditDistance < ecoli2500.txt
java EditDistance...etc. etc.
しかし、プログラムはそれらのコマンドで動作します。シェル スクリプトに正しく応答するようにプログラムをセットアップする必要があります。多分あなたの何人かは助けることができます。