execution times of functions

Learning APL or new to Dyalog? Ask "silly" questions here, without fear...

execution times of functions

Postby Stu on Mon Dec 12, 2016 12:21 am

Is there a simple way to determine the execution time of a function? I found something called "cmpx" on the Web, but it looks quite complicated and I'm not sure it's what I want.
Stu
 
Posts: 97
Joined: Thu Dec 31, 2015 1:30 am

Re: execution times of functions

Postby JohnS|Dyalog on Mon Dec 12, 2016 9:22 am

Hi Stu: operator time from dfns.dws displays the time to apply a function, with a resolution of around a hundredth of a second:
      )copy dfns time
... saved ...

2 + thisfn × 3 thatfn 4 ⍝ monadic and dyadic function calls
42
2 + thisfn time × 3 thatfn time 4 ⍝ same again with time in seconds
0.03
0.45
42
Function cmpx compares the execution times of a number of (typically small) expressions to a high resolution. Here is a comparison of some expressions for the first item of a vector. Their timings vary but each takes 0.2-0.3 microseconds on my machine.
      )copy dfns cmpx
... saved ...

cmpx'⍬⍴3 2' '3 2[1]' '⊣/3 2'
⍬⍴3 2 → 2.2E¯7 | 0% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕
3 2[1] → 3.3E¯7 | +49% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕
⊣/3 2 → 2.1E¯7 | -7% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕
time: http://dfns.dyalog.com/n_time.htm
cmpx: http://dfns.dyalog.com/n_cmpx.htm
JohnS|Dyalog
 

Re: execution times of functions

Postby PGilbert on Mon Dec 12, 2016 12:53 pm

I like to use this dfn that is a variation of time:

Code: Select all
      ∆profile←{
     
     ⍝ James L. Ryan
     ⍝ Taliesin
     ⍝ 73 Thomas Road, P.O. Box 151
     ⍝ Alloway, New Jersey 08001
     ⍝ USA
     
     ⍝ This function provides the means for obtaining a reasonably accurate
     ⍝ processor time figure for all but the most trivial of APL expressions.
     ⍝ The function is called by giving the expression to be tested as a
     ⍝ character vector argument which will be executed in the calling namespace.
     ⍝ The expression will be executed repeatedly until either a thousand
     ⍝ iterations have occurred or until the total accumulated time exceeds
     ⍝ a full second. The expression given in the argument must return a
     ⍝ value. The result is a two element vector, the first element stating
     ⍝ how many times the test expression was executed and the second element
     ⍝ stating the average time per execution.
     
          ⍺←0 0                          ⍝ initial iterations and total
          i t←⍺                          ⍝ current iterations and total
          i+←1                           ⍝ increment iterations
          b←⎕AI ⋄ x←(⎕IO⊃⎕NSI)⍎⍵ ⋄ e←⎕AI ⍝ time the test expression
          t+←⊃-/1↑¨1↓¨e b                ⍝ total accumulated time
          (t>1000)∨i=500:i(t÷i)          ⍝ report if total>1000 or iterations=500
          (i t)∇ ⍵                       ⍝ keep going
      }

      time←{                                  ⍝ Time function application. http://dfns.dyalog.com/n_time.htm
          tfmt←{                              ⍝ elapsed time format hh:mm:ss.cc
              csec←{⌊0.5+0.1×⍵-⍺}             ⍝ elapsed centi-seconds.
              tvec←{0 60 60 100⊤⍵}            ⍝ time vector: hours mins secs centi.
              pair←{(-2⌈⍴⍵)↑'0',⍵}∘⍕          ⍝ 2-digit time unit.
              sepr←{1↓↑,/':::.',¨⍵}           ⍝ ':' or '.' separated pairs.
              trim←{(~{⌽∨\⌽⍵}¯2⌽'00:'⍷⍵)/⍵}   ⍝ leading 00:s removed.
              trim sepr pair¨tvec ⍺ csec ⍵    ⍝ hh:mm:ss.cc
          }
          0::(,↑⍬⍴⎕DM)⎕SIGNAL ⎕EN             ⍝ catchall: pass back error.
          fm←⍬⍴2↓⎕AI                          ⍝ start time.
          6::{}⎕←fm tfmt ⍬⍴2↓⎕AI              ⍝ value error: fn didn't return result.
          ⍺←⊢ ⋄ rslt←⍺ ⍺⍺ ⍵                   ⍝ apply function.
          ⎕←fm tfmt ⍬⍴2↓⎕AI                   ⍝ display elapsed time.
          1:shy←rslt                          ⍝ pass on shy result.
      }


The expression to evaluate is put between quotes and it need to be assigned to a variable. For example:

      ∆profile 'a←⍳1000'
500 0.03


The expression was evaluated 500 times and the average is 0.03 seconds.
User avatar
PGilbert
 
Posts: 401
Joined: Sun Dec 13, 2009 8:46 pm
Location: Montréal, Québec, Canada

Re: execution times of functions

Postby Morten|Dyalog on Mon Dec 12, 2016 1:07 pm

The cmpx function has been embedded into a user command, so it is always available:

Code: Select all
      ]runtime "1E7?1E7"
* Benchmarking "1E7?1E7"
             Exp
 CPU (avg):  813
 Elapsed:    825

You can also compare several expressions:

Code: Select all
      ]runtime "1E6?1E6" "1E7?1E7" -compare
                                                                     
  1E6?1E6 → 3.5E¯2 |     0% ⎕⎕                                       
* 1E7?1E7 → 7.0E¯1 | +1915% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕

See ]?runtime for more details.
User avatar
Morten|Dyalog
 
Posts: 394
Joined: Tue Sep 09, 2008 3:52 pm

Re: execution times of functions

Postby Stu on Wed Dec 14, 2016 1:59 am

Both time and cmpx turn out to be very useful. Thanks guys!
Stu
 
Posts: 97
Joined: Thu Dec 31, 2015 1:30 am


Return to New to Dyalog?

Who is online

Users browsing this forum: No registered users and 1 guest