{"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":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.9 - aioseo.com -->\n\t<meta name=\"description\" content=\"I&#039;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&#039;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\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Alastair\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/alrichardson.com\/?p=218\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.9\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Alastair R | Technical Artist \/ Technical Animator \/ Problem Solver \/ Workflow Builder\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Profiling code. | Alastair R\" \/>\n\t\t<meta property=\"og:description\" content=\"I&#039;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&#039;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\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/alrichardson.com\/?p=218\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2016-08-15T11:35:04+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2016-08-15T11:35:14+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Profiling code. | Alastair R\" \/>\n\t\t<meta name=\"twitter:description\" content=\"I&#039;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&#039;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\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/alrichardson.com\\\/?p=218#article\",\"name\":\"Profiling code. | Alastair R\",\"headline\":\"Profiling code.\",\"author\":{\"@id\":\"https:\\\/\\\/alrichardson.com\\\/?author=2#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/alrichardson.com\\\/#organization\"},\"datePublished\":\"2016-08-15T21:35:04+10:00\",\"dateModified\":\"2016-08-15T21:35:14+10:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/alrichardson.com\\\/?p=218#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/alrichardson.com\\\/?p=218#webpage\"},\"articleSection\":\"Maya\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/alrichardson.com\\\/?p=218#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/alrichardson.com#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/alrichardson.com\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/alrichardson.com\\\/?cat=3#listItem\",\"name\":\"Maya\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/alrichardson.com\\\/?cat=3#listItem\",\"position\":2,\"name\":\"Maya\",\"item\":\"https:\\\/\\\/alrichardson.com\\\/?cat=3\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/alrichardson.com\\\/?p=218#listItem\",\"name\":\"Profiling code.\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/alrichardson.com#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/alrichardson.com\\\/?p=218#listItem\",\"position\":3,\"name\":\"Profiling code.\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/alrichardson.com\\\/?cat=3#listItem\",\"name\":\"Maya\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/alrichardson.com\\\/#organization\",\"name\":\"Alastair R\",\"description\":\"Technical Artist \\\/ Technical Animator \\\/ Problem Solver \\\/ Workflow Builder\",\"url\":\"https:\\\/\\\/alrichardson.com\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/alrichardson.com\\\/?author=2#author\",\"url\":\"https:\\\/\\\/alrichardson.com\\\/?author=2\",\"name\":\"Alastair\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/alrichardson.com\\\/?p=218#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/686377a6142e92739b67c0dce993cfd6876af9d2f22fbd49f75483da7fe629d9?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Alastair\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/alrichardson.com\\\/?p=218#webpage\",\"url\":\"https:\\\/\\\/alrichardson.com\\\/?p=218\",\"name\":\"Profiling code. | Alastair R\",\"description\":\"I'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'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\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/alrichardson.com\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/alrichardson.com\\\/?p=218#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/alrichardson.com\\\/?author=2#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/alrichardson.com\\\/?author=2#author\"},\"datePublished\":\"2016-08-15T21:35:04+10:00\",\"dateModified\":\"2016-08-15T21:35:14+10:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/alrichardson.com\\\/#website\",\"url\":\"https:\\\/\\\/alrichardson.com\\\/\",\"name\":\"Alastair R\",\"description\":\"Technical Artist \\\/ Technical Animator \\\/ Problem Solver \\\/ Workflow Builder\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/alrichardson.com\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Profiling code. | Alastair R","description":"I'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'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","canonical_url":"https:\/\/alrichardson.com\/?p=218","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/alrichardson.com\/?p=218#article","name":"Profiling code. | Alastair R","headline":"Profiling code.","author":{"@id":"https:\/\/alrichardson.com\/?author=2#author"},"publisher":{"@id":"https:\/\/alrichardson.com\/#organization"},"datePublished":"2016-08-15T21:35:04+10:00","dateModified":"2016-08-15T21:35:14+10:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/alrichardson.com\/?p=218#webpage"},"isPartOf":{"@id":"https:\/\/alrichardson.com\/?p=218#webpage"},"articleSection":"Maya"},{"@type":"BreadcrumbList","@id":"https:\/\/alrichardson.com\/?p=218#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/alrichardson.com#listItem","position":1,"name":"Home","item":"https:\/\/alrichardson.com","nextItem":{"@type":"ListItem","@id":"https:\/\/alrichardson.com\/?cat=3#listItem","name":"Maya"}},{"@type":"ListItem","@id":"https:\/\/alrichardson.com\/?cat=3#listItem","position":2,"name":"Maya","item":"https:\/\/alrichardson.com\/?cat=3","nextItem":{"@type":"ListItem","@id":"https:\/\/alrichardson.com\/?p=218#listItem","name":"Profiling code."},"previousItem":{"@type":"ListItem","@id":"https:\/\/alrichardson.com#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/alrichardson.com\/?p=218#listItem","position":3,"name":"Profiling code.","previousItem":{"@type":"ListItem","@id":"https:\/\/alrichardson.com\/?cat=3#listItem","name":"Maya"}}]},{"@type":"Organization","@id":"https:\/\/alrichardson.com\/#organization","name":"Alastair R","description":"Technical Artist \/ Technical Animator \/ Problem Solver \/ Workflow Builder","url":"https:\/\/alrichardson.com\/"},{"@type":"Person","@id":"https:\/\/alrichardson.com\/?author=2#author","url":"https:\/\/alrichardson.com\/?author=2","name":"Alastair","image":{"@type":"ImageObject","@id":"https:\/\/alrichardson.com\/?p=218#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/686377a6142e92739b67c0dce993cfd6876af9d2f22fbd49f75483da7fe629d9?s=96&d=mm&r=g","width":96,"height":96,"caption":"Alastair"}},{"@type":"WebPage","@id":"https:\/\/alrichardson.com\/?p=218#webpage","url":"https:\/\/alrichardson.com\/?p=218","name":"Profiling code. | Alastair R","description":"I'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'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","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/alrichardson.com\/#website"},"breadcrumb":{"@id":"https:\/\/alrichardson.com\/?p=218#breadcrumblist"},"author":{"@id":"https:\/\/alrichardson.com\/?author=2#author"},"creator":{"@id":"https:\/\/alrichardson.com\/?author=2#author"},"datePublished":"2016-08-15T21:35:04+10:00","dateModified":"2016-08-15T21:35:14+10:00"},{"@type":"WebSite","@id":"https:\/\/alrichardson.com\/#website","url":"https:\/\/alrichardson.com\/","name":"Alastair R","description":"Technical Artist \/ Technical Animator \/ Problem Solver \/ Workflow Builder","inLanguage":"en-US","publisher":{"@id":"https:\/\/alrichardson.com\/#organization"}}]},"og:locale":"en_US","og:site_name":"Alastair R | Technical Artist \/ Technical Animator \/ Problem Solver \/ Workflow Builder","og:type":"article","og:title":"Profiling code. | Alastair R","og:description":"I'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'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","og:url":"https:\/\/alrichardson.com\/?p=218","article:published_time":"2016-08-15T11:35:04+00:00","article:modified_time":"2016-08-15T11:35:14+00:00","twitter:card":"summary","twitter:title":"Profiling code. | Alastair R","twitter:description":"I'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'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"},"aioseo_meta_data":{"post_id":"218","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[],"defaultGraph":"","defaultPostTypeGraph":""},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"location":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2020-12-21 05:24:41","updated":"2025-06-04 06:10:45","seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/alrichardson.com\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/alrichardson.com\/?cat=3\" title=\"Maya\">Maya<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tProfiling code.\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/alrichardson.com"},{"label":"Maya","link":"https:\/\/alrichardson.com\/?cat=3"},{"label":"Profiling code.","link":"https:\/\/alrichardson.com\/?p=218"}],"_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}]}}