{"id":5797,"date":"2023-03-18T00:01:52","date_gmt":"2023-03-18T07:01:52","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5797"},"modified":"2023-03-25T09:33:01","modified_gmt":"2023-03-25T16:33:01","slug":"hang-on-a-sec-part-i","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5797","title":{"rendered":"Hang On a Sec, Part I"},"content":{"rendered":"<p>I learned to program on a microcomputer, a gizmo radically different in software and hardware design from today&#8217;s systems. An example of this difference is that if you wanted to write code that paused for a second, you wrote a <em>for<\/em> loop delay. Such a thing is obnoxiously impractical today &mdash; which sounds like a dare!<br \/>\n<!--more--><br \/>\n<img decoding=\"async\" src=\"https:\/\/oldcomputers.net\/pics\/trs80-iii.jpg\" alt=\"TRS-80 Model III\" \/><br \/>\nOn my trusty TRS-80 Model III &mdash; yes the one with the TV set for a monitor that frazzled my eyeballs &mdash; I knew that writing a <code>FOR<\/code> loop (in BASIC) counting from one to 1,000 would pause a program for one second. Silly as it sounds, this technique worked. And it was consistent for all TRS-80 Model III computers. Lots of programmers used this trick.<\/p>\n<p>The same trick was employed in the early days of the PC, which was also technically a microcomputer. The PC&#8217;s processor was faster, so the delay loops needed to be longer. It wasn&#8217;t until multitasking operating systems took over in the late 1990s that writing a delay loop proved to be embarrassingly wrong.<\/p>\n<p>Different processors have different speeds. Not only that, but some programs can be interrupted by other processes. These advances in computing technology are marvelous, but they render the delay loop useless. Further, modern computer hardware features a system clock, which is far more reliable for programming a pause.<\/p>\n<p>Being curious and feeling nostalgic, I wondered how large a loop needed to be on my current computer to pause program execution for one second?<\/p>\n<p>The first step is to see how fast the computer counts between seconds. To do so, I wrote a program that monitors when seconds turn over. The following code fetches the current epoch time, or clock ticks in seconds, then waits for the value to change.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_03_18-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2023_03_18-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    time_t now,then;\r\n\r\n    <span class=\"comments\">\/* obtain clock tick Epoch value *\/<\/span>\r\n    time(&amp;now);\r\n    printf(\"%ld\\n\",now);\r\n\r\n    <span class=\"comments\">\/* pause one second *\/<\/span>\r\n    then = now;\r\n    while(then==now)\r\n        time(&amp;now);\r\n    printf(\"%ld\\n\",now);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Variable <code>now<\/code> stores the current epoch time, which is also saved in variable <code>then<\/code>. A <em>while<\/em> loop spins as long as both <code>now<\/code> and <code>then<\/code> are equal. Within the loop, variable <code>now<\/code> is updated with the current clock tick value. When it changes, the loop ends and the new time is output:<\/p>\n<p><code>1677951318<br \/>\n1677951319<\/code><\/p>\n<p>While this code indicates when the system clock&#8217;s seconds turn over, it doesn&#8217;t really time an exact second. That&#8217;s because the program may start between seconds. No, to time a full second, a second <em>while<\/em> loop is necessary. Here is the updated code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_03_18-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2023_03_18-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    time_t now,then;\r\n\r\n    <span class=\"comments\">\/* obtain clock tick Epoch value *\/<\/span>\r\n    time(&amp;now);\r\n\r\n    <span class=\"comments\">\/* pause one second *\/<\/span>\r\n    then = now;\r\n    while(then==now)\r\n        time(&amp;now);\r\n    <span class=\"comments\">\/* now a new second has begun\r\n       start the next \"full second\" loop *\/<\/span>\r\n    printf(\"%ld\\n\",now);\r\n    then = now;\r\n    while(then==now)\r\n        time(&amp;now);\r\n    printf(\"%ld\\n\",now);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The value of <code>now<\/code> is output after the first<em>while<\/em> loop ends at the start of a new second. Then, after the second <em>while<\/em> loop stops, the next clock tick value is output:<\/p>\n<p><code>1677951742<br \/>\n1677951743<\/code><\/p>\n<p>You may encounter a slight pause before the first line is output. Still, the time between the first line output and the next is one full second (or as close as it can be given other things going on in the system).<\/p>\n<p>The next step is to count the number of iterations for the second <em>while<\/em> loop. I cover this code update in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5805\">next week&#8217;s Lesson<\/a>, along with the final <em>for<\/em> loop code that delays the computer for one second &mdash; just like the old days.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spinning through a loop takes time. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5797\">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-5797","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\/5797","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=5797"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5797\/revisions"}],"predecessor-version":[{"id":5824,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5797\/revisions\/5824"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}