Skip to content

Commit 6ce98ee

Browse files
committed
Resumable timers for accurate timing of short running operations
See also xolox/vim-easytags#80 where I suggested to add accurate timing to the vim-easytags plug-in. This change will facilitate that.
1 parent e5b41ce commit 6ce98ee

File tree

4 files changed

+123
-10
lines changed

4 files changed

+123
-10
lines changed

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ from the source code of the miscellaneous scripts using the Python module
3737

3838
<!-- Start of generated documentation -->
3939

40-
The documentation of the 93 functions below was extracted from
41-
19 Vim scripts on July 19, 2014 at 12:32.
40+
The documentation of the 94 functions below was extracted from
41+
19 Vim scripts on July 19, 2014 at 13:11.
4242

4343
### Asynchronous Vim script evaluation
4444

@@ -911,6 +911,33 @@ Test comparison of version strings with `xolox#misc#version#at_least()`.
911911

912912
### Timing of long during operations
913913

914+
#### The `xolox#misc#timer#resumable()` function
915+
916+
Create a resumable timer object. This returns an object (a dictionary with
917+
functions) with the following "methods":
918+
919+
- `start()` instructs the timer object to start counting elapsed time
920+
(when a timer object is created it is not automatically started).
921+
922+
- `stop()` instructs the timer object to stop counting elapsed time.
923+
This adds the time elapsed since `start()` was last called to the
924+
total elapsed time. This method will raise an error if called out of
925+
sequence.
926+
927+
- `format()` takes the total elapsed time and reports it as a string
928+
containing a formatted floating point number.
929+
930+
Timer objects are meant to accurately time short running operations so
931+
they're dependent on Vim's [reltime()][] and [reltimestr()][] functions.
932+
In order to make it possible to use timer objects in my Vim plug-ins
933+
unconditionally there's a fall back to [localtime()][] when [reltime()][]
934+
is not available. In this mode the timer objects are not very useful but
935+
at least they shouldn't raise errors.
936+
937+
[localtime()]: http://vimdoc.sourceforge.net/htmldoc/eval.html#localtime()
938+
[reltime()]: http://vimdoc.sourceforge.net/htmldoc/eval.html#reltime()
939+
[reltimestr()]: http://vimdoc.sourceforge.net/htmldoc/eval.html#reltimestr()
940+
914941
#### The `xolox#misc#timer#start()` function
915942

916943
Start a timer. This returns a list which can later be passed to

autoload/xolox/misc.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
" Last Change: July 19, 2014
55
" URL: http://peterodding.com/code/vim/misc/
66

7-
let g:xolox#misc#version = '1.14.2'
7+
let g:xolox#misc#version = '1.15'

autoload/xolox/misc/timer.vim

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
" Timing of long during operations.
22
"
33
" Author: Peter Odding <[email protected]>
4-
" Last Change: June 21, 2014
4+
" Last Change: July 19, 2014
55
" URL: http://peterodding.com/code/vim/misc/
66

77
if !exists('g:timer_enabled')
@@ -15,6 +15,69 @@ endif
1515
let s:has_reltime = has('reltime')
1616
let s:unique_marker = 'xolox#misc#timer#value'
1717

