{"id":1474,"date":"2014-11-05T11:48:42","date_gmt":"2014-11-05T11:48:42","guid":{"rendered":"http:\/\/www.dyalog.com\/blog\/?p=1474"},"modified":"2015-06-22T14:32:53","modified_gmt":"2015-06-22T14:32:53","slug":"a-speed-up-story-2","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2014\/11\/a-speed-up-story-2\/","title":{"rendered":"A Speed-Up Story"},"content":{"rendered":"<p>The first e-mail of the work week came from Nicolas Delcros. He wondered whether anything clever can be done with <code class=\"language-apl\"> \u2218.\u2261 <\/code> on enclosed character strings. I immediately thought of using &#8220;<a title=\"In Praise of Magic Functions: Part I\" href=\"http:\/\/www.dyalog.com\/blog\/2015\/06\/in-praise-of-magic-functions-part-one\/\">magic functions<\/a>&#8220;, an implementation technique whereby interpreter facilities are coded as dfns. I thought of magic functions because the APL expressions involved in this case are particularly terse:<\/p>\n<pre><code class=\"language-apl\">   t \u2190' zero one two three four five six seven eight nine'\r\n   t,\u2190' z\u00e9ro un deux trois quatre cinq six sept huit neuf'\r\n   t,\u2190' zero eins zwei drei vier f\u00fcnf sechs sieben acht neun'\r\n   b\u2190\u233da\u21901\u2193\u00a8(' '=t)\u2282t\r\n\r\n   cmpx 'a\u2218.\u2261a' '\u2218.=\u2368\u2373\u2368a'\r\n  a\u2218.\u2261a   \u2192 9.69E\u00af5 |   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\r\n  \u2218.=\u2368\u2373\u2368a \u2192 8.21E\u00af6 | -92% \u2395\u2395\r\n   cmpx 'a\u2218.\u2261b' '(a\u2373a)\u2218.=(a\u2373b)'\r\n  a\u2218.\u2261b         \u2192 9.70E\u00af5 |   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\r\n  (a\u2373a)\u2218.=(a\u2373b) \u2192 1.43E\u00af5 | -86% \u2395\u2395\u2395\u2395\r\n\r\n   y\u2190\u233dx\u2190300\u2374a\r\n\r\n   cmpx 'x\u2218.\u2261x' '\u2218.=\u2368\u2373\u2368x'\r\n  x\u2218.\u2261x   \u2192 9.53E\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\r\n  \u2218.=\u2368\u2373\u2368x \u2192 1.52E\u00af4 | -99% \u2395\r\n   cmpx 'x\u2218.\u2261y' '(x\u2373x)\u2218.=(x\u2373y)'\r\n  x\u2218.\u2261y         \u2192 9.55E\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\r\n  (x\u2373x)\u2218.=(x\u2373y) \u2192 1.95E\u00af4 | -98% \u2395\r\n<\/code><\/pre>\n<p>The advantage will be even greater if\/when we speed up <code class=\"language-apl\">\u2373<\/code>. (And, obviously, the idea is also applicable to <code class=\"language-apl\">\u2218.\u2262<\/code> : just replace <code class=\"language-apl\">\u2261<\/code> with <code class=\"language-apl\">\u2262<\/code> and <code class=\"language-apl\">=<\/code> with <code class=\"language-apl\">\u2260<\/code> .)<\/p>\n<p>Jay Foad objected that the comparisons above aren&#8217;t quite fair as the more verbose expressions should check that either <code class=\"language-apl\">\u2395ct\u21900<\/code> or that the arguments do not contain any elements subject to tolerant comparison. I countered that the checking in C would not greatly affect the benchmarks as the time to do the checking is <code class=\"language-apl\">O(m+n)<\/code> but the benefits are <code class=\"language-apl\">O(m\u00d7n)<\/code> .<\/p>\n<p>Since the factors are so promising and the coding relatively easy, I went ahead and did the work, with the following results:<\/p>\n<table>\n<tbody>\n<tr>\n<td><\/td>\n<td align=\"center\" valign=\"bottom\"><b>14.1 <\/b><\/td>\n<td align=\"center\" valign=\"bottom\"><b>14.0 <\/b><\/td>\n<td align=\"center\" valign=\"bottom\"><b>ratio <\/b><\/td>\n<td align=\"center\" valign=\"bottom\"><b>cost of checking<\/b><\/td>\n<\/tr>\n<tr>\n<td><code class=\"language-apl\">a\u2218.\u2261a<\/code><\/td>\n<td><code class=\"language-apl\">9.16e\u00af6<\/code><\/td>\n<td align=\"center\"><code class=\"language-apl\">9.69e\u00af5<\/code><\/td>\n<td align=\"right\"><code class=\"language-apl\">10.58<\/code><\/td>\n<td align=\"center\"><code class=\"language-apl\">1.09<\/code><\/td>\n<\/tr>\n<tr>\n<td><code class=\"language-apl\">a\u2218.\u2261b<\/code><\/td>\n<td><code class=\"language-apl\">1.53e\u00af5<\/code><\/td>\n<td><code class=\"language-apl\">9.70e\u00af5<\/code><\/td>\n<td align=\"right\"><code class=\"language-apl\">6.34<\/code><\/td>\n<td align=\"center\"><code class=\"language-apl\">1.08<\/code><\/td>\n<\/tr>\n<tr>\n<td><code class=\"language-apl\">x\u2218.\u2261x<\/code><\/td>\n<td><code class=\"language-apl\">1.48e\u00af4<\/code><\/td>\n<td><code class=\"language-apl\">9.53e\u00af3<\/code><\/td>\n<td align=\"right\"><code class=\"language-apl\">64.39<\/code><\/td>\n<td align=\"center\"><code class=\"language-apl\">1.00<\/code><\/td>\n<\/tr>\n<tr>\n<td><code class=\"language-apl\">x\u2218.\u2261y<\/code><\/td>\n<td><code class=\"language-apl\">1.94e\u00af4<\/code><\/td>\n<td><code class=\"language-apl\">9.55e\u00af3<\/code><\/td>\n<td align=\"right\"><code class=\"language-apl\">49.23<\/code><\/td>\n<td align=\"center\"><code class=\"language-apl\">1.01<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The last column (the cost of checking that tolerant comparison is not involved) is under 10% and decreases as the argument sizes increase.<\/p>\n<p>This work is another illustration of the ubiquity and practical usefulness of the &#8220;selfie&#8221; concept \u2014 in the new (14.1) implementation, <code class=\"language-apl\">x\u2218.\u2261y<\/code> is faster when the left and right arguments are the same than when they are not. In particular, the selfie <code class=\"language-apl\">x\u2373x<\/code> or <code class=\"language-apl\">\u2373\u2368x<\/code> occurs twice, and bolsters the point made in item 3 of <a href=\"http:\/\/www.jsoftware.com\/papers\/amuse-bouches.htm\"><i>Sixteen APL Amuse-Bouches<\/i><\/a>:<\/p>\n<blockquote><p><code class=\"language-apl\">x\u2373x<\/code> are like ID numbers; questions of identity on <code class=\"language-apl\">x<\/code> can often be answered more efficiently on <code class=\"language-apl\">x\u2373x<\/code> than on <code class=\"language-apl\">x<\/code> itself.<\/p><\/blockquote>\n<p>Finally, after all that, I recommend that one should consider using <code class=\"language-apl\">x\u2373y<\/code> before using <code class=\"language-apl\">x\u2218.\u2261y<\/code> . The latter requires <code class=\"language-apl\">O(m\u00d7n)<\/code> space and <code class=\"language-apl\">O(m\u00d7n)<\/code> time, and is inherently inefficient.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The first e-mail of the work week came from Nicolas Delcros. He wondered whether anything clever can be done with \u2218.\u2261 on enclosed character strings. I immediately thought of using &#8220;magic functions&#8220;, an implementation technique whereby interpreter facilities are coded &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2014\/11\/a-speed-up-story-2\/\">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,18],"tags":[],"class_list":["post-1474","post","type-post","status-publish","format-standard","hentry","category-apl-language","category-tips-tricks"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1474","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=1474"}],"version-history":[{"count":6,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1474\/revisions"}],"predecessor-version":[{"id":3080,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1474\/revisions\/3080"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=1474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=1474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=1474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}