title: Why is os.mkdir() slower when called explicitly?
body: <p>I have been working a project which has to create a large directory structure. My first solution was to keep a dict of all the dirs which exist and if it comes across one which has not been made use os.makedirs() to create it and any missing intermediaries.
  When I profiled this code I found that a huge majority of the time (105 out of 132 seconds) was spent calling posix.stat() to determine that intermediary directories did not exist. However I am building this entire structure in an empty directory so I already knew that none of the intermediate directories would exist.</p>
  
  <p>In order to leverage this I wrote a version of the code which keeps and internal memo describing the structure of the directory tree so that it can determine which directories have been created without querying the os:</p>
  
  <pre><code>class DirTree:
    def __init__(self, root):
      self.root = os.path.abspath(root)
      self.tree = {}
    def makedirs(self, path):
      relpath = os.path.relpath(path, self.root).replace('\\', '/')
      built = self.root
      node = self.tree
      for directory in relpath.split('/'):
          built = os.path.join(built, directory)
          if directory in node:
              node = node[directory]
          else:
              node[directory] = {}
              node = node[directory]
              os.mkdir(built, 0777)
  </code></pre>
  
  <p>This code does run faster, however when I run it through the profiler the same 4068 calls to os.mkdir() now take 4 times longer (94s instead of 24s). It don't understand why this function takes longer when it's called form my function than when it is called by os.makedirs().
  Anybody have an idea why?</p>