<?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>timing | Learn C Games Programming Blog</title>
	<atom:link href="https://learncgames.com/tag/timing/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>Sun, 26 Mar 2023 08:51:51 +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>timing | 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>New Linux Timing Code</title>
		<link>https://learncgames.com/new-linux-timing-code/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=new-linux-timing-code</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Sun, 26 Mar 2023 00:00:22 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Clang]]></category>
		<category><![CDATA[Techniques]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[timing]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=3221</guid>

					<description><![CDATA[<p>For some time, I had a problem with my old Linux timing code. The units hr_time.h/c had to have the includes arranged in the right order and some flags set for it to compile. Plus it never seemed right having to do this in hr_time.h #include &#60;linux/time.h&#62; #define __timespec_defined 1 #define __itimerspec_defined 1 #define __timeval_defined [&#8230;]</p>
The post <a href="https://learncgames.com/new-linux-timing-code/">New Linux Timing Code</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:30,&quot;href&quot;:&quot;https:\/\/solarianprogrammer.com\/2019\/04\/17\/c17-programming-measuring-execution-time-delaying-program&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20251202052526\/https:\/\/solarianprogrammer.com\/2019\/04\/17\/c17-programming-measuring-execution-time-delaying-program\/&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 11:04:01&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-11 21:41:38&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-20 02:02:02&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-23 20:04:04&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-27 11:01:33&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-02 22:29:45&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-07 18:00:40&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-12 00:00:17&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-15 12:32:54&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-21 20:48:50&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-26 05:21:13&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-02 18:33:35&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-08 11:16:27&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-11 20:05:36&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-15 09:28:22&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-20 20:18:19&quot;,&quot;http_code&quot;:206}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-20 20:18:19&quot;,&quot;http_code&quot;:206},&quot;process&quot;:&quot;done&quot;}]'></div>
<p><img fetchpriority="high" decoding="async" class="alignleft wp-image-3223 size-medium" src="https://learncgames.com/wp-content/uploads/2023/03/2yj6mbvj0sg-300x169.jpg" alt="silver and white round analog watch" width="300" height="169" srcset="https://learncgames.com/wp-content/uploads/2023/03/2yj6mbvj0sg-300x169.jpg 300w, https://learncgames.com/wp-content/uploads/2023/03/2yj6mbvj0sg-1024x576.jpg 1024w, https://learncgames.com/wp-content/uploads/2023/03/2yj6mbvj0sg-768x432.jpg 768w, https://learncgames.com/wp-content/uploads/2023/03/2yj6mbvj0sg-1536x864.jpg 1536w, https://learncgames.com/wp-content/uploads/2023/03/2yj6mbvj0sg.jpg 1600w" sizes="(max-width: 300px) 100vw, 300px" /></p>
<p>For some time, I had a problem with my old Linux timing code. The units hr_time.h/c had to have the includes arranged in the right order and some flags set for it to compile. Plus it never seemed right having to do this in hr_time.h</p>
<pre><code class="language-c" data-line="">#include &lt;linux/time.h&gt;
#define __timespec_defined 1
#define __itimerspec_defined 1
#define __timeval_defined 1
</code></pre>
<p>But today I found some better code on this <a title="Link to entry on Solarianprogrammer.com" href="https://solarianprogrammer.com/2019/04/17/c17-programming-measuring-execution-time-delaying-program/" target="_blank" rel="nofollow noopener">Solarian</a>Programmer website and I&#8217;m always happy to give credit where it&#8217;s due. I&#8217;ve witched to using C17 which clang on Ubuntu (clang 14) and Raspberry Pi (clang 11) both seem to accept.</p>
<p>So now my hr_time.h looks like this:</p>
<p>hr_time.h</p>
<pre><code class="language-c" data-line="">#pragma once
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;

void startTimer();
void stopTimer();
double diff();</code></pre>
<p>hr_time.c</p>
<pre><code class="language-c" data-line="">#include &quot;hr_time.h&quot;
#include &lt;time.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;

struct timespec start;
struct timespec end;

