{"id":1017,"date":"2017-03-21T09:16:16","date_gmt":"2017-03-21T16:16:16","guid":{"rendered":"http:\/\/somethingk.com\/main\/?p=1017"},"modified":"2017-03-21T09:16:16","modified_gmt":"2017-03-21T16:16:16","slug":"setting-a-timer-in-c","status":"publish","type":"post","link":"https:\/\/somethingk.com\/main\/setting-a-timer-in-c\/","title":{"rendered":"Setting a timer in C++"},"content":{"rendered":"<p>I wanted to time some functions to measure performance of different types of functions in order to find what action was optimal. Here is my code for measuring the time of a C++ action.<\/p>\n<p>Basically, you collect a start clock() and a end clock() then find the total time it took to perform something by taking the difference between the two. By default the clock() function uses the CLOCKS_PER_SEC variable to convert the found clocks to seconds. My code shows how to get the result in seconds and Microseconds.<\/p>\n<p>Happy Coding!<\/p>\n<div class=\"snippetcpt-wrap\" id=\"snippet-1018\" data-id=\"1018\" data-edit=\"\" data-copy=\"\/main\/wp-json\/wp\/v2\/posts\/1017?snippet=17b1fac833&#038;id=1018\" data-fullscreen=\"https:\/\/somethingk.com\/main\/code-snippets\/setting-a-timer-in-c\/?full-screen=1\">\n\t\t\t\t<pre class=\"prettyprint linenums lang-c_cpp\" title=\"Setting a timer in C++\">#include &lt;time.h&gt; \/\/Need this for clock()\r\n\r\nint main() {\r\n    int seconds_to_micro = 1000000; \/\/conversion from seconds to microseconds var \r\n    \r\n    clock_t start = clock(); \/\/Get starting point clock\r\n    \r\n    \/\/***********PERFORM AN ACTION HERE********\r\n    \r\n    clock_t end = clock(); \/\/Get ending point clock\r\n    \r\n    float time_in_seconds = static_cast&lt;float&gt;(end - start)\/ CLOCKS_PER_SEC \/\/in seconds\r\n    float time_in_microseconds = static_cast&lt;float&gt;(end - start)\/ CLOCKS_PER_SEC * seconds_to_micro; \/\/in microseconds\r\n    \r\n    return;\r\n}<\/pre>\n\t\t\t<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I wanted to time some functions to measure performance of different types of functions in order to find what action was optimal. Here is my code for measuring the time of a C++ action. Basically, you collect a start clock() and a end clock() then find the total time it took to perform something by taking the difference between the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[177],"tags":[303,521,523,522,525,524,520],"class_list":["post-1017","post","type-post","status-publish","format-standard","hentry","category-development","tag-c","tag-clock","tag-clocks_per_sec","tag-clock_t","tag-microseconds","tag-seconds","tag-timer"],"_links":{"self":[{"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/posts\/1017","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/comments?post=1017"}],"version-history":[{"count":1,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/posts\/1017\/revisions"}],"predecessor-version":[{"id":1019,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/posts\/1017\/revisions\/1019"}],"wp:attachment":[{"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/media?parent=1017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/categories?post=1017"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/tags?post=1017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}