{"id":5805,"date":"2023-03-25T00:01:12","date_gmt":"2023-03-25T07:01:12","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5805"},"modified":"2023-03-18T09:55:20","modified_gmt":"2023-03-18T16:55:20","slug":"hang-on-a-sec-part-ii","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5805","title":{"rendered":"Hang On a Sec, Part II"},"content":{"rendered":"<p>The old days are gone, and with them the practice of using a <em>for<\/em> loop as a timing delay. Loops still do pause program execution, with the question being how long does it take a computer to wait for a loop?<br \/>\n<!--more--><br \/>\nPicking up from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5797\">last week&#8217;s Lesson<\/a>, I need to know how many steps a loop needs to emulate those old days and pause program execution for about one second. I could just guess, starting with one million or so, but last Lesson I chose to use the computer&#8217;s clock to help me.<\/p>\n<p>The final code presented last week used the epoch value returned from the <em>time()<\/em> function to monitor a flip from one second to the next. As the epoch value represents seconds, this approach seems good enough to present a situation where the computer can count away during the loop and report the count value. Here is the update to last week&#8217;s code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_03_25-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2023_03_25-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    long count;\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    then = now;\r\n    count = 0;\r\n    while(then==now)\r\n    {\r\n        count++;\r\n        time(&amp;now);\r\n    }\r\n    printf(\"Cycles: %ld\\n\",count);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>New to this version of the code is the declaration of <em>long<\/em> variable <code>count<\/code>. It&#8217;s initialized to zero at Line 19. Within the second <em>while<\/em> loop, <code>count<\/code> is incremented.<\/p>\n<p>Gone are the two <em>printf()<\/em> statements that output the clock tick values. A single <em>printf()<\/em> statement at Line 25 outputs the number of cycles (the value of variable <code>count<\/code>) processed during the one-second loop. On my computer, I see this result:<\/p>\n<p><code>Cycles: 377291420<\/code><\/p>\n<p>The next step is to write a program that uses this value in a <em>for<\/em> loop to attempt to pause execution for one second. Here is the code I wrote:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_03_25-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2023_03_25-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    long delay;\r\n\r\n    <span class=\"comments\">\/* pause one second *\/<\/span>\r\n    for( delay=0; delay&lt;377291420; delay ++ )\r\n        ;\r\n\r\n    return(0);\r\n}<\/pre>\n<p>I could have used a <em>while<\/em> loop, but <em>for<\/em> loops are how I wrote code way back when. No output is needed. Instead, I use the command line <em>time<\/em> utility to see how long the program (<code>a.out<\/code>) takes to run:<\/p>\n<p><code>$ time .\/a.out<\/p>\n<p>real    0m0.640s<br \/>\nuser    0m0.633s<br \/>\nsys     0m0.000s<\/code><\/p>\n<p>Rather than a full second, the code took 0.64 seconds to run &mdash; far less than what I hoped. Bummer.<\/p>\n<p>Then I remembered something important: The original program that counted the loop&#8217;s iterations also called the <em>time()<\/em> function. Adding that call adds to the delay. To more accurately pause a second, the <em>for<\/em> loop should also call the <em>time()<\/em> function, as shown in this update:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_03_25-Lesson-c.c\" rel=\"noopener\" target=\"_blank\">2023_03_25-Lesson-c.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    long delay;\r\n\r\n    <span class=\"comments\">\/* pause one second *\/<\/span>\r\n    for( delay=0; delay&lt;377291420; delay ++ )\r\n        time(NULL);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>time()<\/em> function requires an argument, which is set as <code>NULL<\/code>. Also, the <code>time.h<\/code> header is required. Duh.<\/p>\n<p>Here is the result when the program is run from the <em>time<\/em> utility:<\/p>\n<p><code>$ time .\/a.out<\/p>\n<p>real    0m0.944s<br \/>\nuser    0m0.937s<br \/>\nsys     0m0.001s<\/code><\/p>\n<p>The value 0.944 is much closer to one second, which I&#8217;m flagging as a success.<\/p>\n<p>Then again, it&#8217;s not really a success. Running these delay programs on an older PC yielded a cycles value of 6,686,683 &mdash; far less than 377,291,420 on my main, fast computer. So the final program, the one that supposedly takes one second to run, takes nearly a minute to run on that older PC! Hopefully you can see why using a delay loop on a modern computer shouldn&#8217;t be a thing any more.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Writing a loop that pauses exactly one second &#8211; on my computer. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5805\">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-5805","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\/5805","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=5805"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5805\/revisions"}],"predecessor-version":[{"id":5823,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5805\/revisions\/5823"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}