{"id":69,"date":"2013-05-25T00:01:01","date_gmt":"2013-05-25T08:01:01","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=69"},"modified":"2013-05-18T09:45:34","modified_gmt":"2013-05-18T17:45:34","slug":"the-delay-function","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=69","title":{"rendered":"The <em>delay()<\/em> Function"},"content":{"rendered":"<p>Most C programmers know the <em>sleep()<\/em> function, which pauses program execution for a given number of seconds. Seconds are a vast chunk of time, especially in a computer where things happen quickly. So a desire exists for a function that delays execution for smaller slices of time. For that duty you&#8217;ll need to code your own function, similar to the <em>delay()<\/em> function I use.<br \/>\n<!--more--><br \/>\nThe <em>delay()<\/em> function is built upon a C library function called <em>clock()<\/em>.<\/p>\n<p>The <em>clock()<\/em> function returns a time value in clock ticks, which is based on the processor&#8217;s speed. The value returned is of the <code>clock_t<\/code> variable type. You can use subsequent reads of the <em>clock()<\/em> function to determine elapsed time.<\/p>\n<p>The following code demonstrates elapsed time as measured by the <em>clock()<\/em> function:<\/p>\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    clock_t tix;\r\n\r\n    tix = clock();\r\n    printf(\"And the clock() function returned %ld.\\n\",tix);\r\n    tix = clock();\r\n    printf(\"And the clock() function returned %ld.\\n\",tix);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here is the sample output I see on my system:<\/p>\n<p><code>And the clock() function returned 1072.<br \/>\nAnd the clock() function returned 1107.<\/code><\/p>\n<p>Of course, to make the <em>clock()<\/em> function useful, it helps to know how many clock ticks take place a second. That information is stored in the <code>CLOCKS_PER_SEC<\/code> constant.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\nint main(void)\r\n{\r\n\tclock_t ticks1, ticks2;\r\n\r\n\tticks1=clock();\r\n\tticks2=ticks1;\r\n\twhile((ticks2\/CLOCKS_PER_SEC-ticks1\/CLOCKS_PER_SEC)&lt;1)\r\n\t\tticks2=clock();\r\n\r\n\tprintf(\"It took %ld ticks to wait one second.\\n\",ticks2-ticks1);\r\n\tprintf(\"This value should be the same as CLOCKS_PER_SEC which is %d.\\n\",CLOCKS_PER_SEC);\r\n\treturn 0;\r\n}<\/pre>\n<p>The above code initializes both <code>clock_t<\/code> variables to the same value at Line 9. Then a <code>while<\/code> loop spins until the difference between the values is 1. Here&#8217;s the output:<\/p>\n<p><code>It took 999017 ticks to wait one second.<br \/>\nThis value should be the same as CLOCKS_PER_SEC which is 1000000.<\/code><\/p>\n<p>Close enough.<\/p>\n<p>Because the <code>CLOCKS_PER_SECOND<\/code> value could be different on each computer, you need to do some math to figure out how to concoct a <em>delay()<\/em> function that works on all platforms. Below you see my solution, which uses a millisecond duration as the argument to <em>delay()<\/em>.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\nvoid delay(int milliseconds);\r\n\r\nint main()\r\n{\r\n    int x;\r\n\r\n    puts(\"Loop with 250 millisecond delay:\");\r\n    for(x=0;x&lt;10;x++)\r\n    {\r\n        printf(\"%d\\n\",x+1);\r\n        delay(250);\r\n    }\r\n    putchar('\\n');\r\n\r\n    return(0);\r\n}\r\n\r\nvoid delay(int milliseconds)\r\n{\r\n    long pause;\r\n    clock_t now,then;\r\n\r\n    pause = milliseconds*(CLOCKS_PER_SEC\/1000);\r\n    now = then = clock();\r\n    while( (now-then) &lt; pause )\r\n        now = clock();\r\n}<\/pre>\n<p>At Line 26, the <em>delay()<\/em> function calculates the <code>pause<\/code> value in milliseconds (one thousandth of a second). The <code>clock_t<\/code> variables <code>now<\/code> and <code>then<\/code> are initialized at Line 27. Then the <code>while<\/code> loop waits until the proper number of milliseconds have passed.<\/p>\n<p>Feel free to use the <em>delay()<\/em> function in your code when a short pause is required.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you need to pause program execution for a given number of milliseconds, feel free to use my <em>delay()<\/em> function. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=69\">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-69","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\/69","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=69"}],"version-history":[{"count":10,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/69\/revisions"}],"predecessor-version":[{"id":87,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/69\/revisions\/87"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=69"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=69"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=69"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}