{"id":4041,"date":"2017-04-13T07:51:26","date_gmt":"2017-04-13T07:51:26","guid":{"rendered":"http:\/\/www.dyalog.com\/blog\/?p=4041"},"modified":"2017-04-19T09:20:01","modified_gmt":"2017-04-19T09:20:01","slug":"calculation-v-look-up","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2017\/04\/calculation-v-look-up\/","title":{"rendered":"Calculation v Look-Up"},"content":{"rendered":"<p>(<code class=\" language-apl\">\u2395io\u21900<\/code> and timings are done in Dyalog version 15.0.)<\/p>\n<h2>Table Look-Up<\/h2>\n<p>Some functions can be computed faster by table look-up than a more traditional and more conventional calculation. For example:<\/p>\n<pre><code class=\" language-apl\">      b\u2190?1e6\u23742\r\n\r\n      cmpx '*b' '(*0 1)[b]'\r\n *b        \u2192 1.32E\u00af2 |   0% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\r\n (*0 1)[b] \u2192 1.23E\u00af3 | -91% \u2395\u2395\u2395<\/code><\/pre>\n<p>Some observations about this benchmark:<\/p>\n<p><strong>(a)<\/strong> The advantage of table look-up depends on the time to calculate the function directly, <em>versus<\/em> the time to do indexing, <code class=\" language-apl\">\u237a[\u2375]<\/code> or <code class=\" language-apl\">\u2375\u2337\u237a<\/code>, plus a small amount of time to calculate the function on a (much) smaller representative set.<\/p>\n<p>Indexing is particularly efficient when the indices are boolean:<\/p>\n<pre><code class=\" language-apl\">      bb\u2190b,1\r\n      bi\u2190b,2\r\n      cmpx '2 3[bb]' '2 3 3[bi]'\r\n 2 3[bb]   \u2192 1.12E\u00af4 |    0% \u2395\u2395\u2395\u2395\u2395\r\n 2 3 3[bi] \u2192 7.39E\u00af4 | +558% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395<\/code><\/pre>\n<p><code class=\" language-apl\">bi<\/code> is the same as <code class=\" language-apl\">bb<\/code> except that the last element of <code class=\" language-apl\">2<\/code> forces it to be 1-byte integers (for <code class=\" language-apl\">bi<\/code>) instead of 1-bit booleans (for <code class=\" language-apl\">bb<\/code>).<\/p>\n<p><strong>(b)<\/strong> Although the faster expression works best with 0-origin, the timing for 1-origin is similar.<\/p>\n<pre><code class=\" language-apl\">      cmpx '*b' '{\u2395io\u21900 \u22c4 (*0 1)[\u2375]}b'\r\n *b                   \u2192 1.32E\u00af2 |   0% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\r\n {\u2395io\u21900 \u22c4 (*0 1)[\u2375]}b \u2192 1.22E\u00af3 | -91% \u2395\u2395\u2395<\/code><\/pre>\n<p>That is, if the current value of <code class=\" language-apl\">\u2395io<\/code> is 1, the implementation can use a local <code class=\" language-apl\">\u2395io<\/code> setting of 0.<\/p>\n<p><strong>(c)<\/strong> The fact that <code class=\" language-apl\">*b<\/code> is currently slower than <code class=\" language-apl\">(*0 1)[b]<\/code> represents a judgment by the implementers that <code class=\" language-apl\">*b<\/code> is not a sufficiently common computation to warrant writing faster code for it. Other similar functions already embody the table look-up technique:<\/p>\n<pre><code class=\" language-apl\">      cmpx '\u2355b' '\u00af1\u2193,(2 2\u2374''0 1 '')[b;]'\r\n \u2355b                   \u2192 6.95E\u00af4 |  0% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\r\n \u00af1\u2193,(2 2\u2374'0 1 ')[b;] \u2192 6.92E\u00af4 | -1% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395<\/code><\/pre>\n<h2>Small Domains<\/h2>\n<p>Table look-up works best for booleans, but it also works for other small domains such as 1-byte integers:<\/p>\n<pre><code class=\" language-apl\">      i1\u2190?1e6\u2374128    \u235d 1-byte integers\r\n\r\n      cmpx '*i1' '(*\u2373128)[i1]'\r\n *i1         \u2192 1.48E\u00af2 |   0% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\r\n (*\u2373128)[i1] \u2192 9.30E\u00af4 | -94% \u2395\u2395\r\n\r\n      cmpx '\u25cbi1' '(\u25cb\u2373128)[i1]'\r\n \u25cbi1         \u2192 2.78E\u00af3 |   0% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\r\n (\u25cb\u2373128)[i1] \u2192 9.25E\u00af4 | -67% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395<\/code><\/pre>\n<p>In general, a table for 1-byte integers needs to have values for <code class=\" language-apl\">\u00af128+\u2373256<\/code>. Here <code class=\" language-apl\">\u2373128<\/code> is used to simulate that in C indexing can be done on 1-byte indices without extra computation on the indices. In APL, general 1-byte indices are used as <code class=\" language-apl\">table[i1+128]<\/code>; in C, the expression is <code class=\" language-apl\">(table+offset)[i]<\/code>.<\/p>\n<p>Domains considered &#8220;small&#8221; get bigger all the time as machines come equipped with ever larger memories.<\/p>\n<h2>Larger Domains<\/h2>\n<p>Table look-up is applicable when the argument is not a substantial subset of a large domain and there is a fast way to detect that it is not.<\/p>\n<pre><code class=\" language-apl\">      i4s\u21901e7+?1e6\u23741e5    \u235d small-range 4-byte integers\r\n\r\n      G\u2190{(\u2282u\u2373\u2375)\u2337\u237a\u237a u\u2190\u222a\u2375}\r\n\r\n      cmpx '\u235fi4s' '\u235fG i4s'\r\n \u235fi4s   \u2192 1.44E\u00af2 |   0% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\r\n \u235fG i4s \u2192 9.59E\u00af3 | -34% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395<\/code><\/pre>\n<p>The definition of <code class=\" language-apl\">G<\/code> implements that the argument is not used directly as indices (as in <code class=\" language-apl\">(\u2282\u2375)\u2337\u237a\u237a u<\/code>) but needed to be looked up, the <code class=\" language-apl\">u\u2373\u2375<\/code> in <code class=\" language-apl\">(\u2282u\u2373\u2375)\u2337\u237a\u237a u<\/code>. Thus both index-of and indexing are key enablers for making table look-up competitive.<\/p>\n<p>\u00a74.3 of <em><a href=\"http:\/\/www.jsoftware.com\/papers\/tot.htm\" target=\"_blank\">Notation as a Tool of Thought<\/a><\/em> presents a different way to distribute the results of a calculation on the &#8220;nub&#8221; (unique items). But it takes more time and space and is limited to numeric scalar results.<\/p>\n<pre><code class=\" language-apl\">      H\u2190{(\u237a\u237a u)+.\u00d7(u\u2190\u222a\u2375)\u2218.=\u2375}\r\n\r\n      i4a\u21901e7+?1e4\u23741e3    \u235d small-range 4-byte integers\r\n\r\n      cmpx '\u235fi4a' '\u235fG i4a' '\u235fH i4a'\r\n \u235fi4a   \u2192 1.56E\u00af4 |       0%\r\n \u235fG i4a \u2192 6.25E\u00af5 |     -60%\r\n \u235fH i4a \u2192 1.78E\u00af1 | +113500% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395<\/code><\/pre>\n<p>The benchmark is done on the smaller <code class=\" language-apl\">i4a<\/code> as a benchmark on <code class=\" language-apl\">i4s<\/code> founders on the <code class=\" language-apl\">\u2262\u222a\u2375<\/code> by <code class=\" language-apl\">\u2262\u2375<\/code> boolean matrix (for <code class=\" language-apl\">i4s<\/code>, 12.5 GB = <code class=\" language-apl\">\u00d7\/ 0.125 1e5 1e6<\/code>) created by <code class=\" language-apl\">H<\/code>.<\/p>\n<h2>Mathematical Background<\/h2>\n<p>&#8220;<a href=\"https:\/\/en.wikipedia.org\/wiki\/New_Math\" target=\"_blank\">New Math<\/a>&#8221; teaches that a function is a set of ordered pairs whose first items are unique:<\/p>\n<p><code class=\" language-apl\">*\u2375: {(\u2375,*\u2375)|\u2375\u220a<em>C<\/em>}\u00a0\u00a0\u00a0\u00a0\u00a0<\/code> The set of all <code class=\" language-apl\">(\u2375,*\u2375)<\/code> where <code class=\" language-apl\">\u2375<\/code> is a complex number<br \/>\n<code class=\" language-apl\">\u235f\u2375: {(\u2375,\u235f\u2375)|\u2375\u220a<em>C<\/em>~{0}}\u00a0<\/code> The set of all <code class=\" language-apl\">(\u2375,\u235f\u2375)<\/code> where <code class=\" language-apl\">\u2375<\/code> is a non-zero complex number<br \/>\n<code class=\" language-apl\">\u25cb\u2375: {(\u2375,\u25cb\u2375)|\u2375\u220a<em>C<\/em>}\u00a0\u00a0\u00a0\u00a0\u00a0<\/code> The set of all <code class=\" language-apl\">(\u2375,\u25cb\u2375)<\/code> where <code class=\" language-apl\">\u2375<\/code> is a complex number<br \/>\n<code class=\" language-apl\">\u2355\u2375: {(\u2375,\u2355\u2375)|0=\u2374\u2374\u2375}\u00a0\u00a0\u00a0<\/code> The set of all <code class=\" language-apl\">(\u2375,\u2355\u2375)<\/code> where <code class=\" language-apl\">\u2375<\/code> is a scalar<\/p>\n<p>When <code class=\" language-apl\">\u2375<\/code> is restricted (for example) to the boolean domain, the functions, the sets of ordered pairs, are more simply represented by enumerating all the possibilities:<\/p>\n<p><code class=\" language-apl\">*\u2375: {(0,1), (1,2.71828)}<br \/>\n\u235f\u2375: {(0,Err), (1,0)}<br \/>\n\u25cb\u2375: {(0,0), (1,3.14159)}<br \/>\n\u2355\u2375: {(0,'0'), (1,'1')}<\/code><\/p>\n<h2>Realized and Potential Performance Improvements<\/h2>\n<p><strong>realized<\/strong> (available no later than 15.0)<\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\" width=\"200px\">boolean<br \/>\n1-byte integer<\/td>\n<td style=\"text-align: left;\"><code class=\" language-apl\">\u2355 \u2218.f<\/code><br \/>\n<code class=\" language-apl\">\u2355\u00a0\u00a0\u00a0\u00a0 a\u2218\u2355<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>potential<\/strong> (non-exhaustive list)<\/p>\n<table>\n<tbody>\n<tr>\n<td style=\"text-align: left;\" width=\"200px\">boolean<br \/>\n1-byte integer<br \/>\n2-byte integer<br \/>\nsmall-range 4-byte integer<\/td>\n<td style=\"text-align: left;\"><code class=\" language-apl\">* \u235f | - \u25cb\u00a0\u00a0 ! \u2373 a\u2218f f.g<\/code><br \/>\n<code class=\" language-apl\">* \u235f | - \u25cb \u00d7 ! \u2373 a\u2218f f.g \u2218.f<\/code><br \/>\n<code class=\" language-apl\">* \u235f | - \u25cb \u00d7\u00a0\u00a0 \u2373 a\u2218f<\/code><br \/>\n<code class=\" language-apl\">* \u235f |\u00a0\u00a0\u00a0\u00a0 \u00d7<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><code class=\" language-apl\">a<\/code> is a scalar; <code class=\" language-apl\">f<\/code> and <code class=\" language-apl\">g<\/code> are primitive scalar dyadic functions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(\u2395io\u21900 and timings are done in Dyalog version 15.0.) Table Look-Up Some functions can be computed faster by table look-up than a more traditional and more conventional calculation. For example: b\u2190?1e6\u23742 cmpx &#8216;*b&#8217; &#8216;(*0 1)[b]&#8217; *b \u2192 1.32E\u00af2 | 0% &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2017\/04\/calculation-v-look-up\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-4041","post","type-post","status-publish","format-standard","hentry","category-apl-language"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/4041","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/comments?post=4041"}],"version-history":[{"count":10,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/4041\/revisions"}],"predecessor-version":[{"id":4906,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/4041\/revisions\/4906"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=4041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=4041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=4041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}