title: Speed improvements of python object creation
body: <p>This question is academic in nature and typically if I had these issues in the real world I'd probably do things differently. However I was curious about whether the speed at which python objects are created could be improved. </p>
  
  <pre><code>#!/usr/bin/env python
  import time
  
  # boring class
  class boring:
      def __init__(self, a, b, c):
          self.a = a
          self.b = b
          self.c = c
  
  def timefunc(f):
      def f_timer(*args, **kwargs):
          start = time.time()
          result = f(*args, **kwargs)
          end = time.time()
          print f.__name__, 'took', end - start, 'time'
          return result
      return f_timer
  
  @timefunc
  def standard_boring():
      for x in range(10000000):
          b = boring(1,2,3)
      del(b)
  
  if __name__ == "__main__":
      standard_boring()
  </code></pre>
  
  <p>On my machine this takes about 4 seconds. So curious what Python is doing I dug deeper using strace.</p>
  
  <p>What I see is many tens of thousands of calls to brk e.g. - </p>
  
  <pre><code>brk(0xe35000)                           = 0xe35000
  brk(0xe34000)                           = 0xe34000
  brk(0xe33000)                           = 0xe33000
  brk(0xe32000)                           = 0xe32000
  brk(0xe31000)                           = 0xe31000
  </code></pre>
  
  <p>Man describes brk as </p>
  
  <blockquote>
    <p>brk() and sbrk() change  the  location  of  the  program  break,  which
           defines  the end of the process's data segment (i.e., the program break
           is the first location after the end of the uninitialized data segment).
           Increasing the program break has the effect of allocating memory to the
           process; decreasing the break deallocates memory.</p>
  </blockquote>
  
  <p>Each call to this presumably some number of cycles, as such the very fact there are loads of calls to this makes me think this is what takes the time.</p>
  
  <p>So is there a way to tell python "Yo, I'm going to be using lots of memory so allocate loads up front". Can I do anything like that? I know why you wouldn't want to, like I said this is academic.</p>
  
  <p>I've experimented with slots, hypothesizing that if I use less memory then this should improve my speed by the very nature of reducing the reoccurring memory allocation requests. I found the results from this quite "lumpy".</p>
  
  <p>Any ideas? </p>