18+
function! xolox#misc#timer#resumable() " {{{1
19+
" Create a resumable timer object. This returns an object (a dictionary with
20+
" functions) with the following "methods":
21+
"
22+
" - `start()` instructs the timer object to start counting elapsed time
23+
" (when a timer object is created it is not automatically started).
24+
"
25+
" - `stop()` instructs the timer object to stop counting elapsed time.
26+
" This adds the time elapsed since `start()` was last called to the
27+
" total elapsed time. This method will raise an error if called out of
28+
" sequence.
29+
"
30+
" - `format()` takes the total elapsed time and reports it as a string
31+
" containing a formatted floating point number.
32+
"
33+
" Timer objects are meant to accurately time short running operations so
34+
" they're dependent on Vim's [reltime()][] and [reltimestr()][] functions.
35+
" In order to make it possible to use timer objects in my Vim plug-ins
36+
" unconditionally there's a fall back to [localtime()][] when [reltime()][]
37+
" is not available. In this mode the timer objects are not very useful but
38+
" at least they shouldn't raise errors.
39+
"
40+
" [localtime()]: http://vimdoc.sourceforge.net/htmldoc/eval.html#localtime()
41+
" [reltime()]: http://vimdoc.sourceforge.net/htmldoc/eval.html#reltime()
42+
" [reltimestr()]: http://vimdoc.sourceforge.net/htmldoc/eval.html#reltimestr()
43+
let object = {'total': [0, 0]}
44+
function object.start() dict
45+
if s:has_reltime
46+
let self.current = reltime()
47+
else
48+
let self.current = localtime()
49+
endif
50+
endfunction
51+
function object.stop() dict
52+
if empty(get(self, 'current'))
53+
throw "timer.stop() called on a timer that was never started!"
54+
endif
55+
if s:has_reltime
56+
let elapsed_time_string = xolox#misc#str#trim(reltimestr(reltime(self.current)))
57+
" This is a bit silly (converting to a string and then parsing that) but
58+
" the value of reltime() is documented as being platform specific...
59+
let [seconds, microseconds] = split(elapsed_time_string, '\.')
60+
let self.total[0] += substitute(seconds, '^0*', '', '')
61+
let self.total[1] += substitute(microseconds, '^0*', '', '')
62+
let self.current = []
63+
else
64+
let self.total[0] += localtime() - self.current
65+
let self.current = 0
66+
endif
67+
endfunction
68+
function object.format() dict
69+
let seconds = self.total[0]
70+
let microseconds = self.total[1]
71+
if microseconds >= 1000000
72+
let additional_seconds = microseconds / 1000000
73+
let seconds += additional_seconds
74+
let microseconds -= additional_seconds * 1000000
75+
endif
76+
return printf('%i.%06i', seconds, microseconds)
77+
endfunction
78+
return object
79+
endfunction
80+
1881
function! xolox#misc#timer#start() " {{{1
1982
" Start a timer. This returns a list which can later be passed to
2083
" `xolox#misc#timer#stop()`.

doc/misc.txt

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,11 @@ function
113113
20. The |xolox#misc#tests#version_string_parsing()| function
114114
21. The |xolox#misc#tests#version_string_comparison()| function
115115
18. Timing of long during operations |misc-timing-of-long-during-operations|
116-
1. The |xolox#misc#timer#start()| function
117-
2. The |xolox#misc#timer#stop()| function
118-
3. The |xolox#misc#timer#force()| function
119-
4. The |xolox#misc#timer#convert()| function
116+
1. The |xolox#misc#timer#resumable()| function
117+
2. The |xolox#misc#timer#start()| function
118+
3. The |xolox#misc#timer#stop()| function
119+
4. The |xolox#misc#timer#force()| function
120+
5. The |xolox#misc#timer#convert()| function
120121
19. Version string handling |misc-version-string-handling|
121122
1. The |xolox#misc#version#parse()| function
122123
2. The |xolox#misc#version#at_least()| function
@@ -167,8 +168,8 @@ For those who are curious: The function descriptions given below were extracted
167168
from the source code of the miscellaneous scripts using the Python module
168169
'vimdoctool.py' included in vim-tools [5].
169170

170-
The documentation of the 93 functions below was extracted from 19 Vim scripts
171-
on July 19, 2014 at 12:32.
171+
The documentation of the 94 functions below was extracted from 19 Vim scripts
172+
on July 19, 2014 at 13:11.
172173

173174
-------------------------------------------------------------------------------
174175
*misc-asynchronous-vim-script-evaluation*
@@ -1087,6 +1088,28 @@ Test comparison of version strings with |xolox#misc#version#at_least()|.
10871088
*misc-timing-of-long-during-operations*
10881089
Timing of long during operations ~
10891090

1091+
-------------------------------------------------------------------------------
1092+
The *xolox#misc#timer#resumable()* function
1093+
1094+
Create a resumable timer object. This returns an object (a dictionary with
1095+
functions) with the following "methods":
1096+
1097+
- 'start()' instructs the timer object to start counting elapsed time (when a
1098+
timer object is created it is not automatically started).
1099+
1100+
- 'stop()' instructs the timer object to stop counting elapsed time. This
1101+
adds the time elapsed since 'start()' was last called to the total elapsed
1102+
time. This method will raise an error if called out of sequence.
1103+
1104+
- 'format()' takes the total elapsed time and reports it as a string
1105+
containing a formatted floating point number.
1106+
1107+
Timer objects are meant to accurately time short running operations so they're
1108+
dependent on Vim's |reltime()| and |reltimestr()| functions. In order to make
1109+
it possible to use timer objects in my Vim plug-ins unconditionally there's a
1110+
fall back to |localtime()| when |reltime()| is not available. In this mode the
1111+
timer objects are not very useful but at least they shouldn't raise errors.
1112+
10901113
-------------------------------------------------------------------------------
10911114
The *xolox#misc#timer#start()* function
10921115

0 commit comments

Comments
 (0)