{"id":5720,"date":"2023-01-28T00:01:04","date_gmt":"2023-01-28T08:01:04","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5720"},"modified":"2023-01-21T10:24:28","modified_gmt":"2023-01-21T18:24:28","slug":"checking-the-cpu-clock","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5720","title":{"rendered":"Checking the CPU Clock"},"content":{"rendered":"<p>The <em>clock()<\/em> function has nothing to do with human time. Nope. It returns a value from the computer&#8217;s CPU, the processor time. You can use this value to determine the amount of time it takes your programs to run.<br \/>\n<!--more--><br \/>\nOf course, the <em>time<\/em> utility at the command prompt does the same thing without your need to code anything. Just type:<\/p>\n<p><code>time <em>program<\/em><\/code><\/p>\n<p>where <code><em>program<\/em><\/code> is the name of the program you want to time. The results look like this:<\/p>\n<p><code>$ time .\/a.out<br \/>\n<em>[program output appears here]<\/em><\/p>\n<p>real\t0m0.004s<br \/>\nuser\t0m0.001s<br \/>\nsys\t0m0.002s<\/code><\/p>\n<p>The three values are: 4 milliseconds for the entire process; 1 millisecond of user time; and 2 milliseconds of system time.<\/p>\n<p>Within your code, you can use the <em>clock()<\/em> function to fetch these CPU time values. The value returned is a <em>clock_t<\/em> (<em>long unsigned<\/em>) integer representing the CPU time, but this value must be interpreted properly.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_01_28-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2023_01_28-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    clock_t start;\r\n\r\n    start = clock();\r\n    printf(\"Processor start time: %lu\\n\",start);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <code>time.h<\/code> header is required to define and use the <em>clock()<\/em> function.<\/p>\n<p>The <em>clock_t<\/em> variable <code>start<\/code> holds the value returned from <em>clock()<\/em>, which is output by using the <code>%lu<\/code> placeholder.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Processor start time: 2191<\/code><\/p>\n<p>The value 2191 (or whatever you see on your system) is meaningless without context. In this case, the value is related to the number of clock ticks per second the processor generates. This per-second value is different for each processor, though the defined constant <code>CLOCKS_PER_SEC<\/code> is used to calculate the number of processor ticks per second. The <code>CLOCKS_PER_SEC<\/code> value is set by default to one million, which doesn&#8217;t make it any more useful given that (again) processors have different clock rates.<\/p>\n<p>To make <em>clock()<\/em> function&#8217;s return value more realistic, divide it by the <code>CLOCKS_PER_SEC<\/code> constant. To get the best results, cast the <em>clock_t<\/em> value as a <em>double<\/em>. You must also update the code to replace the <code>%lu<\/code> conversion character with <code>%f<\/code>:<\/p>\n<p><code>printf(\"Processor start time: %f\\n\",(double)start\/CLOCKS_PER_SEC);<\/code><\/p>\n<p><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_01_28-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">Click here<\/a> to view the full code on GitHub. Here is the updated output:<\/p>\n<p><code>Processor start time: 0.002196<\/code><\/p>\n<p>The point of this exercise is to time how long code, or a portion of code, executes. To do so, two calls must be made to <em>clock()<\/em>, one before all the action and another after. The following update to the code adds a <em>for<\/em> loop to ensure that time is consumed before the second reading is made:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_01_28-Lesson-c.c\" rel=\"noopener\" target=\"_blank\">2023_01_28-Lesson-c.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\nint main()\r\n{\r\n    int r;\r\n    clock_t start,finish;\r\n\r\n    <span class=\"comments\">\/* obtain start time *\/<\/span>\r\n    start = clock();\r\n    printf(\"Processor start time: %f\\n\",(float)start\/CLOCKS_PER_SEC);\r\n\r\n    <span class=\"comments\">\/* seed the randomizer *\/<\/span>\r\n    srand( (unsigned)time(NULL) );\r\n    \r\n    <span class=\"comments\">\/* loop for a while *\/<\/span>\r\n    for(;;)\r\n    {\r\n        r = rand() % 1000;\r\n        if( r==1 )\r\n            break;\r\n    }\r\n\r\n    <span class=\"comments\">\/* obtain finish time *\/<\/span>\r\n    finish = clock();\r\n    printf(\"Processor end time: %f\\n\",(float)finish\/CLOCKS_PER_SEC);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>for<\/em> loop keeps repeating until the a random value in the range of zero to 1,000 is equal to one. This loop can take a few milliseconds to complete, which makes the output values different each run:<\/p>\n<p><code>Processor start time: 0.002194<br \/>\nProcessor end time: 0.002220<\/code><\/p>\n<p>To discover how much CPU time the program has consumed, the code must obtain the difference between the two values. The following <em>printf()<\/em> statement is added just before the <em>return<\/em> statement:<\/p>\n<p><code>printf(\"Total program runtime: %f\\n\",<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((float)finish-start)\/CLOCKS_PER_SEC<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<\/code><\/p>\n<p>The full code is <a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_01_28-Lesson-d.c\">available on GitHub<\/a>. The output now shows the difference:<\/p>\n<p><code>Processor start time: 0.002229<br \/>\nProcessor end time: 0.002258<br \/>\nTotal program runtime: 0.000029<\/code><\/p>\n<p>I&#8217;ve seen examples on the Interwebs where this result is multiplied by 1,000. I assume that&#8217;s to translate the result into milliseconds, though I don&#8217;t see any data to back that up.<\/p>\n<p>If I run the program through the <em>time<\/em> utility, I see this output:<\/p>\n<p><code>$ time .\/a.out<br \/>\nProcessor start time: 0.002200<br \/>\nProcessor end time: 0.002236<br \/>\nTotal program runtime: 0.000036<\/p>\n<p>real&nbsp;&nbsp;&nbsp;&nbsp;0m0.004s<br \/>\nuser&nbsp;&nbsp;&nbsp;&nbsp;0m0.001s<br \/>\nsys&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0m0.002s<\/code><\/p>\n<p>The <em>time<\/em> utility reports a total runtime of four milliseconds, which is close to the program result of .000036. The difference might explain why I&#8217;ve seen code that multiplies the <em>clock_t<\/em> value by 1,000. Again, that&#8217;s just my assumption.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An extremely low-level function is <em>clock()<\/em>, which has nothing to do with human time. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5720\">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-5720","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\/5720","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=5720"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5720\/revisions"}],"predecessor-version":[{"id":5731,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5720\/revisions\/5731"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}