{"id":2138,"date":"2008-03-28T08:31:56","date_gmt":"2008-03-28T13:31:56","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2008\/03\/28\/timeit-benchmarking-function\/"},"modified":"2017-06-07T22:21:59","modified_gmt":"2017-06-08T02:21:59","slug":"timeit-benchmarking-function","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2008\/03\/28\/timeit-benchmarking-function\/","title":{"rendered":"TIMEIT Benchmarking Function"},"content":{"rendered":"<div class=\"content\">\r\n\r\n<i>You may have noticed some recent changes in the format of this blog. Here\u2019s what to expect on a regular basis \u2013 two topics\r\nper week.<\/i>\r\n\r\n&nbsp;\r\n<div>\r\n<ul>\r\n\t<li><i>On Tuesdays Doug will provide MATLAB tutorials.<\/i><\/li>\r\n\t<li><i>On Fridays guest bloggers Jiro, Brett and Bob will highlight File Exchange submissions.<\/i><\/li>\r\n<\/ul>\r\n<\/div>\r\nBob's pick this week is <tt>timeit<\/tt> by Steve Eddins.\r\n\r\nIf you follow his weekly blog <a href=\"https:\/\/blogs.mathworks.com\/steve\/\">Steve on Image Processing<\/a> you may have already seen this recent leap day post, <a href=\"https:\/\/blogs.mathworks.com\/steve\/2008\/02\/29\/timing-code-in-matlab\/\">Timing code in MATLAB<\/a>. If you haven't read it yet and care about MATLAB performance, check it out. Steve's explanation is nice. He mentions other\r\nMATLAB performance resources. And like all good M-file submissions it includes examples like this.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">A = rand(12000, 400);\r\nB = rand(400, 12000);\r\nf = @() sum(A.' .* B, 1);\r\ntimeit(f)<\/pre>\r\n<pre style=\"font-style: oblique;\">ans =\r\n      0.14863\r\n<\/pre>\r\nWhat I love about <tt>timeit<\/tt> is what it does automatically so I don't have to think about. It warms up the code, decides how many times to repeat the\r\noperation, accounts for overhead and determines how long the operation took using statistics. Thank you Steve!\r\n\r\nDo you have a favorite tip for measuring performance to share?\r\n\r\n<script>\/\/ <![CDATA[\r\nfunction grabCode_08cc0c9d626f42efa22e3876f4533660() {\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='08cc0c9d626f42efa22e3876f4533660 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 08cc0c9d626f42efa22e3876f4533660';\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        author = 'Robert Bemis';\r\n        copyright = 'Copyright 2008 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('\r\n\r\n<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\r\n\r\n\r\n\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }\r\n\/\/ ]]><\/script>\r\n<p style=\"text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;\">\r\n<a><span style=\"font-size: x-small; font-style: italic;\">Get\r\nthe MATLAB code\r\n<noscript>(requires JavaScript)<\/noscript><\/span><\/a>\r\n\r\nPublished with MATLAB\u00ae 7.6<\/p>\r\n\r\n<\/div>\r\n<!--\r\n08cc0c9d626f42efa22e3876f4533660 ##### SOURCE BEGIN #####\r\n%%\r\n% _You may have noticed some recent changes in the format of this blog. Here\u00e2\u20ac\u2122s\r\n% what to expect on a regular basis \u00e2\u20ac\u201c two topics per week._\r\n%%\r\n% * _On Tuesdays Doug will provide MATLAB tutorials._\r\n% * _On Fridays guest bloggers Jiro, Brett and Bob will highlight File\r\n% Exchange submissions._\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadAuthor.do?objectId=969735&objectType=author % Bob>'s pick this week is\r\n% |<https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadFile.do?objectId=18798&objectType=file % timeit>| by Steve Eddins.\r\n%%\r\n% If you follow his weekly blog\r\n% <https:\/\/blogs.mathworks.com\/steve\/ Steve on Image Processing> you may have\r\n% already seen this recent leap day post,\r\n% <https:\/\/blogs.mathworks.com\/steve\/2008\/02\/29\/timing-code-in-matlab\/ Timing % code in MATLAB>. If you haven't read it yet and care about MATLAB\r\n% performance, check it out. Steve's explanation is nice. He mentions other\r\n% MATLAB performance resources. And like all good M-file submissions it\r\n% includes examples like this.\r\nA = rand(12000, 400);\r\nB = rand(400, 12000);\r\nf = @() sum(A.' .* B, 1);\r\ntimeit(f)\r\n%%\r\n% What I love about |timeit| is what it does automatically so I don't have to\r\n% think about. It warms up the code, decides how many times to repeat the\r\n% operation, accounts for overhead and determines how long the operation took\r\n% using statistics. Thank you Steve!\r\n%%\r\n% Do you have a favorite tip for measuring performance to share?\r\n\r\n##### SOURCE END ##### 08cc0c9d626f42efa22e3876f4533660\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n\r\nYou may have noticed some recent changes in the format of this blog. Here\u2019s what to expect on a regular basis \u2013 two topics\r\nper week.\r\n\r\n&nbsp;\r\n\r\n\r\n\tOn Tuesdays Doug will provide MATLAB... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2008\/03\/28\/timeit-benchmarking-function\/\">read more >><\/a><\/p>","protected":false},"author":46,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2138"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/46"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2138"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2138\/revisions"}],"predecessor-version":[{"id":8641,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2138\/revisions\/8641"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}