{"id":5875,"date":"2023-05-13T00:01:53","date_gmt":"2023-05-13T07:01:53","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5875"},"modified":"2023-05-06T10:25:48","modified_gmt":"2023-05-06T17:25:48","slug":"programming-with-nanoseconds","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5875","title":{"rendered":"Programming with Nanoseconds"},"content":{"rendered":"<p>From <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5862\">last week&#8217;s Lesson<\/a>, the <em>clock_gettime()<\/em> function returns values in both <em>time_t<\/em> (Unix Epoch) seconds as well as nanoseconds. This rich variety makes the function quite useful for coding time-critical details where values less than a second are desired.<br \/>\n<!--more--><br \/>\nI recall programming for ancient microcomputers where various system functions returned the system time accurate to values less than one second. These details made it easy to code wait times less than a second, which I often did for communications programs. (I did a lot of &#8220;modem&#8221; programming back in the day.)<\/p>\n<p>When I began programming for Unix, I noticed that the standard C library offered functions that only tracked time to the second &mdash; nothing faster. The <em>clock_gettime()<\/em> function, and its capability of returning nanosecond values, piqued my curiosity. Specifically, I wanted to know how fast a program could fetch the nanosecond values.<\/p>\n<p>The following code calls the <em>clock_gettime()<\/em> function twice, each time capturing the nanosecond value. The output shows the difference between the values or the number of nanoseconds that passes between function calls.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_05_13-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2023_05_13-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\nint main()\r\n{\r\n    struct timespec res;\r\n    long nano1,nano2;\r\n\r\n    <span class=\"comments\">\/* read consecutive nanosecond values *\/<\/span>\r\n    clock_gettime(CLOCK_REALTIME,&amp;res);\r\n    nano1 = res.tv_nsec;\r\n    clock_gettime(CLOCK_REALTIME,&amp;res);\r\n    nano2 = res.tv_nsec;\r\n    printf(\"Difference: %lu\\n\",nano2-nano1);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Variable <code>res.tv_nsec<\/code> respresents the nanosecond member of the <em>timespec<\/em> structure, the number of nanoseconds elapsed since the last second returned from a <em>clock_gettime()<\/em> function call. At Line 11, the initial value fetched is saved in variable <code>nano1<\/code>. Then the code calls the <em>clock_gettime()<\/em> function again, this time storing the <code>res.tv_nsec<\/code> value in variable <code>nano2<\/code>. A <em>printf()<\/em> statement outputs the difference.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Difference: 88<\/code><\/p>\n<p>In the output, 88 nanoseconds passed between <em>clock_gettime()<\/em> calls. I ran the code a few times and the values ranged from 69 to 93 nanoseconds. Changing the <code>CLOCK_REALTIME<\/code> constant to <code>CLOCK_MONOTONIC<\/code> increased the range of values returned from 54 to 126 nanoseconds. Don&#8217;t ask me to explain why, though the larger range did surprise me.<\/p>\n<p>The <em>clock_gettime()<\/em> function takes time itself to do its thing, which means trying to pare down the nanosecond interval between calls is going to be difficult. Aside from coding the call in Assembly (which may not show any improvements), I modified the code to use two <em>timespec<\/em> structures. This way I could call the <em>clock_gettime()<\/em> function twice in a row, hopefully to obtain a tighter nanosecond interval. Here is the code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_05_13-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2023_05_13-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\nint main()\r\n{\r\n    struct timespec res1,res2;\r\n\r\n    <span class=\"comments\">\/* read consecutive nanosecond values *\/<\/span>\r\n    clock_gettime(CLOCK_REALTIME,&amp;res1);\r\n    clock_gettime(CLOCK_REALTIME,&amp;res2);\r\n    printf(\"Difference: %lu\\n\",res2.tv_nsec-res1.tv_nsec);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Alas, this improvement didn&#8217;t noticeably alter the output:<\/p>\n<p><code>Difference: 70<\/code><\/p>\n<p>Multiple runs showed intervals similar to the first program. Regardless, my goal was to see how tight I could make the calls. After all, if you want to write a program that steps in nanosecond increments, would it be possible to use the <em>clock_gettime()<\/em> function to either set the intervals or time them? Probably not.<\/p>\n<p>In next week&#8217;s Lesson, I use the <em>clock_gettime()<\/em> function to write a time-delay function using the nanosecond values. Well, using them as best I can.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Your computer&#8217;s clock knows time down to the nanosecond, which you can fetch for use in your programs. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5875\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-5875","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5875","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5875"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5875\/revisions"}],"predecessor-version":[{"id":5881,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5875\/revisions\/5881"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5875"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5875"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5875"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}