<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>optimize | Learn C Games Programming Blog</title>
	<atom:link href="https://learncgames.com/tag/optimize/feed/" rel="self" type="application/rss+xml" />
	<link>https://learncgames.com</link>
	<description>A blog about C, programming games and my ebook(s).</description>
	<lastBuildDate>Fri, 26 Feb 2021 20:49:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://learncgames.com/wp-content/uploads/2020/03/cropped-favicon-32x32.png</url>
	<title>optimize | Learn C Games Programming Blog</title>
	<link>https://learncgames.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">181446779</site>	<item>
		<title>Simple but effective optimisation in C</title>
		<link>https://learncgames.com/simple-but-effective-optimisation-in-c/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=simple-but-effective-optimisation-in-c</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Mon, 01 Mar 2021 00:00:30 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[optimize]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=2795</guid>

					<description><![CDATA[<p>Here&#8217;s a short program. It repeats a loop twice, indexing through a text string a million times adding up the value. Instead of starting the total at 0, I set it to the outer loop index, to try and reduce the scope for immediate optimisation. #include&#60;stdio.h&#62; #include &#60;string.h&#62; #include &#34;hr_time.h&#34; stopWatch s; char* testString = [&#8230;]</p>
The post <a href="https://learncgames.com/simple-but-effective-optimisation-in-c/">Simple but effective optimisation in C</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<p><img fetchpriority="high" decoding="async" class="alignleft size-medium wp-image-2797" src="https://learncgames.com/wp-content/uploads/2021/03/time-731110_640-300x200.jpg" alt="Stopwatch" width="300" height="200" srcset="https://learncgames.com/wp-content/uploads/2021/03/time-731110_640-300x200.jpg 300w, https://learncgames.com/wp-content/uploads/2021/03/time-731110_640.jpg 640w" sizes="(max-width: 300px) 100vw, 300px" />Here&#8217;s a short program. It repeats a loop twice, indexing through a text string a million times adding up the value. Instead of starting the total at 0, I set it to the outer loop index, to try and reduce the scope for immediate optimisation.</p>
<pre><code class="language-c" data-line="">#include&lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &quot;hr_time.h&quot;

stopWatch s;

char* testString = &quot;This is a rather long string just to prove a point&quot;;
int main()
{
    int total = 0;
    startTimer(&amp;s);
    for (int i = 0; i &lt; 1000000; i++) {
        total = i;
        for (int index = 0; index &lt; (int)strlen(testString); index++) {
            total += testString[index];
        }
    }
    stopTimer(&amp;s);
    printf(&quot;Total =%d Took %8.5f\n&quot;, total, getElapsedTime(&amp;s));

    startTimer(&amp;s);
    int len = (int)strlen(testString);
    for (int i = 0; i &lt; 1000000; i++) {
        total = i;
        for (int index = 0; index &lt; len; index++) {
            total += testString[index];
        }
    }
    stopTimer(&amp;s);
    printf(&quot;Total =%d Took %8.5f\n&quot;,total, getElapsedTime(&amp;s));
    return 0;
}
</code></pre>
<p>I compiled and ran it twice, once in Debug and once in Release mode on Windows using MSVC.</p>
<p>Debug:</p>
<pre>Total =1004673 Took 0.55710
Total =1004673 Took 0.11465</pre>
<p>Release</p>
<pre>Total =1004673 Took  0.00762
Total =1004673 Took  0.00765
</pre>
<p>Clearly in Release compilation, the compiler is smart enough to realise that it can optimise <strong>strlen(testString)</strong> away so there&#8217;s no difference between the two times. But in debug it&#8217;s clear that calling <strong>strlen()</strong> inside a for loop is relatively slow.</p>
<p>I compiled it and ran it with clang on Ubuntu 20.04 in the Hyper-V VM. The times with default optimization were</p>
<pre>0.18370 
0.10644</pre>
<p>and with &#8220;-O3&#8221; added to the compile line for maximum optimisation,. this changed to</p>
<pre>0.0762
0.0745</pre>
<p>which is almost identical to the Windows release compile times.</p>The post <a href="https://learncgames.com/simple-but-effective-optimisation-in-c/">Simple but effective optimisation in C</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2795</post-id>	</item>
	</channel>
</rss>
