{"id":44,"date":"2023-05-12T02:53:36","date_gmt":"2023-05-12T02:53:36","guid":{"rendered":"https:\/\/feellikelearning.com\/en\/?p=44"},"modified":"2023-05-12T02:53:36","modified_gmt":"2023-05-12T02:53:36","slug":"what-is-time-monotonic-in-python","status":"publish","type":"post","link":"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/12\/what-is-time-monotonic-in-python\/","title":{"rendered":"What is time.monotonic() in Python?"},"content":{"rendered":"\n<p>In Python, the <code>time.monotonic()<\/code> function returns the value of a monotonic clock. It is used to measure elapsed time in a way that is not affected by system clock adjustments or changes due to time synchronization.<\/p>\n\n\n\n<p>The monotonic clock is not affected by system time adjustments, such as changes made by the user or by the system itself for daylight saving time. This makes it useful for measuring time intervals or performance-related tasks where you need a reliable and consistent time reference.<\/p>\n\n\n\n<p>The <code>time.monotonic()<\/code> function returns a floating-point value representing the time in seconds since an unspecified starting point. It does not have a defined relationship to the time of day and should not be used for that purpose. The only guarantee is that the value will increase over time, so you can measure the difference between two calls to <code>time.monotonic()<\/code> to calculate elapsed time.<\/p>\n\n\n\n<p>Here&#8217;s a simple example that demonstrates the usage of <code>time.monotonic()<\/code> to measure the execution time of a code block:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import time\n\nstart_time = time.monotonic()\n\n# Code block to measure execution time\n# ...\n\nend_time = time.monotonic()\nelapsed_time = end_time - start_time\n\nprint(\"Elapsed time:\", elapsed_time, \"seconds\")\n<\/code><\/pre>\n\n\n\n<p>By subtracting the start time from the end time, you can determine the elapsed time in seconds. It&#8217;s important to note that the value returned by <code>time.monotonic()<\/code> represents relative time and is not related to the system&#8217;s wall clock time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">what is the difference between time.monotonic and time.time?<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>time.monotonic()<\/code>:\n<ul class=\"wp-block-list\">\n<li>It returns the value of a monotonic clock in seconds (floating-point).<\/li>\n\n\n\n<li>The unit of measurement for <code>time.monotonic()<\/code> is seconds.<\/li>\n\n\n\n<li>It provides a reliable and consistent time reference for measuring elapsed time, but it does not have a defined relationship with the time of day.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><code>time.time()<\/code>:\n<ul class=\"wp-block-list\">\n<li>It returns the current time as a floating-point value representing the number of seconds since the Unix epoch (January 1, 1970, at 00:00:00 UTC).<\/li>\n\n\n\n<li>The unit of measurement for <code>time.time()<\/code> is also seconds.<\/li>\n\n\n\n<li>Although the function returns the time in seconds, it can often include a fractional part representing milliseconds or smaller units, depending on the platform.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Another way is time.perf_counter()<\/h2>\n\n\n\n<p>Here&#8217;s an example that demonstrates how to calculate the elapsed time in milliseconds:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import time\n\nstart_time = time.perf_counter()\n\n# Code block to measure execution time\n# ...\n\nend_time = time.perf_counter()\nelapsed_time_ms = (end_time - start_time) * 1000  # Convert to milliseconds\n\nprint(\"Elapsed time:\", elapsed_time_ms, \"milliseconds\")\n<\/code><\/pre>\n\n\n\n<p>In the above code, <code>time.perf_counter()<\/code> is used to obtain high-resolution time measurements, typically in fractional seconds. By subtracting the start time from the end time, you get the elapsed time in seconds. Multiplying it by 1000 converts it to milliseconds.<\/p>\n\n\n\n<p>Keep in mind that <code>time.perf_counter()<\/code> measures the time elapsed between two points in your code, including any sleep time or system-level delays. If you want to measure the time for specific code blocks excluding delays, you may need to adjust the timing logic accordingly.<\/p>\n\n\n\n<p>By using <code>time.perf_counter()<\/code>, you can achieve more precise measurements of elapsed time in milliseconds.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Python, the time.monotonic() function returns the value of a monotonic clock. It is used to measure elapsed time in a way that is not affected by system clock adjustments or changes due to time synchronization. The monotonic clock is not affected by system time adjustments, such as changes made by the user or by [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-44","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>What is time.monotonic() in Python? - Feel Like Learning<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/12\/what-is-time-monotonic-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is time.monotonic() in Python? - Feel Like Learning\" \/>\n<meta property=\"og:description\" content=\"In Python, the time.monotonic() function returns the value of a monotonic clock. It is used to measure elapsed time in a way that is not affected by system clock adjustments or changes due to time synchronization. The monotonic clock is not affected by system time adjustments, such as changes made by the user or by [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/12\/what-is-time-monotonic-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Feel Like Learning\" \/>\n<meta property=\"article:published_time\" content=\"2023-05-12T02:53:36+00:00\" \/>\n<meta name=\"author\" content=\"feellikelearning\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"feellikelearning\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/12\/what-is-time-monotonic-in-python\/\",\"url\":\"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/12\/what-is-time-monotonic-in-python\/\",\"name\":\"What is time.monotonic() in Python? - Feel Like Learning\",\"isPartOf\":{\"@id\":\"https:\/\/feellikelearning.com\/en\/#website\"},\"datePublished\":\"2023-05-12T02:53:36+00:00\",\"dateModified\":\"2023-05-12T02:53:36+00:00\",\"author\":{\"@id\":\"https:\/\/feellikelearning.com\/en\/#\/schema\/person\/1ec5aac313d6de20215fe2b8e176b8a7\"},\"breadcrumb\":{\"@id\":\"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/12\/what-is-time-monotonic-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/12\/what-is-time-monotonic-in-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/12\/what-is-time-monotonic-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/feellikelearning.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"What is time.monotonic() in Python?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/feellikelearning.com\/en\/#website\",\"url\":\"https:\/\/feellikelearning.com\/en\/\",\"name\":\"Feel Like Learning\",\"description\":\"keep curiosity alive\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/feellikelearning.com\/en\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/feellikelearning.com\/en\/#\/schema\/person\/1ec5aac313d6de20215fe2b8e176b8a7\",\"name\":\"feellikelearning\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/feellikelearning.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/36aec9d519f02362e3e89b0716ae640d08701f57e818830f3f197db5fbc1ae20?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/36aec9d519f02362e3e89b0716ae640d08701f57e818830f3f197db5fbc1ae20?s=96&d=mm&r=g\",\"caption\":\"feellikelearning\"},\"sameAs\":[\"http:\/\/feellikelearning.com\/en\"],\"url\":\"https:\/\/feellikelearning.com\/en\/index.php\/author\/feellikelearning\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What is time.monotonic() in Python? - Feel Like Learning","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/12\/what-is-time-monotonic-in-python\/","og_locale":"en_US","og_type":"article","og_title":"What is time.monotonic() in Python? - Feel Like Learning","og_description":"In Python, the time.monotonic() function returns the value of a monotonic clock. It is used to measure elapsed time in a way that is not affected by system clock adjustments or changes due to time synchronization. The monotonic clock is not affected by system time adjustments, such as changes made by the user or by [&hellip;]","og_url":"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/12\/what-is-time-monotonic-in-python\/","og_site_name":"Feel Like Learning","article_published_time":"2023-05-12T02:53:36+00:00","author":"feellikelearning","twitter_card":"summary_large_image","twitter_misc":{"Written by":"feellikelearning","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/12\/what-is-time-monotonic-in-python\/","url":"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/12\/what-is-time-monotonic-in-python\/","name":"What is time.monotonic() in Python? - Feel Like Learning","isPartOf":{"@id":"https:\/\/feellikelearning.com\/en\/#website"},"datePublished":"2023-05-12T02:53:36+00:00","dateModified":"2023-05-12T02:53:36+00:00","author":{"@id":"https:\/\/feellikelearning.com\/en\/#\/schema\/person\/1ec5aac313d6de20215fe2b8e176b8a7"},"breadcrumb":{"@id":"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/12\/what-is-time-monotonic-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/12\/what-is-time-monotonic-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/12\/what-is-time-monotonic-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/feellikelearning.com\/en\/"},{"@type":"ListItem","position":2,"name":"What is time.monotonic() in Python?"}]},{"@type":"WebSite","@id":"https:\/\/feellikelearning.com\/en\/#website","url":"https:\/\/feellikelearning.com\/en\/","name":"Feel Like Learning","description":"keep curiosity alive","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/feellikelearning.com\/en\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/feellikelearning.com\/en\/#\/schema\/person\/1ec5aac313d6de20215fe2b8e176b8a7","name":"feellikelearning","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/feellikelearning.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/36aec9d519f02362e3e89b0716ae640d08701f57e818830f3f197db5fbc1ae20?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/36aec9d519f02362e3e89b0716ae640d08701f57e818830f3f197db5fbc1ae20?s=96&d=mm&r=g","caption":"feellikelearning"},"sameAs":["http:\/\/feellikelearning.com\/en"],"url":"https:\/\/feellikelearning.com\/en\/index.php\/author\/feellikelearning\/"}]}},"_links":{"self":[{"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/posts\/44","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/comments?post=44"}],"version-history":[{"count":2,"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/posts\/44\/revisions"}],"predecessor-version":[{"id":46,"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/posts\/44\/revisions\/46"}],"wp:attachment":[{"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/media?parent=44"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/categories?post=44"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/tags?post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}