{"id":3903,"date":"2012-10-22T11:01:56","date_gmt":"2012-10-22T10:01:56","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=3903"},"modified":"2012-10-22T11:01:56","modified_gmt":"2012-10-22T10:01:56","slug":"a-puzzle","status":"publish","type":"post","link":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/2012\/10\/22\/a-puzzle\/","title":{"rendered":"A puzzle"},"content":{"rendered":"<p>I am always interested in some of the subtle effects that coding can have on not just the behavior of code, but also its performance. Recently, I stumbled across some benchmarking code &#8211; I cannot recall how I actually found it. It was not written in C, so I translated it.<\/p>\n<p>In the process, I realized that a literal translation might not be ideal &#8230;<!--more--><\/p>\n<p>The code is to do with Mandelbrot sets. My first C translation looked like this:<\/p>\n<pre>int mandelbrot(float a)<\/pre>\n<pre>{<\/pre>\n<pre style=\"padding-left: 30px\">float b;<\/pre>\n<pre style=\"padding-left: 30px\">int iterations = 50;<\/pre>\n<pre style=\"padding-left: 30px\">int i;<\/pre>\n<pre style=\"padding-left: 30px\">b = a;<\/pre>\n<pre style=\"padding-left: 30px\">for (i=1; i&lt;=iterations; i++)<\/pre>\n<pre style=\"padding-left: 30px\">{<\/pre>\n<pre style=\"padding-left: 60px\">if (abs(a) &gt; 2)<\/pre>\n<pre style=\"padding-left: 90px\">return (i - 1);<\/pre>\n<pre style=\"padding-left: 60px\">a = pow(a, 2) + b;<\/pre>\n<pre style=\"padding-left: 30px\">}<\/pre>\n<pre style=\"padding-left: 30px\">return (iterations);<\/pre>\n<pre>}<\/pre>\n<p>&nbsp;<\/p>\n<p>I then saw a couple of optimizations that I could make:<\/p>\n<pre>#define ITERATIONS 50<\/pre>\n<pre>int mandelbrot(float a)<\/pre>\n<pre>{<\/pre>\n<pre style=\"padding-left: 30px\">float b;<\/pre>\n<pre style=\"padding-left: 30px\">int i;<\/pre>\n<pre style=\"padding-left: 30px\">b = a;<\/pre>\n<pre style=\"padding-left: 30px\">for (i=1; i&lt;=ITERATIONS; i++)<\/pre>\n<pre style=\"padding-left: 30px\">{<\/pre>\n<pre style=\"padding-left: 60px\">if (abs(a) &gt; 2)<\/pre>\n<pre style=\"padding-left: 90px\">return (i - 1);<\/pre>\n<pre style=\"padding-left: 60px\">a = a * a + b;<\/pre>\n<pre style=\"padding-left: 30px\">}<\/pre>\n<pre style=\"padding-left: 30px\">return (ITERATIONS);<\/pre>\n<pre>}<\/pre>\n<p>&nbsp;<\/p>\n<p>I made it clear that <strong>iterations<\/strong> was a constant. I could have simply qualified it with <strong>const<\/strong>, but I chose to use <strong>#define<\/strong>. Call me old fashioned if you like. Both would most likely produce the same result. A good compiler may well optimize this without my help.<\/p>\n<p>Since the code was translated from a language that included an exponentiation operator, using the <strong>pow()<\/strong> library function seemed logical, as C has no such operator. As <strong>a<\/strong> is simply squared, multiplication seemed better. Again, a smart compiler may well have addressed this.<\/p>\n<p>I then saw another possible enhancement that might be effective:<\/p>\n<pre>#define ITERATIONS 50<\/pre>\n<pre>int mandelbrot(float a)<\/pre>\n<pre>{<\/pre>\n<pre style=\"padding-left: 30px\">float b;<\/pre>\n<pre style=\"padding-left: 30px\">int i;<\/pre>\n<pre style=\"padding-left: 30px\">b = a;<\/pre>\n<pre style=\"padding-left: 30px\">for (i=1; i&lt;=ITERATIONS; i++)<\/pre>\n<pre style=\"padding-left: 30px\">{<\/pre>\n<pre style=\"padding-left: 60px\">if (abs(b) &gt; 2)<\/pre>\n<pre style=\"padding-left: 90px\">return (i - 1);<\/pre>\n<pre style=\"padding-left: 60px\">b = b * b + a;<\/pre>\n<pre style=\"padding-left: 30px\">}<\/pre>\n<pre style=\"padding-left: 30px\">return (ITERATIONS);<\/pre>\n<pre>}<\/pre>\n<p>&nbsp;<\/p>\n<p>Does this improve matters? If so, why? Please let me know your thoughts by <a href=\"mailto:colin_walls@mentor.com\">email<\/a> or comment. Sorry, no prizes, but I will follow up this posting with my further thoughts sometime soon.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am always interested in some of the subtle effects that coding can have on not just the behavior of&#8230;<\/p>\n","protected":false},"author":71677,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1],"tags":[313,300,327,340],"industry":[],"product":[],"coauthors":[],"class_list":["post-3903","post","type-post","status-publish","format-standard","hentry","category-news","tag-c","tag-embedded-software","tag-optimization","tag-programming-languages"],"_links":{"self":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/3903","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/users\/71677"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/comments?post=3903"}],"version-history":[{"count":0,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/3903\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=3903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=3903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=3903"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=3903"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=3903"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=3903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}