void startTimer() {
	if (timespec_get(&amp;start, TIME_UTC) != TIME_UTC) {
		printf(&quot;Error in calling timespec_get\n&quot;);
		exit(EXIT_FAILURE);
	}
};

void stopTimer() {
	if (timespec_get(&amp;end, TIME_UTC) != TIME_UTC) {
		printf(&quot;Error in calling timespec_get\n&quot;);
		exit(EXIT_FAILURE);
	}
};

double diff()
{
	return  (double)(end.tv_sec - start.tv_sec) + 
			((double)(end.tv_nsec - start.tv_nsec) / 1000000000L);
}</code></pre>
<p>I&#8217;m only ever using one timer, if you want to modify this to pass in a timer in the startTimer and stopTimer then it&#8217;s easy enough.</p>The post <a href="https://learncgames.com/new-linux-timing-code/">New Linux Timing Code</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">3221</post-id>	</item>
		<item>
		<title>Timing of SDL vs TTF fonts</title>
		<link>https://learncgames.com/timing-of-sdl-vs-ttf-fonts/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=timing-of-sdl-vs-ttf-fonts</link>
					<comments>https://learncgames.com/timing-of-sdl-vs-ttf-fonts/#comments</comments>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Sat, 05 Dec 2020 00:00:28 +0000</pubDate>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Source code]]></category>
		<category><![CDATA[timing]]></category>
		<category><![CDATA[ttf.sdl]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=1975</guid>

					<description><![CDATA[<p>So I got round to writing that SDL vs TTF timing comparison. However I had to make a few assumptions. The program was written for Windows but easily converts to Linux (apart from the timing code but I did that back in March for Linux) is about 250 lines long. I&#8217;ve uploaded it to GitHub, [&#8230;]</p>
The post <a href="https://learncgames.com/timing-of-sdl-vs-ttf-fonts/">Timing of SDL vs TTF fonts</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:13,&quot;href&quot;:&quot;https:\/\/github.com\/David-H-Bolton\/LearnC&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20250807013721\/https:\/\/github.com\/David-H-Bolton\/LearnC&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 10:35:09&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-09 16:40:16&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-12 18:24:15&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-15 22:16:30&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-18 22:19:31&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-21 22:22:05&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-25 04:17:25&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-28 04:17:49&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-03 04:22:25&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-06 04:38:11&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-09 08:48:04&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-12 09:17:10&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-15 10:18:23&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-18 12:43:50&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-21 15:14:39&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-24 15:19:12&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-27 16:38:15&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-30 19:05:39&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-02 22:49:21&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-06 04:18:17&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-09 04:19:40&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-12 09:53:38&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-15 10:17:36&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-18 13:35:54&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-04-21 15:50:44&quot;,&quot;http_code&quot;:206}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-21 15:50:44&quot;,&quot;http_code&quot;:206},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:342,&quot;href&quot;:&quot;https:\/\/www.libsdl.org\/projects\/SDL_ttf&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20260203013147\/https:\/\/www.libsdl.org\/projects\/SDL_ttf\/&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 14:11:57&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-10 08:45:18&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-13 09:27:43&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-17 20:03:09&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-21 12:06:06&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-24 15:37:57&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-28 04:34:10&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-03 17:50:08&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-07 07:52:55&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-10 21:40:32&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-13 22:50:50&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-17 06:25:46&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-21 16:36:02&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-24 19:27:42&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-27 21:41:41&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-31 01:44:44&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-04 13:20:07&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-08 00:37:24&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-13 06:01:03&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-17 08:55:47&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-20 17:40:48&quot;,&quot;http_code&quot;:206}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-20 17:40:48&quot;,&quot;http_code&quot;:206},&quot;process&quot;:&quot;done&quot;}]'></div>
<p><img decoding="async" class="alignleft size-full wp-image-1976" src="https://learncgames.com/wp-content/uploads/2020/12/timings.png" alt="Timings" width="649" height="325" srcset="https://learncgames.com/wp-content/uploads/2020/12/timings.png 649w, https://learncgames.com/wp-content/uploads/2020/12/timings-300x150.png 300w" sizes="(max-width: 649px) 100vw, 649px" />So I got round to writing that SDL vs TTF timing comparison. However I had to make a few assumptions. The program was written for Windows but easily converts to Linux (<em>apart from the timing code but I did that <a title="Compiling Linux timing code" href="https://learncgames.com/that-clang-c-compilation/" target="_blank" rel="nofollow noopener noreferrer">back in March</a> for Linux</em>) is about 250 lines long. I&#8217;ve uploaded it to <a title="Link to LearnC on GitHub" href="https://github.com/David-H-Bolton/LearnC" target="_blank" rel="nofollow noopener noreferrer">GitHub</a>, and the zip file contains not only the VS 2019 project and source but all the SDL2 dlls plus a free font MONOFONT.TTF (renamed to font.ttf) and test.png which is the bitmap font as a .png file.</p>
<p>Note you&#8217;ll have to edit the <strong>sdlttf.c</strong> file and change the paths to load the font .png and ttf files in lines 96 and 97.</p>
<p>I&#8217;d never used <a title="Link to SDL_ttf on libsdl.org" href="https://www.libsdl.org/projects/SDL_ttf/" target="_blank" rel="nofollow noopener noreferrer">SDL_ttf</a> before though it worked out easy enough. There are three levels of TTF &#8211; fast, not so fast and slow but pretty. I went for the fastest setting. The SDL text is in red and the TTF in yellow and are roughly the same height about 30 points.</p>
<p>If you read the caption you&#8217;ll see that my bitmap font drawing (I call it SDL) is nearly 14 x slower than TTF. In my code I prerendered the phrase &#8220;Sphinx of black quartz, judge my vow&#8221; which has all 26 letters a-z but is shorter than standard phrase &#8220;the lazy quick fox&#8230;&#8221;.</p>
<p>SDL_ttf renders the phrase into a <strong>surface</strong> which is a memory structure in RAM. Calling <strong>SDL_CreateTextureFromSurface</strong> (line 188) creates a texture from this (it copies the bitmap from RAM to VRAM) and so in my program I was just blitting this texture to the screen. The SDL printing in the <strong>print</strong> function (Line 157) gets each character, and calls <strong>printch</strong> (line 143) which works out where in the <strong>textFont</strong> texture that character and blits it. I&#8217;m guessing the overhead of blitting individual characters is what makes it so much slower.</p>
<p>So for fixed phrases where you can prerender the surface and texture, SDL_ttf is way faster and more flexible with colour and to a certain extent size. Colour is set at runtime while size is set when you prerender the font into the surface. If you want different sizes then you need multiple surfaces and corresponding textures for each size. Things like say a score would be a bit slower because it would need to be rendered each time and copied to a texture or prerender all 10 digits and have a texture for each and draw it much like the print method does.</p>
<p>The program is a little rough in places; it was thrown together over three nights.</p>The post <a href="https://learncgames.com/timing-of-sdl-vs-ttf-fonts/">Timing of SDL vs TTF fonts</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></content:encoded>
					
					<wfw:commentRss>https://learncgames.com/timing-of-sdl-vs-ttf-fonts/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1975</post-id>	</item>
		<item>
		<title>High speed timing in C</title>
		<link>https://learncgames.com/high-speed-timing-in-c/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=high-speed-timing-in-c</link>
		
		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Fri, 06 Mar 2020 06:00:48 +0000</pubDate>
				<category><![CDATA[Other stuff]]></category>
		<category><![CDATA[Source code]]></category>
		<category><![CDATA[Techniques]]></category>
		<category><![CDATA[timing]]></category>
		<guid isPermaLink="false">https://learncgames.com/?p=70</guid>

					<description><![CDATA[<p>One of the things I like doing is timing code. Not to benchmark it per se, but to get an idea of performance. I have a small set of functions to do this on most CPUs for the last ten or fifteen years. It uses the high frequency clock, which. on my PC (a five [&#8230;]</p>
The post <a href="https://learncgames.com/high-speed-timing-in-c/">High speed timing in C</a> first appeared on <a href="https://learncgames.com">Learn C Games Programming Blog</a>.]]></description>
										<content:encoded><![CDATA[<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:13,&quot;href&quot;:&quot;https:\/\/github.com\/David-H-Bolton\/LearnC&quot;,&quot;archived_href&quot;:&quot;http:\/\/web-wp.archive.org\/web\/20250807013721\/https:\/\/github.com\/David-H-Bolton\/LearnC&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-02-06 10:35:09&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-09 16:40:16&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-12 18:24:15&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-15 22:16:30&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-18 22:19:31&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-21 22:22:05&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-25 04:17:25&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-28 04:17:49&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-03 04:22:25&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-06 04:38:11&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-09 08:48:04&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-12 09:17:10&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-15 10:18:23&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-18 12:43:50&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-21 15:14:39&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-24 15:19:12&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-27 16:38:15&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-30 19:05:39&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-02 22:49:21&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-06 04:18:17&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-09 04:19:40&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-12 09:53:38&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-15 10:17:36&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-18 13:35:54&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-04-21 15:50:44&quot;,&quot;http_code&quot;:206}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-04-21 15:50:44&quot;,&quot;http_code&quot;:206},&quot;process&quot;:&quot;done&quot;}]'></div>
