{"id":1279,"date":"2015-12-23T14:12:54","date_gmt":"2015-12-23T19:12:54","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=1279"},"modified":"2015-11-11T14:13:55","modified_gmt":"2015-11-11T19:13:55","slug":"its-about-time","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2015\/12\/23\/its-about-time\/","title":{"rendered":"It&#8217;s About Time!"},"content":{"rendered":"\r\n<div class=\"content\"><!--introduction--><p>Well, it's about that time.  What time?  It turns out that this is my 400th post. In 10 years!  It's been a lot of fun for me; and I hope you've learnd a lot. So time to talk about time again.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#4a85322c-3b79-4beb-9be6-b7abd0a4a0aa\">Timing before computers<\/a><\/li><li><a href=\"#0c10b6e2-0ecf-48f4-803f-15d57d2ccb1d\">Timing 1.0<\/a><\/li><li><a href=\"#cae79622-464a-4f86-81db-bfc8031a7c03\">Next Advance in Timing<\/a><\/li><li><a href=\"#f5bf3015-61bb-45f4-9908-2c0184032e52\">Timing accuracy<\/a><\/li><li><a href=\"#71829eb8-93a5-4ad9-813d-e52ebac774eb\">GPU Timing<\/a><\/li><li><a href=\"#8ab72d2f-cd01-4e3c-b9e2-ea48bcef045e\">Ten Years<\/a><\/li><\/ul><\/div><h4>Timing before computers<a name=\"4a85322c-3b79-4beb-9be6-b7abd0a4a0aa\"><\/a><\/h4><p>There are a bunch of ways to time code execution in MATLAB.  Of course, you could always use a stopwatch when you start to run some code, and, assuming it takes long enough, but not so long that you lose interest, you might get reasonable estimate.  However, many computations happen very fast on a human timescale, and so we want a way to measure the time more accurately.<\/p><h4>Timing 1.0<a name=\"0c10b6e2-0ecf-48f4-803f-15d57d2ccb1d\"><\/a><\/h4><p>When I started using MATLAB, we performed timings using <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2015b\/matlab\/ref\/tic.html\"><tt>tic<\/tt><\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2015b\/matlab\/ref\/toc.html\"><tt>toc<\/tt><\/a>.  Like this:<\/p><pre class=\"codeinput\">a = rand(1000);\r\ntic, b = a*a*a*a; elapsedtic = toc\r\n<\/pre><pre class=\"codeoutput\">elapsedtic =\r\n      0.17972\r\n<\/pre><h4>Next Advance in Timing<a name=\"cae79622-464a-4f86-81db-bfc8031a7c03\"><\/a><\/h4><p>The good thing about <tt>tic<\/tt> and <tt>toc<\/tt> is that they help you measure the time you wait before the prompt reappears.  So if your computer tries to go do something else while the computation is going on, you may incorrectly see a much larger time than the time actually spent on your calculation.  To mitigate that, we introduced <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2015b\/matlab\/ref\/cputime.html\"><tt>cputime<\/tt><\/a>.  In the days of single core machines, the original tic-toc times and cputime <i>should<\/i> have been about the same.  Unless, of course, your computer was multitasking majorly in the background.  Then <tt>cputime<\/tt> might be more reflective of the actual computation time required.  But even then, we thought <tt>tic<\/tt> and <tt>toc<\/tt> were more what people wanted to know, and recommended it in the documentation.<\/p><pre class=\"codeinput\">t = cputime; c = a*a*a*a; elapsedcpu = cputime-t\r\n<\/pre><pre class=\"codeoutput\">elapsedcpu =\r\n       0.4212\r\n<\/pre><p>But wait!  <tt>elapsedcpu<\/tt> is much larger than <tt>elapsedtic<\/tt>, by a factor of<\/p><pre class=\"codeinput\">ratio = elapsedcpu\/elapsedtic\r\n<\/pre><p>What's going on?  Well, I happen to be running this example on a dual core machine...  So, for functionality that is multithreaded, meaning much of the math in MATLAB, the cputime calculation is likely to appear larger by a factor of the number of cores you have access to.<\/p><pre class=\"codeoutput\">ratio =\r\n       2.3437\r\n<\/pre><h4>Timing accuracy<a name=\"f5bf3015-61bb-45f4-9908-2c0184032e52\"><\/a><\/h4><p>Even with all of these timing tools, and with next advance being the introduction of <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2015b\/matlab\/ref\/profile.html\"><tt>profiling<\/tt><\/a> in MATLAB (which you can reach from the EDITOR Toolstrip), we still saw a lot of variability in timing.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2015\/profiler.png\" alt=\"\"> <\/p><p>Some of this variability had to do with details of how the MATLAB execution engine works.  And whether code could be sped up by the JIT or Accelerator (old technology now compared to R2015b!).  Some of the variablity could be alleviated by running the code in functions that MATLAB can interpret and \"compile\" first, and then re-use to run again without having to re-interpret the code.  To smooth out the costs of the first time overhead, we introduced <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2015b\/matlab\/ref\/timeit.html\"><tt>timeit<\/tt><\/a>.<\/p><pre class=\"codeinput\">timeittime = timeit(@() a*a*a*a)\r\n<\/pre><p>So now you can understand the doc excerpt below. While true, it's nice to know why!<\/p><pre>  Although it is possible to measure performance using the cputime\r\n  function, it is recommended that you use the timeit or tic and toc\r\n  functions for this purpose exclusively.<\/pre><pre class=\"codeoutput\">timeittime =\r\n      0.14095\r\n<\/pre><h4>GPU Timing<a name=\"71829eb8-93a5-4ad9-813d-e52ebac774eb\"><\/a><\/h4><p>If you happen to have access to GPUs for your computations, you may want to check out <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2015b\/distcomp\/gputimeit.html\"><tt>gputimeit<\/tt><\/a>.<\/p><h4>Ten Years<a name=\"8ab72d2f-cd01-4e3c-b9e2-ea48bcef045e\"><\/a><\/h4><p>So that's where the time has gone.  Hope yours has been productive!  And have an enjoyable holiday season and happy new year!<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_92f27435eda54ef7b4f0703dd87a3d36() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='92f27435eda54ef7b4f0703dd87a3d36 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 92f27435eda54ef7b4f0703dd87a3d36';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2015 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_92f27435eda54ef7b4f0703dd87a3d36()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2015b<br><\/p><\/div><!--\r\n92f27435eda54ef7b4f0703dd87a3d36 ##### SOURCE BEGIN #####\r\n%% It's About Time!\r\n% Well, it's about that time.  What time?  It turns out that this is my\r\n% 400th post. In 10 years!  It's been a lot of fun for me; and I hope\r\n% you've learnd a lot. So time to talk about time again.\r\n%% Timing before computers\r\n% There are a bunch of ways to time code execution in MATLAB.  Of course,\r\n% you could always use a stopwatch when you start to run some code, and,\r\n% assuming it takes long enough, but not so long that you lose interest,\r\n% you might get reasonable estimate.  However, many computations happen\r\n% very fast on a human timescale, and so we want a way to measure the time\r\n% more accurately.\r\n%% Timing 1.0\r\n% When I started using MATLAB, we performed timings using\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2015b\/matlab\/ref\/tic.html |tic|>\r\n% and <https:\/\/www.mathworks.com\/help\/releases\/R2015b\/matlab\/ref\/toc.html\r\n% |toc|>.  Like this:\r\na = rand(1000);\r\ntic, b = a*a*a*a; elapsedtic = toc\r\n%% Next Advance in Timing\r\n% The good thing about |tic| and |toc| is that they help you measure the\r\n% time you wait before the prompt reappears.  So if your computer tries to\r\n% go do something else while the computation is going on, you may\r\n% incorrectly see a much larger time than the time actually spent on your\r\n% calculation.  To mitigate that, we introduced\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2015b\/matlab\/ref\/cputime.html\r\n% |cputime|>.  In the days of single core machines, the original tic-toc\r\n% times and cputime _should_ have been about the same.  Unless, of course,\r\n% your computer was multitasking majorly in the background.  Then |cputime|\r\n% might be more reflective of the actual computation time required.  But\r\n% even then, we thought |tic| and |toc| were more what people wanted to\r\n% know, and recommended it in the documentation.\r\n\r\nt = cputime; c = a*a*a*a; elapsedcpu = cputime-t\r\n%%\r\n% But wait!  |elapsedcpu| is much larger than |elapsedtic|, by a factor of\r\nratio = elapsedcpu\/elapsedtic\r\n%%%\r\n% What's going on?  Well, I happen to be running this example on a dual\r\n% core machine...  So, for functionality that is multithreaded, meaning\r\n% much of the math in MATLAB, the cputime calculation is likely to appear\r\n% larger by a factor of the number of cores you have access to.\r\n\r\n%% Timing accuracy\r\n% Even with all of these timing tools, and with next advance being the\r\n% introduction of\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2015b\/matlab\/ref\/profile.html\r\n% |profiling|> in MATLAB (which you can reach from the EDITOR Toolstrip),\r\n% we still saw a lot of variability in timing.\r\n%\r\n% <<profiler.png>>\r\n%\r\n% Some of this variability had to do with details of how the MATLAB\r\n% execution engine works.  And whether code could be sped up by the JIT or\r\n% Accelerator (old technology now compared to R2015b!).  Some of the\r\n% variablity could be alleviated by running the code in functions that\r\n% MATLAB can interpret and \"compile\" first, and then re-use to run again\r\n% without having to re-interpret the code.  To smooth out the costs of the\r\n% first time overhead, we introduced\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2015b\/matlab\/ref\/timeit.html\r\n% |timeit|>.\r\ntimeittime = timeit(@() a*a*a*a)\r\n\r\n%%%\r\n% So now you can understand the doc excerpt below.\r\n% While true, it's nice to know why!\r\n% \r\n%    Although it is possible to measure performance using the cputime\r\n%    function, it is recommended that you use the timeit or tic and toc\r\n%    functions for this purpose exclusively.\r\n%\r\n%% GPU Timing\r\n% If you happen to have access to GPUs for your computations, you may want\r\n% to check out\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2015b\/distcomp\/gputimeit.html\r\n% |gputimeit|>.\r\n%% Ten Years\r\n% So that's where the time has gone.  Hope yours has been productive!  And\r\n% have an enjoyable holiday season and happy new year!\r\n\r\n##### SOURCE END ##### 92f27435eda54ef7b4f0703dd87a3d36\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2015\/profiler.png\" onError=\"this.style.display ='none';\" \/><\/div><!--introduction--><p>Well, it's about that time.  What time?  It turns out that this is my 400th post. In 10 years!  It's been a lot of fun for me; and I hope you've learnd a lot. So time to talk about time again.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2015\/12\/23\/its-about-time\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[56,58],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/1279"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=1279"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/1279\/revisions"}],"predecessor-version":[{"id":1281,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/1279\/revisions\/1281"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=1279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=1279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=1279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}