embedded Python - optimization for creating/destroying many small objects

Skip Montanaro skip at pobox.com
Tue Jun 25 00:39:13 CEST 2002


    Paul> I would like to simply create ONE of these objects and reuse it
    Paul> for the entire loop, so I am not paying for the memory
    Paul> allocation/deallocation overhead.

Define your object to have something akin to a __setstate__ method, create a
single instance of it before the loop, then set its state during each pass
of the loop:

    holder = MyContainer()
    while 1:
        holder.setstate(data)
        dosomething(holder)

-- 
Skip Montanaro
skip at pobox.com
consulting: http://manatee.mojam.com/~skip/resume.html





More information about the Python-list mailing list