{"id":893,"date":"2013-09-30T18:31:43","date_gmt":"2013-09-30T22:31:43","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=893"},"modified":"2019-11-01T09:25:11","modified_gmt":"2019-11-01T13:25:11","slug":"timeit-makes-it-into-matlab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2013\/09\/30\/timeit-makes-it-into-matlab\/","title":{"rendered":"timeit makes it into MATLAB"},"content":{"rendered":"<div class=\"content\"><p>This is my first blog post with \"Published with MATLAB R2013b\" at the bottom. The latest MATLAB release shipped earlier in September. And, for the first time in a while, a function that I wrote has made it into MATLAB.<\/p><p>Back in 2008, I spent some time trying to incorporate performance benchmarking lessons I learned from <a href=\"https:\/\/www.mathworks.com\/company\/aboutus\/founders\/clevemoler.html\">Cleve Moler<\/a> and <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/117137\">Bill McKeeman<\/a> into a general-purpose function for accurately measuring the \"typical\" running time of MATLAB functions or expressions. The result was <tt>timeit<\/tt>, which I submitted to the File Exchange.<\/p><p>Well, I'm happy to say that <tt>timeit<\/tt> has made it into MATLAB in the R2013b release! The MATLAB development team took the code from the File Exchange, made some minor refinements, did some additional testing, wrote some nice <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/timeit.html\">doc<\/a>, and got it into the release. (Thanks, everyone!)<\/p><p>Here are a couple of examples. The first one is just MATLAB, but the second one requires Image Processing Toolbox. (Note that it's helpful to know a little about <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/anonymous-functions.html\">anonymous functions<\/a> in order to use <tt>timeit<\/tt>.)<\/p><p>How much time does it take to compute sum(A.' .* B, 1), where A is 12000-by-400 and B is 400-by-12000?<\/p><pre class=\"codeinput\">A = rand(12000,400);\r\nB = rand(400,12000);\r\nf = @() sum(A.' .* B, 1);\r\ntimeit(f)\r\n<\/pre><pre class=\"codeoutput\">\r\nans =\r\n\r\n    0.0397\r\n\r\n<\/pre><p>How much time does it take to dilate the text.png image with a 25-by-25 all-ones structuring element?<\/p><pre class=\"codeinput\">bw = imread(<span class=\"string\">'text.png'<\/span>);\r\nse = strel(ones(25,25));\r\ng = @() imdilate(bw,se);\r\ntimeit(g)\r\n<\/pre><pre class=\"codeoutput\">\r\nans =\r\n\r\n   7.7779e-04\r\n\r\n<\/pre><p>Loren also <a href=\"https:\/\/blogs.mathworks.com\/loren\/2013\/09\/22\/timing-code\/\">wrote about <tt>timeit<\/tt><\/a> recently in her Art of MATLAB blog.<\/p><p>There are other goodies in R2013b that I'll write about soon.<\/p><p>Have any readers updated to R2013b yet? Any comments?<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_5f7b34044a4d4a28ab33b6b24ff09e10() {\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='5f7b34044a4d4a28ab33b6b24ff09e10 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 5f7b34044a4d4a28ab33b6b24ff09e10';\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 2013 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_5f7b34044a4d4a28ab33b6b24ff09e10()\"><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; R2013b<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2013b<br><\/p><\/div><!--\r\n5f7b34044a4d4a28ab33b6b24ff09e10 ##### SOURCE BEGIN #####\r\n%%\r\n% This is my first blog post with \"Published with MATLAB R2013b\" at the\r\n% bottom. The latest MATLAB release shipped earlier in September. And, for\r\n% the first time in a while, a function that I wrote has made it into MATLAB.\r\n%\r\n% Back in 2008, I spent some time trying to incorporate performance\r\n% benchmarking lessons I learned from\r\n% <https:\/\/www.mathworks.com\/company\/aboutus\/founders\/clevemoler.html Cleve\r\n% Moler> and\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/117137 Bill\r\n% McKeeman> into a general-purpose function for accurately measuring the\r\n% \"typical\" running time of MATLAB functions or expressions. The result was\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/18798-timeit-benchmarking-function \r\n% |timeit|>, which I submitted to the File Exchange.\r\n%\r\n% Well, I'm happy to say that |timeit| has made it into MATLAB in the\r\n% R2013b release! The MATLAB development team took the code from the File\r\n% Exchange, made some minor refinements, did some additional testing, wrote\r\n% some nice <https:\/\/www.mathworks.com\/help\/matlab\/ref\/timeit.html doc>, and\r\n% got it into the release. (Thanks, everyone!)\r\n%\r\n% Here are a couple of examples. The first one is just MATLAB, but the\r\n% second one requires Image Processing Toolbox. (Note that it's helpful to\r\n% know a little about\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/anonymous-functions.html\r\n% anonymous functions> in order to use |timeit|.)\r\n%\r\n% How much time does it take to compute sum(A.' .* B, 1), where A is\r\n% 12000-by-400 and B is 400-by-12000? \r\n\r\nA = rand(12000,400);\r\nB = rand(400,12000);\r\nf = @() sum(A.' .* B, 1);\r\ntimeit(f)\r\n\r\n%%\r\n% How much time does it take to dilate the text.png image with a 25-by-25\r\n% all-ones structuring element?\r\n\r\nbw = imread('text.png');\r\nse = strel(ones(25,25));\r\ng = @() imdilate(bw,se);\r\ntimeit(g)\r\n\r\n%%\r\n% Loren also <https:\/\/blogs.mathworks.com\/loren\/2013\/09\/22\/timing-code\/\r\n% wrote about |timeit|> recently in her Art of MATLAB blog.\r\n%\r\n% There are other goodies in R2013b that I'll write about soon.\r\n%\r\n% Have any readers updated to R2013b yet? Any comments?\r\n\r\n##### SOURCE END ##### 5f7b34044a4d4a28ab33b6b24ff09e10\r\n-->","protected":false},"excerpt":{"rendered":"<p>This is my first blog post with \"Published with MATLAB R2013b\" at the bottom. The latest MATLAB release shipped earlier in September. And, for the first time in a while, a function that I wrote has... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2013\/09\/30\/timeit-makes-it-into-matlab\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[124,76,288,460,106,126,474],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/893"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=893"}],"version-history":[{"count":6,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/893\/revisions"}],"predecessor-version":[{"id":2627,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/893\/revisions\/2627"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}