{"id":470,"date":"2011-06-28T12:57:36","date_gmt":"2011-06-28T12:57:36","guid":{"rendered":"https:\/\/blogs.mathworks.com\/desktop\/2011\/06\/28\/speed-up-your-guis-with-profiling\/"},"modified":"2011-06-28T12:57:36","modified_gmt":"2011-06-28T12:57:36","slug":"speed-up-your-guis-with-profiling","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2011\/06\/28\/speed-up-your-guis-with-profiling\/","title":{"rendered":"Speed Up Your GUIs With Profiling"},"content":{"rendered":"<p>I bet most of the people that use the MATLAB Profiler run their code through the little &#8220;Run This Code&#8221; box at the top of the Profiler UI. When you do this the profiling happens only for that one function call, and the data is returned immediately. This is fine for testing a script or function, but when you do this for a GUI function, you will only profile the GUI&#8217;s set-up. Unless it takes a long time for it load, this is generally not as useful as profiling the callbacks associated with the GUI.<\/p>\n<p>In order to profile a GUI&#8217;s callbacks you need to start the Profiler from the command line before you do the interesting work, using the command:<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">profile <span style=\"color: #A020F0\">on<\/span><\/pre>\n<p>Once the profiler is running, you can interact with the GUI to run its callbacks. Then, when you&#8217;re ready to stop profiling and view the results, execute the command:<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">profile <span style=\"color: #A020F0\">viewer<\/span><\/pre>\n<p>In following example I created a new GUI using GUIDE&#8217;s &#8220;GUI with Axes and Menu&#8221; template. <\/p>\n<div align=\"center\">\n<img decoding=\"async\" border=\"0\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/mike_katz_speed_up_gui_with_profiling\/gui_with_axes_and_menu.png\" alt=\"GUI with Axes and Menu\">\n<\/div>\n<p>I then opened the Profiler from the Desktop -> Profiler menu. You can see here when I run the &#8220;<tt>profile_gui<\/tt>&#8221; function directly in the Profiler, all the function calls involved in setting up the GUI are listed. In particular at top is the GUIDE&#8217;s helper function <tt>gui_mainfcn<\/tt> and my function <tt>profile_gui<\/tt>.<\/p>\n<div align=\"center\">\n<img decoding=\"async\" border=\"0\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/mike_katz_speed_up_gui_with_profiling\/profile_mainfcn.png\" alt=\"Profiler on just GUI creation\">\n<\/div>\n<p> The whole process took about 1\/4 seconds, so it&#8217;s probably not worth optimizing. Instead, let&#8217;s look into what happens when the &#8220;Update&#8221; button&#8217;s callback is run. To do that, I&#8217;ve typed &#8220;<tt>profile on<\/tt>&#8221; and then started up the GUI. Then I pressed the button a few times followed by a &#8220;<tt>profile viewer<\/tt>.&#8221; From this view we can see <tt>profile_gui<\/tt> and <tt>gui_mainfcn<\/tt> again, as well as my <tt>pushbutton1_Callback<\/tt> and some of the functions called through the callback. According to the &#8220;Calls&#8221; column, you can see I pressed the button 7 times. Interestingly, you can see that the main function was called 12 times. This is because a GUIDE GUI calls back into itself for its various callbacks and set-up routines. <\/p>\n<div align=\"center\">\n<img decoding=\"async\" border=\"0\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/mike_katz_speed_up_gui_with_profiling\/callback_profiled.png\" alt=\"Profile results after callback\">\n<\/div>\n<p>From the top Profiler page we can step into the callback function itself. From the time by line chart, we can see that even though it called <tt>bar<\/tt> only once, it was the longest command to run. The next longest command was <tt>surf<\/tt> but from the Calls column, you can see I ran that one four times. <\/p>\n<div align=\"center\">\n<img decoding=\"async\" border=\"0\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/mike_katz_speed_up_gui_with_profiling\/callback_results.png\" alt=\"Profile results after callback\">\n<\/div>\n<p>At the bottom of the Profiler screen, we can take a look at this same information overlaid on top of the code, by looking at the code by time or number of calls. The first screen capture is the code highlighted by the relative amount of time, and the second by relative number calls.<\/p>\n<div align=\"center\">\n<img decoding=\"async\" border=\"0\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/mike_katz_speed_up_gui_with_profiling\/lines_by_time.png\" alt=\"Profile results by time\">\n<\/div>\n<div align=\"center\">\n<img decoding=\"async\" border=\"0\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/mike_katz_speed_up_gui_with_profiling\/lines_by_numcalls.png\" alt=\"Profile results by number of calls\">\n<\/div>\n<p>If my code took longer, I would use this information to improve its performance. For example, if turned out that that <tt>bar<\/tt> was called many times, I might cache the results (since the data doesn&#8217;t change). <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I bet most of the people that use the MATLAB Profiler run their code through the little &#8220;Run This Code&#8221; box at the top of the Profiler UI. When you do this the profiling happens only for&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2011\/06\/28\/speed-up-your-guis-with-profiling\/\">read more >><\/a><\/p>\n","protected":false},"author":38,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[33,17,132,69],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/470"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/comments?post=470"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/470\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}