{"id":644,"date":"2014-10-04T18:57:40","date_gmt":"2014-10-04T18:57:40","guid":{"rendered":"https:\/\/block.arch.ethz.ch\/blog\/?p=644"},"modified":"2014-10-04T19:00:52","modified_gmt":"2014-10-04T19:00:52","slug":"profile-your-code","status":"publish","type":"post","link":"https:\/\/block.arch.ethz.ch\/blog\/2014\/10\/profile-your-code\/","title":{"rendered":"Profile your code"},"content":{"rendered":"<p>Research in the field of Structural Design is more about developing concepts than implementations. If a concept is sound, the details of the implementation can be figured out later, by someone with more computational and\/or programming experience.<\/p>\n<p>Of course, while developing concepts and approaches, we need some kind of implementation to test and prove they actually work. And, let&#8217;s face it, we all like writing reasonably fast and robust implementations. Therefore, at one point or another, we all rewrite code to optimise and make it run faster.<\/p>\n<p>In my experience, the slowest part of your code is almost never the part you imagined. Profiling your code before optimising will save you a lot of work.<\/p>\n<p><!--more--><\/p>\n<p>Profiling Python code with the Python profilers is easy. A detailed explanation can be found <a href=\"https:\/\/docs.python.org\/2\/library\/profile.html\" target=\"_blank\">here<\/a>. Below is a recipe that always worked well for me.<\/p>\n<pre class=\"brush: py\">\r\nimport cStringIO, cProfile, pstats\r\n\r\nprofile = cProfile.Profile()\r\nprofile.enable()\r\n\r\n# run your code here \r\n\r\nprofile.disable()\r\ns = cStringIO.StringIO()\r\nstats = pstats.Stats(profile, stream=s)\r\nstats.strip_dirs()\r\nstats.sort_stats(1)\r\nstats.print_stats(20)\r\n\r\nprint s.get_value()\r\n<\/pre>\n<p>If you run the profile as part of a standalone script and you want to provide feedback on what went wrong during a failed execution, you can use the traceback module.<\/p>\n<pre class=\"brush: py\">\r\nimport cStringIO, cProfile, pstats\r\nimport traceback\r\n\r\ntry:\r\n    profile = cProfile.Profile()\r\n    profile.enable()\r\n    \r\n    # run your code here \r\n    \r\n    profile.disable()\r\n    s = cStringIO.StringIO()\r\n    stats = pstats.Stats(profile, stream=s)\r\n    stats.strip_dirs()\r\n    stats.sort_stats(1)\r\n    stats.print_stats(20)\r\n    \r\n    print s.get_value()\r\nexcept:\r\n    print traceback.format_exc()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Research in the field of Structural Design is more about developing concepts than implementations. If a concept is sound, the details of the implementation can be figured out later, by someone with more computational and\/or programming experience. Of course, while developing concepts and approaches, we need some kind of implementation to test and prove they [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-644","post","type-post","status-publish","format-standard","hentry","category-code"],"_links":{"self":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts\/644","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/comments?post=644"}],"version-history":[{"count":8,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts\/644\/revisions"}],"predecessor-version":[{"id":652,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts\/644\/revisions\/652"}],"wp:attachment":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/media?parent=644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/categories?post=644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/tags?post=644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}