Speed:
>>> t1.timeit() # Model.__init__ from django trunk with args stuff stripped
179.84739959966981
>>> t2.timeit()
139.67626695571641 # Model.fromargs()

Implementation:
def fromtuple(cls, values):
        dispatcher.send(signal=signals.pre_init, sender=cls,
args=values, kwargs={})
        new_instance = object.__new__(cls)
        if len(values) > len(new_instance._meta.fields):
            raise IndexError("Number of args exceeds number of
fields")
        fields_iter = iter(new_instance._meta.fields)
        for val, field in izip(values, fields_iter):
            setattr(new_instance, field.attname, val)
        for field in fields_iter:
            setattr(new_instance, field.attname, field.get_default())
        dispatcher.send(signal=signals.post_init, sender=cls,
instance=new_instance)
        return new_instance
    fromtuple = classmethod(fromtuple)

    def fromargs(cls, *args):
        return cls.fromtuple(args)
    fromargs = classmethod(fromargs)



> Speed also XXXX
>
> As it stands now (in QSRF r7049), args are indeed faster than kwargs::
>
> >>> t1 = timeit.Timer("Person(1, 'First', 'Last')", "from blah.models
> import Person")
> >>> t2 = timeit.Timer("Person(id=1, first='First', last='Last')",
>
> "from blah.models import Person")>>> t1.timeit()
> 25.09495210647583
> >>> t2.timeit()
>
> 36.52219820022583
>
> However, much of that extra time is spent dealing with the args/kwargs
> confusion; chopping out the code that handles initialization from args
> gives better timing:
>
> >>> t = timeit.Timer("Person(id=1, first='First', last='Last')", "from
>
> blah.models import Person")>>> t.timeit()
>
> 29.819494962692261
>
> So that's about a 15% speed improvement over the current
> __init__(**kwargs) at the cost of losing that same 15% since you can't
> do *args.
>
> [Note, however, that this speed argument is a bit silly when __init__
> fires two extremely slow signals. Improving signal performance --
> which is very high on my todo list -- will probably make this whole
> performance debate a bit silly]
>
> Jacob
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to