00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 import java.util.Hashtable;
00031 import jpl.Query;
00032 import jpl.*;
00033
00034 public class Time
00035 {
00036 static int tree_depth = 10;
00037 static int num_tests = 5;
00038 static int num_trials = 10;
00039 static long[][] data = null;
00040 static Term tree = null;
00041 static Atom empty_tree = new Atom("t");
00042 static Timer timer = new Timer();
00043
00044 public static void
00045 main( java.lang.String argv[] )
00046 {
00047 parse_params( argv );
00048
00049 JPL.init();
00050
00051 run_tests();
00052 }
00053
00054 static void
00055 parse_params( java.lang.String argv[] )
00056 {
00057 int i = 0;
00058
00059 while ( i < argv.length ){
00060 if ( argv[i].equals( "-help" ) ){
00061 print_help();
00062 System.exit( 1 );
00063 } else if ( argv[i].equals( "-num_trials" ) ){
00064 num_trials = java.lang.Integer.valueOf( argv[i+1] ).intValue();
00065 i += 2;
00066 } else if ( argv[i].equals( "-tree_depth" ) ){
00067 tree_depth = java.lang.Integer.valueOf( argv[i+1] ).intValue();
00068 i += 2;
00069 } else {
00070 System.err.println( "Unrecognized option: " + argv[i] );
00071 print_help();
00072 System.exit( 1 );
00073 }
00074 }
00075 data = new long[num_tests][num_trials];
00076 }
00077
00078 static void
00079 print_help()
00080 {
00081 System.out.println(
00082 "java Time\n" +
00083 "\t-help print this screen\n" +
00084 "\t-num_trials <num> specify number of trials (default: 10)\n" +
00085 "\t-tree_depth <num> specify depth of binary tree (default: 10)\n" +
00086 "" );
00087 }
00088
00089 static void
00090 run_tests()
00091 {
00092 test_0();
00093 test_1();
00094 test_2();
00095 test_3();
00096 test_4();
00097
00098 print_results();
00099 }
00100
00101 static void
00102 test_0()
00103 {
00104 System.out.print( "test 0..." );
00105 Query query =
00106 new Query("consult('time.pl')");
00107
00108 if ( !query.hasSolution() ){
00109 System.out.println( "consult('time.pl') failed" );
00110 System.exit( 1 );
00111 }
00112 System.out.println( "passed." );
00113
00114 System.out.print( "trees" );
00115 for ( int i = 0; i < num_trials; ++i ){
00116 timer.start();
00117 tree = create_tree();
00118 timer.stop();
00119
00120 data[0][i] = timer.getElapsedTimeInMillis();
00121 System.out.print( "." );
00122 }
00123 System.out.println( "done." );
00124 }
00125
00126 static void
00127 test_1()
00128 {
00129 Query query = new Query( "traverse", new Term[]{tree} );
00130
00131 System.out.print( "traverse" );
00132 for ( int i = 0; i < num_trials; ++i ){
00133
00134 timer.start();
00135 query.hasSolution();
00136 timer.stop();
00137
00138 data[1][i] = timer.getElapsedTimeInMillis();
00139 System.out.print( "." );
00140 }
00141 System.out.println( "done." );
00142 }
00143
00144 static void
00145 test_2()
00146 {
00147 Query query = new Query( "noop", new Term[]{tree} );
00148
00149 System.out.print( "noop" );
00150 for ( int i = 0; i < num_trials; ++i ){
00151
00152 timer.start();
00153 java.util.Hashtable solution = query.oneSolution();
00154 timer.stop();
00155
00156 data[2][i] = timer.getElapsedTimeInMillis();
00157 System.out.print( "." );
00158 }
00159 System.out.println( "done." );
00160 }
00161
00162 static void
00163 test_3()
00164 {
00165 Variable Y = new Variable("Y");
00166 Query query = new Query( "noop_nobind", new Term[]{tree,Y} );
00167
00168 System.out.print( "noop_nobind" );
00169 for ( int i = 0; i < num_trials; ++i ){
00170
00171 timer.start();
00172 boolean ok = query.hasSolution();
00173 timer.stop();
00174
00175 data[3][i] = timer.getElapsedTimeInMillis();
00176 System.out.print( "." );
00177 }
00178 System.out.println( "done." );
00179 }
00180
00181 static void
00182 test_4()
00183 {
00184 Variable Y = new Variable("Y");
00185 Query query = new Query( "noop_bind", new Term[]{tree,Y} );
00186
00187 System.out.print( "noop_bind" );
00188 for ( int i = 0; i < num_trials; ++i ){
00189
00190 timer.start();
00191 java.util.Hashtable solution = query.oneSolution();
00192 timer.stop();
00193
00194 data[4][i] = timer.getElapsedTimeInMillis();
00195 System.out.print( "." );
00196 }
00197 System.out.println( "done." );
00198 }
00199
00200 static java.text.NumberFormat format =
00201 java.text.NumberFormat.getInstance();
00202 static {
00203 format.setMaximumFractionDigits(3);
00204 }
00205
00206
00207 static void
00208 print_results()
00209 {
00210 long num_terms = (long)(Math.pow( 2, tree_depth ) + 1);
00211 System.out.println();
00212 System.out.println( "num_trials: " + num_trials );
00213 System.out.println( "tree_depth: " + tree_depth +
00214 " (= " + num_terms + " terms)" );
00215 System.out.println();
00216
00217 for ( int j = 0; j < num_tests; ++j ){
00218 System.out.print( "test_" + j + "\t\t" );
00219 }
00220 System.out.println( "\n" );
00221 for ( int i = 0; i < num_trials; ++i ){
00222 for ( int j = 0; j < num_tests; ++j ){
00223 System.out.print( data[j][i] + "\t\t" );
00224 }
00225 System.out.println();
00226 }
00227 System.out.println( "\n" );
00228
00229 for ( int j = 0; j < num_tests; ++j ){
00230 System.out.println(
00231 "test_" + j + ": " +
00232 "avg: " + format.format( avg( j ) ) + "ms\t\t" +
00233 format.format( avg( j )/num_terms ) + "ms/term"
00234 );
00235 }
00236 }
00237
00238 static double
00239 avg( int test )
00240 {
00241 long min = java.lang.Long.MAX_VALUE,
00242 max = java.lang.Long.MIN_VALUE;
00243
00244 long sum = 0L;
00245 for ( int i = 0; i < num_trials; ++i ){
00246 sum += data[test][i];
00247 if ( min < data[test][i] ){
00248 min = data[test][i];
00249 }
00250 if ( max > data[test][i] ){
00251 max = data[test][i];
00252 }
00253 }
00254
00255
00256 return sum/(num_trials);
00257 }
00258
00259
00260 static Term
00261 create_tree()
00262 {
00263 return binary_tree( tree_depth );
00264 }
00265
00266 static Term
00267 binary_tree( int depth )
00268 {
00269 if ( depth <= 0 ){
00270 return empty_tree;
00271 } else {
00272 return
00273 new Compound(
00274 "t",
00275 new Term[]{
00276 binary_tree( depth - 1 ),
00277 binary_tree( depth - 1 )
00278 }
00279 );
00280 }
00281 }
00282
00283
00284 static class Timer
00285 {
00286 private java.util.Calendar calendar = java.util.Calendar.getInstance();
00287
00288 private long start_time = 0L,
00289 stop_time = 0L;
00290
00291 boolean running = false;
00292
00293 public
00294 Timer()
00295 {
00296 }
00297
00298 public void
00299 start()
00300 {
00301 if ( !running ){
00302 start_time = getMillis();
00303 running = true;
00304 }
00305 }
00306
00307 private long
00308 getMillis()
00309 {
00310 return System.currentTimeMillis();
00311 }
00312
00313 public void
00314 stop()
00315 {
00316 if ( running ){
00317 stop_time = getMillis();
00318 running = false;
00319 }
00320 }
00321
00322 public long
00323 getElapsedTimeInMillis()
00324 {
00325 if ( running ){
00326 return getMillis() - start_time;
00327 } else {
00328 return stop_time - start_time;
00329 }
00330 }
00331
00332 public double
00333 getElapsedTimeInSeconds()
00334 {
00335 return getElapsedTimeInMillis()/1000;
00336 }
00337 }
00338 }