# How can I calculate the duration of a function call?

I have this function which insert 2 dates in a file:

``````function MaFunction()
put =strftime(\"%c\")
exe 'w'
exe 'call Function1()'
put = strftime(\"%c\")
endfunction
``````

Example:

``````24/03/2020 07:31:22
24/03/2020 07:31:28
``````

I want to know how long Function1 takes to run and display it like:

``````echo "Duration = "+ result + " seconds."
``````

Duration = 6 seconds.

There is the `reltime()` function to get relative times, and `reltimestr()` to display that as the number of seconds:

``````:let start = reltime()
:echo reltime(start)
[65, 11796]
:echo reltimestr(reltime(start))
71.267801
``````

So in your case, you would do something like:

``````function MaFunction()
let start = reltime()
call Function1()
echom printf('Duration = %s seconds', reltimestr(reltime(start)))
endfunction
``````

You can try using `strptime` function, if available, to convert both dates to unix time, substract and get number of seconds. Unfortunately on my win box this func is not available.

Something like this, NOT TESTED:

``````let u_time1 = strptime("%c", "24/03/2020 07:31:22")
let u_time2 =  strptime("%c", "24/03/2020 07:31:28")

echo "Duration = " . (u_time2 - u_time1) . " seconds."
``````

PS, well of course there will be no seconds but milliseconds, so divide it by 1000.

PPS, or not milliseconds :), depends on implementation and I can't check it :(

Another update.

If you will use `localtime()` function instead of `strftime` (and if you don't need human readable dates) then it would be easy (`for` cycle is for testing purposes to get ~4 seconds between `localtime` calls):

``````let u_time1 = localtime()

for x in range(100000)
echo x
endfor

let u_time2 = localtime()

echom "Duration = " . (u_time2 - u_time1) . " seconds."
``````

In my plugin library, I define the following to help running a function n times in order to have an estimation of the time it takes to execute

It's meant to be used this way

``````echo lh#time#bench_n(10000, function('Function1'), the, list, of, parameters)[1] / 10000
``````

`lh#time#bench_n()` returns a list of two elements: the 10000 results of the 10000 function calls (in order to check the function, and the benchmark, execute correctly -- when the parameters are lists, there are some mishaps sometimes and we need to control we are benchmarking something meaningful), and the total execution time.

The becnhmarking functions are defined this way:

``````" # Bench
{{{2
" Function: lh#time#bench(F) {{{3
if exists('*reltimefloat')
function! lh#time#bench(F, ...) abort
let t0 = reltime()
let res = call(a:F, a:000)
let t = reltime()
return [res, reltimefloat(reltime(t0, t))]
endfunction
elseif exists('*reltime')
function! lh#time#bench(F, ...) abort
let t0 = reltime()
let res = call(a:F, a:000)
let t = reltime()
return [res, eval(reltimestr(reltime(t0, t)))]
endfunction
else
function! lh#time#bench(F, ...) abort
let res = call(a:F, a:000)
return [res, 0]
endfunction
endif

" Function: lh#time#bench_n(n, F, ...) {{{3
function! lh#time#bench_n(n, F, ...) abort
let tot = 0
for i in range(1, a:n)
let [res, b] = call('lh#time#bench', [a:F] + deepcopy(a:000))
let tot += b
endfor
return [res, tot]
endfunction
``````