__delitem__ affecting performance

2006-10-19 Thread Karl H.
Hi,

I was performing some timing tests on a class that inherits from the 
built-in list, and got some curious results:

import timeit

class MyList(list):
 def __init__(self):
 list.__init__(self)
 self[:] = [0,0,0]

 def __delitem__(self,index):
 print 'deleting'

ml = MyList()

def test():
 global ml
 ml[0] += 0
 ml[1] += 0
 ml[2] += 0

t = timeit.Timer("test()","from __main__ import test")
print t.timeit()

 >> 4.11651382676

import timeit

class MyList(list):
 def __init__(self):
 list.__init__(self)
 self[:] = [0,0,0]

ml = MyList()

def test():
 global ml
 ml[0] += 0
 ml[1] += 0
 ml[2] += 0

t = timeit.Timer("test()","from __main__ import test")
print t.timeit()

 >> 2.23268591383

Does anybody know why defining __delitem__ is causing the code to run 
slower? It is not being called, so I don't see why it would affect 
performance. Overriding other sequence operators like __delslice__ does 
not exhibit this behavior.

The speed difference doesn't really bother me, but I am curious.

I used Python 2.4 for this test.

-Karl
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyThreadState_SetAsyncExc (nThreadId ??????, exc);

2006-12-14 Thread Karl H.
iwl wrote:
> what is the nThreadId-Parameter of PyThreadState_SetAsyncExc?
> 
> I try to implement a Terminate-Button in my C-Prog for my
> embedded Python, but its hard to find an example how to
> stop an interpreter running in an thread.
> 
> I found no other Python C-App-Func returning such a parameter.
> 

Use the "thread_id" member of the PyThreadState object:

PyThreadState *tstate;

PyThreadState_SetAsyncExc(tstate->thread_id,exc);

-Karl
-- 
http://mail.python.org/mailman/listinfo/python-list