2847
|
1 ## Copyright (C) 1996, 1997 John W. Eaton |
2313
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
901
|
19 |
3301
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} etime (@var{t1}, @var{t2}) |
|
22 ## Return the difference (in seconds) between two time values returned from |
|
23 ## @code{clock}. For example: |
3426
|
24 ## |
3301
|
25 ## @example |
|
26 ## t0 = clock (); |
|
27 ## # many computations later... |
|
28 ## elapsed_time = etime (clock (), t0); |
|
29 ## @end example |
3426
|
30 ## |
3301
|
31 ## @noindent |
|
32 ## will set the variable @code{elapsed_time} to the number of seconds since |
|
33 ## the variable @code{t0} was set. |
|
34 ## @end deftypefn |
3408
|
35 ## @seealso{tic, toc, clock, and cputime} |
901
|
36 |
2314
|
37 ## Author: jwe |
|
38 |
2311
|
39 function secs = etime (t1, t0) |
901
|
40 |
|
41 if (nargin != 2) |
|
42 usage ("etime (t1, t0)"); |
|
43 endif |
|
44 |
|
45 if (is_vector (t1) && length (t1) == 6 && is_vector (t0) && length (t0) == 6) |
|
46 |
1021
|
47 if (t1 (1) != t0 (1)) |
1518
|
48 error ("etime: can't handle timings over year boundaries yet"); |
1021
|
49 endif |
901
|
50 |
2303
|
51 ## XXX FIXME XXX -- could check here to ensure that t1 and t0 really do |
|
52 ## make sense as vectors returned from clock(). |
901
|
53 |
1337
|
54 days_in_months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; |
1021
|
55 |
|
56 if (is_leap_year (t1 (1))) |
|
57 days_in_months (2) = days_in_months (2) + 1; |
|
58 endif |
901
|
59 |
1021
|
60 d1 = sum (days_in_months (1:(t1 (2) - 1))) + t1 (3); |
|
61 d0 = sum (days_in_months (1:(t0 (2) - 1))) + t0 (3); |
|
62 |
|
63 s1 = 86400 * d1 + 3600 * t1 (4) + 60 * t1 (5) + t1 (6); |
|
64 s0 = 86400 * d0 + 3600 * t0 (4) + 60 * t0 (5) + t0 (6); |
|
65 |
|
66 secs = s1 - s0; |
|
67 |
|
68 else |
|
69 error ("etime: args are not 6-element vectors"); |
901
|
70 endif |
|
71 |
|
72 |
|
73 endfunction |