{"id":20813,"date":"2017-02-24T18:18:41","date_gmt":"2017-02-24T18:18:41","guid":{"rendered":"https:\/\/legacy.livecode.com\/?p=20813"},"modified":"2017-02-24T18:18:41","modified_gmt":"2017-02-24T18:18:41","slug":"the-livecode-script-profiler","status":"publish","type":"post","link":"https:\/\/legacy.livecode.com\/the-livecode-script-profiler\/","title":{"rendered":"The LiveCode Script Profiler"},"content":{"rendered":"<p>LiveCode\u2019s new Script Profiler, available in the LiveCode Business edition, can help you identify the most costly lines of code in a stack. \u00a0This can help you optimise your code by identifying inefficiencies.<!--more--><\/p>\n<p>LiveCode\u2019s new Script Profiler, available in the LiveCode Business edition, can help you identify the most costly lines of code in a stack. \u00a0This can help you optimise your code by identifying inefficiencies.<\/p>\n<p>To use the Script Profiler you open a stack, start the profiler and use the stack. When each line of a script in the stack is executed the execution time will be recorded. When the profiler is stopped a report is presented.<\/p>\n<p>In this example we will look at an area that can often benefit from some optimisation, looping over an array. This example just uses a simple array with 10000 values in it. The code loops across the array and prints out all the values stored in the array.<\/p>\n<h2>Open the stack you want to profile<\/h2>\n<p>Open the stack you want to profile, and ensure it is the topStack.<\/p>\n<p>The initial looping code is<\/p>\n<pre><span style=\"color: #000000;\">command arrayLoop \r\n<\/span><span style=\"color: #7f7f00;\">   local<\/span><span style=\"color: #000000;\"> tList\r\n\r\n<\/span><span style=\"color: #7f7f00;\">   put<\/span> <span style=\"color: #7f007f;\">empty<\/span> <span style=\"color: #7f007f;\">into<\/span> <span style=\"color: #7f007f;\">field<\/span> <span style=\"color: #00007f;\">\"list\"\r\n<\/span><span style=\"color: #000000;\">   repeat <\/span><span style=\"color: #7f007f;\">with<\/span><span style=\"color: #000000;\"> x = <\/span><span style=\"color: #00007f;\">1<\/span> <span style=\"color: #7f007f;\">to<\/span> <span style=\"color: #7f007f;\">the<\/span> <span style=\"color: #007f7f;\">number<\/span> <span style=\"color: #7f007f;\">of<\/span> <span style=\"color: #7f007f;\">lines<\/span> <span style=\"color: #7f007f;\">in<\/span> <span style=\"color: #7f007f;\">the<\/span> <span style=\"color: #7f007f;\">keys<\/span> <span style=\"color: #7f007f;\">of<\/span><span style=\"color: #000000;\"> sTestArray\r\n<\/span><span style=\"color: #7f7f00;\">      put<\/span> <span style=\"color: #7f007f;\">line<\/span><span style=\"color: #000000;\"> x <\/span><span style=\"color: #7f007f;\">of<\/span> <span style=\"color: #7f007f;\">the<\/span> <span style=\"color: #7f007f;\">keys<\/span> <span style=\"color: #7f007f;\">of<\/span><span style=\"color: #000000;\"> sTestArray <\/span><span style=\"color: #7f007f;\">into<\/span><span style=\"color: #000000;\"> tKey\r\n<\/span><span style=\"color: #7f7f00;\">      put<\/span><span style=\"color: #000000;\"> sTestArray[tKey] &amp; <\/span><span style=\"color: #7f7f00;\">return<\/span> <span style=\"color: #7f007f;\">after<\/span><span style=\"color: #000000;\"> tList\r\n<\/span><span style=\"color: #000000;\">   end repeat\r\n\r\n<\/span><span style=\"color: #7f7f00;\">   put<\/span><span style=\"color: #000000;\"> tList <\/span><span style=\"color: #7f007f;\">into<\/span> <span style=\"color: #7f007f;\">field<\/span> <span style=\"color: #00007f;\">\"list\"\r\n<\/span><span style=\"color: #000000;\">end arrayLoop<\/span><\/pre>\n<h2>Start the Script Profiler<\/h2>\n<p>Choose &#8216;Start Profiling Scripts&#8217; from the Development menu, this will begin profiling the <a href=\"https:\/\/legacy.livecode.com\/resources\/api\/#livecode_script\/topstack\">topStack<\/a>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-20852 size-full\" src=\"https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler1-1.png\" alt=\"script_profiler1\" width=\"619\" height=\"517\" srcset=\"https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler1-1.png 619w, https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler1-1-300x251.png 300w\" sizes=\"auto, (max-width: 619px) 100vw, 619px\" \/><\/p>\n<h2>Use the Stack<\/h2>\n<p>Starting using the stack to begin the profile, when each line of a script in the stack is executed the execution time will be recorded.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-20853 size-full\" src=\"https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler2-1.png\" alt=\"script_profiler2\" width=\"420\" height=\"443\" srcset=\"https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler2-1.png 420w, https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler2-1-284x300.png 284w\" sizes=\"auto, (max-width: 420px) 100vw, 420px\" \/><\/p>\n<h2>Stop the Script Profiler<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-20854 size-full\" src=\"https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler3-1.png\" alt=\"script_profiler3\" width=\"615\" height=\"516\" srcset=\"https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler3-1.png 615w, https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler3-1-300x252.png 300w\" sizes=\"auto, (max-width: 615px) 100vw, 615px\" \/><\/p>\n<p>To stop the Script Profiler select &#8216;Stop Profiling Scripts&#8217; from the Development menu.<\/p>\n<h2>The Script Profile Report<\/h2>\n<p>When the Script Profiler is stopped a report is shown.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-20855 size-full\" src=\"https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler4-1.png\" alt=\"script_profiler4\" width=\"986\" height=\"692\" srcset=\"https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler4-1.png 986w, https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler4-1-300x211.png 300w, https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler4-1-768x539.png 768w\" sizes=\"auto, (max-width: 986px) 100vw, 986px\" \/><\/p>\n<p>The report shows each line of script that was executed<\/p>\n<ul>\n<li>the line number<\/li>\n<li>number of times the line was called<\/li>\n<li>the time to execute the line<\/li>\n<\/ul>\n<p>The total time in seconds is displayed at the bottom of the report.<\/p>\n<p>Double clicking a line in the report will open the line of script in the Script Editor.<\/p>\n<h1>Optimisation<\/h1>\n<p>Now we can see where all the time is going we know where we want to optimise our code.<\/p>\n<p>In this example the vast majority\u00a0of the time is spent on the line<\/p>\n<pre><span style=\"color: #7f7f00;\">put<\/span> <span style=\"color: #7f007f;\">line<\/span><span style=\"color: #000000;\"> x <\/span><span style=\"color: #7f007f;\">of<\/span> <span style=\"color: #7f007f;\">the<\/span> <span style=\"color: #7f007f;\">keys<\/span> <span style=\"color: #7f007f;\">of<\/span><span style=\"color: #000000;\"> sTestArray <\/span><span style=\"color: #7f007f;\">into<\/span><span style=\"color: #000000;\"> tKey\r\n<\/span><\/pre>\n<p>With a little thought we can see that we can change the form of the repeat loop we are using to something more efficient. We are only really interested in the values stored in the array, not the keys, so we can change the code to loop across the <strong>elements<\/strong> of the array.<\/p>\n<pre><span style=\"color: #000000;\">command arrayLoop \r\n<\/span><span style=\"color: #7f7f00;\">   local<\/span><span style=\"color: #000000;\"> tList\r\n\r\n<\/span><span style=\"color: #7f7f00;\">   put<\/span> <span style=\"color: #7f007f;\">empty<\/span> <span style=\"color: #7f007f;\">into<\/span> <span style=\"color: #7f007f;\">field<\/span> <span style=\"color: #00007f;\">\"list\"\r\n<\/span><span style=\"color: #000000;\">   repeat <\/span><span style=\"color: #7f007f;\">for<\/span> <span style=\"color: #7f007f;\">each<\/span> <span style=\"color: #7f007f;\">element<\/span><span style=\"color: #000000;\"> tValue <\/span><span style=\"color: #7f007f;\">in<\/span><span style=\"color: #000000;\"> sTestArray\r\n<\/span><span style=\"color: #7f7f00;\">      put<\/span><span style=\"color: #000000;\"> tValue &amp; <\/span><span style=\"color: #7f7f00;\">return<\/span> <span style=\"color: #7f007f;\">after<\/span><span style=\"color: #000000;\"> tList\r\n<\/span><span style=\"color: #000000;\">   end repeat\r\n\r\n<\/span><span style=\"color: #7f7f00;\">   put<\/span><span style=\"color: #000000;\"> tList <\/span><span style=\"color: #7f007f;\">into<\/span> <span style=\"color: #7f007f;\">field<\/span> <span style=\"color: #00007f;\">\"list\"\r\n<\/span><span style=\"color: #000000;\">end arrayLoop<\/span><\/pre>\n<p>Now run the Script Profiler again and see how much faster the code is, 0.221 seconds rather than 10.318 seconds.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-20856 size-full\" src=\"https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler5-1.png\" alt=\"script_profiler5\" width=\"985\" height=\"691\" srcset=\"https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler5-1.png 985w, https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler5-1-300x210.png 300w, https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler5-1-768x539.png 768w\" sizes=\"auto, (max-width: 985px) 100vw, 985px\" \/><\/p>\n<h2>Alternative Ways to Start and Stop the Script Profiler<\/h2>\n<p>You can also start and stop the Script Profiler using the stack context menu, opened by ctrl\/right clicking on the stack, or via the stack context menu in the Project Browser.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-20857 size-full\" src=\"https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler6-1.png\" alt=\"script_profiler6\" width=\"874\" height=\"851\" srcset=\"https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler6-1.png 874w, https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler6-1-300x292.png 300w, https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler6-1-768x748.png 768w\" sizes=\"auto, (max-width: 874px) 100vw, 874px\" \/>\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-20858 size-full\" src=\"https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler7-1.png\" alt=\"script_profiler7\" width=\"874\" height=\"750\" srcset=\"https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler7-1.png 874w, https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler7-1-300x257.png 300w, https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler7-1-768x659.png 768w\" sizes=\"auto, (max-width: 874px) 100vw, 874px\" \/><br \/>\n<em>Using the stack context menu<\/em><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-20859 size-full\" src=\"https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler8-1.png\" alt=\"script_profiler8\" width=\"568\" height=\"491\" srcset=\"https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler8-1.png 568w, https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler8-1-300x259.png 300w\" sizes=\"auto, (max-width: 568px) 100vw, 568px\" \/><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-20860 size-full\" src=\"https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler9.png\" alt=\"script_profiler9\" width=\"659\" height=\"496\" srcset=\"https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler9.png 659w, https:\/\/legacy.livecode.com\/wp-content\/uploads\/2017\/02\/script_profiler9-300x226.png 300w\" sizes=\"auto, (max-width: 659px) 100vw, 659px\" \/><br \/>\n<em>Using the Project Browser<\/em><\/p>\n<h2><\/h2>\n<p>Available in LiveCode Business Edition 9.0.0 DP-5 onwards.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>LiveCode\u2019s new Script Profiler, available in the LiveCode Business edition, can help you identify the most costly lines of code in a stack. \u00a0This can help you optimise your code by identifying inefficiencies.<\/p>\n","protected":false},"author":5,"featured_media":20719,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"om_disable_all_campaigns":false,"footnotes":""},"categories":[45],"tags":[160,304,309],"class_list":["post-20813","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-business","tag-livecode-9","tag-script-profiler"],"acf":[],"_links":{"self":[{"href":"https:\/\/legacy.livecode.com\/wp-json\/wp\/v2\/posts\/20813","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/legacy.livecode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/legacy.livecode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/legacy.livecode.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/legacy.livecode.com\/wp-json\/wp\/v2\/comments?post=20813"}],"version-history":[{"count":14,"href":"https:\/\/legacy.livecode.com\/wp-json\/wp\/v2\/posts\/20813\/revisions"}],"predecessor-version":[{"id":20873,"href":"https:\/\/legacy.livecode.com\/wp-json\/wp\/v2\/posts\/20813\/revisions\/20873"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/legacy.livecode.com\/wp-json\/wp\/v2\/media\/20719"}],"wp:attachment":[{"href":"https:\/\/legacy.livecode.com\/wp-json\/wp\/v2\/media?parent=20813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/legacy.livecode.com\/wp-json\/wp\/v2\/categories?post=20813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/legacy.livecode.com\/wp-json\/wp\/v2\/tags?post=20813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}