Help on Interpreting profiles (while loop vs recursive function)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Help on Interpreting profiles (while loop vs recursive function)

Lifepillar
I wanted to profile the performance of a while loop vs a recursive
function, so I wrote two functions, Iter() and Rec(), computing the
same value (full code at the end of the message).

The execution took ~4s, but Rec()'s measured times were (full profile
at the end of the message):

FUNCTION  Rec()
Called 319100 times
Total time:  55.655449
  Self time:   0.100880

count  total (s)   self (s)
319100              1.070460   return etc...

So, the total time looks wildly wrong. Am I missing something?

Thanks,
Life.

""" START CODE
fun! Iter(n)
   let l:res = a:n
   let l:i = 0
   while (l:res > 2)
     let l:i += 1
     if l:res % 2 == 0
       let l:res = l:res / 2
     else
       let l:res = 3 * l:res + 1
     endif
   endwhile
   return l:i
endf

fun! Rec(n, i)
   return (a:n > 2)
         \ ? (a:n % 2 == 0 ?  Rec(a:n / 2, a:i + 1) : Rec(3 * a:n + 1,
a:i + 1))
         \ : a:i
endf

for i in range(1,100)
   for j in range(3,102)
     call Iter(j)
   endfor
endfor

set maxfuncdepth=200
for i in range(1,100)
   for j in range(3,102)
     call Rec(j,0)
   endfor
endfor
""" END CODE

SCRIPT  recursive.vim
Sourced 1 time
Total time:   3.877530
  Self time:   0.098649

count  total (s)   self (s)
     1              0.000010 fun! Iter(n)
                               let l:res = a:n
                               let l:i = 0
                               while (l:res > 2)
                                 let l:i += 1
                                 if l:res % 2 == 0
                                   let l:res = l:res / 2
                                 else
                                   let l:res = 3 * l:res + 1
                                 endif
                               endwhile
                               return l:i
                             endf

     1              0.000004 fun! Rec(n, i)
                               return (a:n > 2)
                                     \ ? (a:n % 2 == 0 ?  Rec(a:n / 2,
a:i + 1) : Rec(3 * a:n + 1, a:i + 1))
                                     \ : a:i
                             endf

   101              0.000135 for i in range(1,100)
10100              0.008887   for j in range(3,102)
10000   2.205133   0.027498     call Iter(j)
10000              0.007991   endfor
   100              0.000060 endfor

     1              0.000007 set maxfuncdepth=200
   101              0.000126 for i in range(1,100)
10100              0.010572   for j in range(3,102)
10000   1.628922   0.027676     call Rec(j,0)
10000              0.010928   endfor
   100              0.000065 endfor

FUNCTION  Iter()
Called 10000 times
Total time:   2.176493
  Self time:   2.176493

count  total (s)   self (s)
10000              0.011883   let l:res = a:n
10000              0.008844   let l:i = 0
319100              0.304749   while (l:res > 2)
309100              0.260323     let l:i += 1
309100              0.256933     if l:res % 2 == 0
207200              0.221210       let l:res = l:res / 2
207200              0.097171     else
101900              0.140026       let l:res = 3 * l:res + 1
101900              0.057105     endif
309100              0.202117   endwhile
10000              0.008519   return l:i

FUNCTION  Rec()
Called 319100 times
Total time:  55.655449
  Self time:   0.100880

count  total (s)   self (s)
319100              1.070460   return (a:n > 2) ? (a:n % 2 == 0 ?
Rec(a:n / 2, a:i + 1) : Rec(3 * a:n + 1, a:i + 1)) : a:i

FUNCTIONS SORTED ON TOTAL TIME
count  total (s)   self (s)  function
319100  55.655449   0.100880  Rec()
10000   2.176493             Iter()

FUNCTIONS SORTED ON SELF TIME
count  total (s)   self (s)  function
10000              2.176493  Iter()
319100  55.655449   0.100880  Rec()



--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Help on Interpreting profiles (while loop vs recursive function)

Lifepillar
On 16/05/2018 21:09, Lifepillar wrote:

> I wanted to profile the performance of a while loop vs a recursive
> function, so I wrote two functions, Iter() and Rec(), computing the
> same value (full code at the end of the message).
>
> The execution took ~4s, but Rec()'s measured times were (full profile
> at the end of the message):
>
> FUNCTION  Rec()
> Called 319100 times
> Total time:  55.655449
>   Self time:   0.100880
>
> count  total (s)   self (s)
> 319100              1.070460   return etc...
>
> So, the total time looks wildly wrong. Am I missing something?

Well, I missed this from `:help profiling`:

- The "self" time is wrong when a function is used recursively.

So, I guess that none of the numbers above is reliable?

Life.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.