<p>One of the things I like doing is timing code. Not to benchmark it per se, but to get an idea of performance. I have a small set of functions to do this on most CPUs for the last ten or fifteen years. It uses the high frequency clock, which. on my PC (a five year old I7-5930K), this counts at the rate of 3,500,000,000 per second.</p>
<p>You just read this timer twice, subtract the difference and then divide by the frequency (3500,000,000) to get a time in fractions of a second accurate to nano-seconds. (10-9 seconds).</p>
<p>Here&#8217;s the code for Windows. It&#8217;s in the several of the ebook chapters, e.g. <a href="https://github.com/David-H-Bolton/LearnC" target="_blank" rel="noopener noreferrer">chapter 48 </a>(download the file <strong>asteroids_ch48.zip</strong> and unzip) . Or you can just copy from here.</p>
<p>hr_time.h</p>
<pre><code class="language-c" data-line="">#include &lt;windows.h&gt;

typedef struct {
LARGE_INTEGER start;
LARGE_INTEGER stop;
} stopWatch;

void startTimer(stopWatch *timer);
void stopTimer(stopWatch *timer);
double LIToSecs(LARGE_INTEGER * L);
double getElapsedTime(stopWatch *timer);</code></pre>
<p>and hr_time.c</p>
<pre><code class="language-c" data-line="">#include 

#ifndef hr_timer
#include &quot;hr_time.h&quot;
#define hr_timer
#endif

void startTimer(stopWatch *timer) {
QueryPerformanceCounter(&amp;timer-&gt;start);
}

void stopTimer(stopWatch *timer) {
  QueryPerformanceCounter(&amp;timer-&gt;stop);
}

double LIToSecs(LARGE_INTEGER * L) {
  LARGE_INTEGER frequency;
  QueryPerformanceFrequency(&amp;frequency);
return ((double)L-&gt;QuadPart / (double)frequency.QuadPart);
}
double getElapsedTime(stopWatch *timer) {
LARGE_INTEGER time;
  time.QuadPart = timer-&gt;stop.QuadPart - timer-&gt;start.QuadPart;
return LIToSecs(&amp;time);
}</code></pre>
<p>Use it like this:</p>
<pre><code class="language-c" data-line="">stopWatch s; // declare a stopwatch variable

startTimer(&amp;s);

// your code to be timed here

stopTimer(&amp;s);

printf(&#039;It took %10.6f secs&#039;,getElapsedTime(&amp;s));</code></pre>The post <a href="https://learncgames.com/high-speed-timing-in-c/">High speed timing 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">70</post-id>	</item>
	</channel>
</rss>
