<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Optimization on dev notes</title>
    <link>https://juhanakristianblog.netlify.app/tags/optimization/</link>
    <description>Recent content in Optimization on dev notes</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <lastBuildDate>Fri, 17 Apr 2020 09:33:01 +0000</lastBuildDate>
    <atom:link href="https://juhanakristianblog.netlify.app/tags/optimization/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Memoization in Python</title>
      <link>https://juhanakristianblog.netlify.app/posts/memoization-in-python/</link>
      <pubDate>Fri, 17 Apr 2020 09:33:01 +0000</pubDate>
      <guid>https://juhanakristianblog.netlify.app/posts/memoization-in-python/</guid>
      <description>&lt;p&gt;Memoization is a optimization technique frequently used in functional&#xA;programming. It means storing the result of a computationally intensive function&#xA;call and returing the cached result when the function is called with the same arguments.&lt;/p&gt;&#xA;&lt;p&gt;Python 3.2 introduced the &lt;code&gt;functools.lru_cache&lt;/code&gt; decorator which enables&#xA;memoization of function results in Python.&lt;/p&gt;&#xA;&lt;p&gt;Using &lt;code&gt;lru_cache&lt;/code&gt; is simple as we can see from a example taken from the &lt;a href=&#34;https://docs.python.org/3.8/library/functools.html&#34;&gt;Python documentation&lt;/a&gt;&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#a6e22e&#34;&gt;@lru_cache&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;count_vowels&lt;/span&gt;(sentence):&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    sentence &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; sentence&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;casefold()&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; sum(sentence&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;count(vowel) &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; vowel &lt;span style=&#34;color:#f92672&#34;&gt;in&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#39;aeiou&amp;#39;&lt;/span&gt;)&#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;When calling for ex. &lt;code&gt;count_vowels(&#39;Count vowels in this sentence&#39;)&lt;/code&gt; the result&#xA;is cached and when you call the function with the same argument the cached&#xA;result is returned instead of counting the vowels again. This can be a huge performance boost when dealing with big datasets with potentially similar data.&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
