|
1 | 1 | " Timing of long during operations.
|
2 | 2 | "
|
3 | 3 | " Author: Peter Odding <[email protected]>
|
4 |
| -" Last Change: June 21, 2014 |
| 4 | +" Last Change: July 19, 2014 |
5 | 5 | " URL: http://peterodding.com/code/vim/misc/
|
6 | 6 |
|
7 | 7 | if !exists('g:timer_enabled')
|
|
15 | 15 | let s:has_reltime = has('reltime')
|
16 | 16 | let s:unique_marker = 'xolox#misc#timer#value'
|
17 | 17 |
|
| 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 | + |
18 | 81 | function! xolox#misc#timer#start() " {{{1
|
19 | 82 | " Start a timer. This returns a list which can later be passed to
|
20 | 83 | " `xolox#misc#timer#stop()`.
|
|
0 commit comments