{"id":4868,"date":"2011-12-29T16:40:38","date_gmt":"2011-12-29T11:10:38","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=4868"},"modified":"2011-12-29T16:46:12","modified_gmt":"2011-12-29T11:16:12","slug":"closure-caching-for-increased-performance-memoize","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/closure-caching-for-increased-performance-memoize\/","title":{"rendered":"Closure Caching For Increased Performance (.memoize())"},"content":{"rendered":"<p>Well This is something <b>awesome<\/b> and can increase the performance of the project when used wisely. using the groovy .memoize() we can ask closures to perform kind of caching. This can boost up the performance of the system.<br \/>\n<\/p>\n<h1>&nbsp;<\/h1>\n<h1>_<\/h1>\n<p><strong>Note:<\/strong> This feature is available in groovy 1.8+<\/p>\n<h1>Well Let me Explain this Using Examples<\/h1>\n<p>Now suppose i have a function &#8220;distance&#8221; which calculates distance between two points.<br \/>\nHence for it instead of writing a function(Method) lets write a closure for it.<br \/>\n[java]<br \/>\n        def distance = {x1, y1, x2, y2 -&gt;<br \/>\n            sleep(200)  \/\/just to add a delay for demo purposes<br \/>\n            Math.sqrt((x2 &#8211; x1)**2 + (y2 &#8211; y1)**2)<br \/>\n        }<\/p>\n<p>       \/\/To Call It 5 Times<br \/>\n       5.times {<br \/>\n            def t1 = System.currentTimeMillis()<br \/>\n            println distance(100, 20, 400, 10)<br \/>\n            println &quot;took: ${System.currentTimeMillis() &#8211; t1} milliseconds to execute&quot;<br \/>\n        }<br \/>\n[\/java]<br \/>\nNow if we will Run this the Output will be as follows:<br \/>\n[text]<br \/>\n300.1666203960727<br \/>\ntook: 201 milliseconds to execute<br \/>\n300.1666203960727<br \/>\ntook: 200 milliseconds to execute<br \/>\n300.1666203960727<br \/>\ntook: 201 milliseconds to execute<br \/>\n300.1666203960727<br \/>\ntook: 201 milliseconds to execute<br \/>\n300.1666203960727<br \/>\ntook: 201 milliseconds to execute<br \/>\n[\/text]<\/p>\n<p>Each time this closure is called all statements are executed again and hence re-computation. But if set of inputs are same then technically output should be same. hence a type of caching will help.<br \/>\nAlso if several users are acessing the app then for each user if something is done again and again though the inputs are same then that is again a overhead, this issue can also be solved by <strong>memoizeing<\/strong>. \ud83d\ude42<\/p>\n<h1>Now lets Do it with .memoize()<\/h1>\n<p>[java]<br \/>\n        def distance = {x1, y1, x2, y2 -&gt;<br \/>\n            sleep(200)  \/\/just to add a delay for demo purposes<br \/>\n            Math.sqrt((x2 &#8211; x1)**2 + (y2 &#8211; y1)**2)<br \/>\n        }.memoize() \/\/Note now closure is memoized <\/p>\n<p>       \/\/To Call It 5 Times<br \/>\n       5.times {<br \/>\n            def t1 = System.currentTimeMillis()<br \/>\n            println distance(100, 20, 400, 10)<br \/>\n            println &quot;took: ${System.currentTimeMillis() &#8211; t1}&quot;<br \/>\n        }<br \/>\n[\/java]<br \/>\nNow the result is:<br \/>\n[text]<br \/>\n300.1666203960727<br \/>\ntook: 202 milliseconds to execute<br \/>\n300.1666203960727<br \/>\ntook: 1 milliseconds to execute<br \/>\n300.1666203960727<br \/>\ntook: 1 milliseconds to execute<br \/>\n300.1666203960727<br \/>\ntook: 0 milliseconds to execute<br \/>\n300.1666203960727<br \/>\ntook: 0 milliseconds to execute<br \/>\n[\/text]<br \/>\n<b>We can definitely see the difference between time taken to execute.<\/b><br \/>\nAND HURRAY, ALL REPETITIVE CALLS WITH SAME INPUT DATA ARE NOW CACHED AND HENCE INCREASED PERFORMANCE.<\/p>\n<h1>&nbsp;<\/h1>\n<p>Hope it helped<br \/>\nRegards<br \/>\nKushal Likhi<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Well This is something awesome and can increase the performance of the project when used wisely. using the groovy .memoize() we can ask closures to perform kind of caching. This can boost up the performance of the system. &nbsp; _ Note: This feature is available in groovy 1.8+ Well Let me Explain this Using Examples [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":8},"categories":[7],"tags":[730],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4868"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=4868"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4868\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=4868"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=4868"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=4868"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}