{"id":218,"date":"2016-08-15T21:35:04","date_gmt":"2016-08-15T11:35:04","guid":{"rendered":"http:\/\/alrichardson.com\/?p=218"},"modified":"2016-08-15T21:35:14","modified_gmt":"2016-08-15T11:35:14","slug":"profiling-code","status":"publish","type":"post","link":"https:\/\/alrichardson.com\/?p=218","title":{"rendered":"Profiling code."},"content":{"rendered":"<p>I&#8217;ve been wondering lately, when it comes to finding nodes in a scene, nodes which may or may no exist, which will be faster?<\/p>\n<p>using a Try, Except?<br \/>\nor using maya&#8217;s built in objExists command?<\/p>\n<p>My theory was that the Try, Except would be faster as objExists would have to loop over all nodes in the scene. At least as far as I assume.<br \/>\nSo i put it to the test.<\/p>\n<p>The first thing I neede ws a profiler. A little googling and I turned up Pyhtons Timeit module.<\/p>\n<p>This can be imported using<\/p>\n<pre class=\"lang:python decode:true \" >\r\nimport timeit\r\n\r\nprint timeit.timeit(function)\r\n<\/pre>\n<p>This will print the result of running the specified function one milling times. Its is run this many times to ensure consistency in the results.<br \/>\nThat can be reduced by adding the number flag, to specify how many times the code is run, so:<\/p>\n<pre class=\"lang:python decode:true \" >\r\nimport timeit\r\n\r\nprint timeit.timeit(function, number = 10000)\r\n<\/pre>\n<p>This will run the code ten thousand times. Its quicker and consistant enough for my requirements.<\/p>\n<p>Getting into the problem at hand, I setup two functions, one as a Try, Except and another as objExists.<br \/>\nI ran each one in an empty scene and each one in a scene which included a complex character rig.<br \/>\nThe code and results are below:<\/p>\n<pre class=\"lang:python decode:true \" >import maya.cmds as cmds\r\nimport timeit\r\n\r\ndef exists():\r\n    found = cmds.objExists('locator1')\r\n    return found\r\n\r\ndef select():\r\n    found = False\r\n    try:\r\n        cmds.select('locator1')\r\n        found = True\r\n    except:\r\n        found = False\r\n    return found\r\n\r\nprint timeit.timeit(select, number = 10000)<\/pre>\n<p>The results <\/p>\n<p>The Try, Except took:<br \/>\n0.398722180715 in an empty scene and<br \/>\n0.376243066404 in a complex rig scene<\/p>\n<p>where as the objExists took<br \/>\n0.0698589508479 in an empty scene and<br \/>\n0.0415636909748 in a complex rig scene<\/p>\n<p>astonishing, and, contrary to my expectations, the Try, Except took nearly ten fold longer than the objExists!<br \/>\nFrom this I learned: Using specific commands looks to be faster than using Try, Except! I&#8217;ll have to test more commands and find out :)<br \/>\nIf you&#8217;d like to try this code yourself, you can see the result for each funstion by changing this line:<\/p>\n<pre class=\"lang:python decode:true \" >\r\nprint timeit.timeit(select, number = 10000)<\/pre>\n<p>To this:<\/p>\n<pre class=\"lang:python decode:true \" >\r\nprint timeit.timeit(exists, number = 10000)<\/pre>\n<p>Cheers!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been wondering lately, when it comes to finding nodes in a scene, nodes which may or may no exist, which will be faster? using a Try, Except? or using maya&#8217;s built in objExists command? My theory was that the Try, Except would be faster as objExists would have to loop over all nodes in [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-218","post","type-post","status-publish","format-standard","hentry","category-maya"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/alrichardson.com\/index.php?rest_route=\/wp\/v2\/posts\/218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alrichardson.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alrichardson.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alrichardson.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/alrichardson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=218"}],"version-history":[{"count":2,"href":"https:\/\/alrichardson.com\/index.php?rest_route=\/wp\/v2\/posts\/218\/revisions"}],"predecessor-version":[{"id":220,"href":"https:\/\/alrichardson.com\/index.php?rest_route=\/wp\/v2\/posts\/218\/revisions\/220"}],"wp:attachment":[{"href":"https:\/\/alrichardson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alrichardson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alrichardson.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}