01: /*
02: * TimeCmd.java
03: *
04: * Copyright (c) 1997 Cornell University.
05: * Copyright (c) 1997 Sun Microsystems, Inc.
06: *
07: * See the file "license.terms" for information on usage and
08: * redistribution of this file, and for a DISCLAIMER OF ALL
09: * WARRANTIES.
10: *
11: * RCS: @(#) $Id: TimeCmd.java,v 1.1.1.1 1998/10/14 21:09:18 cvsadmin Exp $
12: *
13: */
14:
15: package tcl.lang;
16:
17: /**
18: * This class implements the built-in "time" command in Tcl.
19: */
20:
21: class TimeCmd implements Command {
22: /**
23: * See Tcl user documentation for details.
24: */
25:
26: public void cmdProc(Interp interp, TclObject argv[])
27: throws TclException {
28: if ((argv.length < 2) || (argv.length > 3)) {
29: throw new TclNumArgsException(interp, 1, argv,
30: "script ?count?");
31: }
32:
33: int count;
34: if (argv.length == 2) {
35: count = 1;
36: } else {
37: count = TclInteger.get(interp, argv[2]);
38: }
39:
40: long startTime = System.currentTimeMillis();
41: for (int i = 0; i < count; i++) {
42: interp.eval(argv[1], 0);
43: }
44: long endTime = System.currentTimeMillis();
45:
46: int uSecs = (int) (((endTime - startTime) * 1000) / count);
47: if (uSecs == 1) {
48: interp.setResult(TclString
49: .newInstance("1 microsecond per iteration"));
50: } else {
51: interp.setResult(TclString.newInstance(uSecs
52: + " microseconds per iteration"));
53: }
54: }
55: }
|