jeremito kirjoitti: > On Feb 7, 8:28 am, "jeremito" <[EMAIL PROTECTED]> wrote: >> On Feb 6, 5:10 pm, Bruno Desthuilliers >> >> >> >> <[EMAIL PROTECTED]> wrote: >>> jeremito a écrit : >>> > On Feb 6, 2:36 pm, Bruno Desthuilliers > <[EMAIL PROTECTED]> wrote: >>> (snip) >>> >>Here's an alternative implementation, so you get the idea. >>> >>class Xs(dict): >>> oops ! I meant: >>> class Xs(object): >>> of course... >>> (snip) >>>> I guess I just >>>> need more experience. >>> Possibly - but not only. You may want to have a look at the >>> FineManual(tm) for all this kind of "magic", starting with >>> :http://docs.python.org/ref/specialnames.htmlhttp://docs.python.org/re... >>> HTH >> Thanks again! Sometimes the problem is simply not knowing where to >> find the documentation, or finding the right portion of the >> documentation. Your help has been invaluable. >> >> Jeremy > > One more question. I will be asking for the value of cs.xT *many* > (~millions) times. Therefore I don't want it's value to be calculated > on the fly. How can I set the value of xT whenever xS, xF, or xG are > changed, but not allow it to be set directly? From the example given > previously, it seems like it can't be done this way. > > Thans, > Jeremy > I'm certainly no wizard in timing, but here goes:
Using the class definition given to you by Bruno, adding the following to the end (and 'import timeit' at the start): #============ lst = timeit.Timer('for i in xrange(10): xx=xs.xT', \ 'from __main__ import Xs;xs = Xs()').repeat(100,1000) lst.sort() print lst print 'Average:', sum(lst)/100 #============ I get the output: [0.017246605364648282, 0.01727426251101738, 0.017275659336591698, 0.017290745052793044, 0.01733264982001903, 0.017347735536220377, and so on ... 0.029063749722380933, 0.029163762433493667, 0.029422733894950315, 0.029790378386079785] Average: 0.0182474979362 Thus: A 1000 assignments take a little over 18 milliseconds. The largest values in the list are probably caused bu GC occurring. But even 30 ms / 1000 iterations i.e. 30 microseconds per fetch seems to be fast enough. All this depends of course on the computer used. Mine is on the fast side, you might test it on your PC. The correct way of programming is to find a good simple algorithm and the data structures needed, to program a clean solution with them and then when you've got a correctly operating application and THEN IF you need speed try to do something about it. "Premature optimization is the worst evil" or something like that is how the saying goes. Hopefully I'm not leading you astray by being a novice in using the timeit module. HTH, Jussi -- http://mail.python.org/mailman/listinfo/python-list