{"id":703,"date":"2013-07-25T10:47:01","date_gmt":"2013-07-25T17:47:01","guid":{"rendered":"http:\/\/10kdev.ivystreetinc.com\/?p=703"},"modified":"2013-07-25T10:47:18","modified_gmt":"2013-07-25T17:47:18","slug":"fibonacci-memoization","status":"publish","type":"post","link":"http:\/\/10kdev.net\/?p=703","title":{"rendered":"Fibonacci Memoization"},"content":{"rendered":"<p>In this age its important for developers to understand \u00a0the use of \u00a0caching tools and no-sql databases to performance scale your applications.<\/p>\n<p>One such technique is a simple little idea called &#8220;memoization&#8221; &#8212; which is a heuristic of \u00a0remembering the result of a previous calculation in a recursive methodology so the calculation doesn&#8217;t have to be executed again.<\/p>\n<p>Here&#8217;s little recursive Fibonacci algorithm that doesn&#8217;t use memoization (in my case the strategy is a simple HashMap cache). I ran the sequence to 40 levels.<\/p>\n<p><strong>Non-cached<\/strong><\/p>\n<p><strong><em>Run time:\u00a088.60717 seconds<\/em><\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\r\npublic BigInteger fibCalc(int calcLevel) {\r\n     if (calcLevel%10 == 0) {System.out.println(&quot;Calculation Level: &quot; + calcLevel);}\r\n     if (calcLevel == 1) {\r\n          return bi(0);\r\n     } else if (calcLevel == 2 || calcLevel == 3) {;\r\n          return bi(1);\r\n     } else {\r\n          BigInteger rtrn = fibCalc(calcLevel-1).add(fibCalc(calcLevel - 2));\r\n          return rtrn;\r\n     }\r\n}\r\n\r\n<\/pre>\n<p><strong>Cached-Memoization<\/strong><\/p>\n<p><strong><em>Run time:\u00a00.00204 seconds<\/em><\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\r\npublic HashMap&lt;Integer,BigInteger&gt; memoization = new HashMap&lt;Integer,BigInteger&gt;();\r\n\r\npublic BigInteger fibCalc(int calcLevel) {\r\n      if(null != memoization.get(calcLevel)) {\r\n           return memoization.get(calcLevel);\r\n      } else {\r\n      if (calcLevel%10 == 0) {System.out.println(&quot;Calculation Level: &quot; + calcLevel);}\r\n      if (calcLevel == 1) {\r\n           memoization.put(calcLevel,bi(0));\r\n           return bi(0);\r\n      } else if (calcLevel == 2 || calcLevel == 3) {\r\n           memoization.put(calcLevel,bi(1));\r\n           return bi(1);\r\n      } else {\r\n           BigInteger rtrn = fibCalc(calcLevel-1).add(fibCalc(calcLevel - 2));\r\n           memoization.put(calcLevel,rtrn);\r\n           return rtrn;\r\n      }\r\n }\r\n<\/pre>\n<p>Wow! What a difference. From about 89 seconds to *almost nothing*!!!<\/p>\n<p>On different job sites the architects have always investigated larger caching strategies, but this goes to show you a little ole developer can take a programming idea and leverage it for good. \u00a0Of course, it should be a reasonable application . . . .<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this age its important for developers to understand \u00a0the use of \u00a0caching tools and no-sql databases to performance scale your applications. One such technique is a simple little idea called &#8220;memoization&#8221; &#8212; which is a heuristic of \u00a0remembering the result of a previous calculation in a recursive methodology so the calculation doesn&#8217;t have to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/703"}],"collection":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=703"}],"version-history":[{"count":17,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/703\/revisions"}],"predecessor-version":[{"id":720,"href":"http:\/\/10kdev.net\/index.php?rest_route=\/wp\/v2\/posts\/703\/revisions\/720"}],"wp:attachment":[{"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=703"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/10kdev.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}