{"id":1922,"date":"2011-04-21T14:06:00","date_gmt":"2011-04-21T13:06:00","guid":{"rendered":"https:\/\/www.peppercrew.nl\/index.php\/2011\/04\/bypass-the-15ms-inaccuracy\/"},"modified":"2011-05-18T20:48:18","modified_gmt":"2011-05-18T19:48:18","slug":"bypass-the-15ms-inaccuracy","status":"publish","type":"post","link":"https:\/\/ingmarverheij.com\/en\/bypass-the-15ms-inaccuracy\/","title":{"rendered":"Bypass the 15ms inaccuracy"},"content":{"rendered":"<p>As Tim Mangan recently <a href=\"https:\/\/www.brianmadden.com\/blogs\/timmangan\/archive\/2011\/04\/13\/system-performance-it-s-still-time-to-change-the-timers.aspx\">blogged<\/a>, the system timer in a multiprocessor Windows machine operates at 15ms. The effect of this 15ms is that a measurement \/ calculation in your program can vary with 15ms. This is fine for most operations in your program, but not when you want accuracy to the millisecond.<\/p>\n<p>Although the best solution to the problem is changing the system timers, as Tim Mangan wrote, a workaround is available.<\/p>\n<p><!--more--><\/p>\n<p>In the winmm.dll (Multimedia functions) an API is available to request a minimum resolution for periodic timers. When this API is called with an argument of 1, the accuracy changes from 15ms to 1ms.<\/p>\n<p>When accuracy is needed the API \u201c<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/dd757624(v=VS.85).aspx\">timeBeginPeriod<\/a>\u201d is called with the minimum resolution in ms as argument. The timers wil now be more accurate.<\/p>\n<p>Next, when the system time needs to be read the API \u201c<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/dd757629(v=VS.85).aspx\">timeGetTime<\/a>\u201d is called. This API wil return the system time (since Windows was started) in milliseconds.<\/p>\n<p>When the accuracy is no longer needed the request for the accuracy can be ended with the API \u201c<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/dd757626(v=VS.85).aspx\">timeEndPeriod<\/a>\u201d.<\/p>\n<p>&nbsp;<\/p>\n<p>Setting the timer accuracy is a <span style=\"text-decoration: underline;\">system-wide<\/span> setting, so it will affect the whole machine. This is important because it will cost more CPU cycles, which require more power, which require more battery. So this will drain you laptop battery a lot faster!<\/p>\n<p>&nbsp;<\/p>\n<p>Ingmar Verheij<\/p>","protected":false},"excerpt":{"rendered":"<p>As Tim Mangan recently blogged, the system timer in a multiprocessor Windows machine operates at 15ms. The effect of this 15ms is that a measurement \/ calculation in your program can vary with 15ms. This is fine for most operations in your program, but not when you want accuracy to the millisecond. Although the best [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[302],"tags":[264,265,266],"class_list":["post-1922","post","type-post","status-publish","format-standard","hentry","category-performance-testing","tag-15ms","tag-system-timer","tag-timebeginperiod"],"_links":{"self":[{"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/posts\/1922","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/comments?post=1922"}],"version-history":[{"count":2,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/posts\/1922\/revisions"}],"predecessor-version":[{"id":2284,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/posts\/1922\/revisions\/2284"}],"wp:attachment":[{"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/media?parent=1922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/categories?post=1922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/tags?post=1922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}