[Tutor] Python Extensions in C
James Reynolds
eire1130 at gmail.com
Thu May 26 21:19:22 CEST 2011
Thank you Rachel and Alan for the feedback.
Oddly enough, I had created an exception between the time I sent this and
your response to catch occasions when the list is empty (I will need to test
a few other things, like what if the list holds items other than strictly
numbers?)
I did utilize some of the suggestions that Rachel suggested because that is
how I generally code in Python in any event, the extra variables were more
for my personal readability, but now that I understand the flow a little I
eliminated some of them (especially the ones that go directly to return
without doing much else)
Interestingly, I ran this through the profiler comparing it to similar
Python algorithms and the stats.mean is twice as slow as just staying in
Python. But, stats.var and stats.stdev are 4-5X faster.
As far as the null point goes, it shouldn't be null at all once it gets to
the point Alan pointed out. The pointer is set in (for example) stat_avg
seq = PySequence_Fast(obj, "Expected a Sequence");
if the point is NULL it won't make it to that, because of:
if (seq == NULL)
return NULL;
But maybe I am missing something that you have seen?
I'm going to try to force it to fail in some of the ways you pointed out,
and maybe some others.
I look forward to more feedback from you all!
I'll look into your suggestion regarding comprehension (i'm not sure what
that means in a programing sense, but I'm sure I'll find out!)
On Thu, May 26, 2011 at 2:55 PM, Stefan Behnel <stefan_ml at behnel.de> wrote:
> Rachel-Mikel ArceJaeger, 26.05.2011 17:46:
>
> A couple small things that will help improve memory management
>>
>> Rather than avg = sumall / count; return avg; Just return sumall/count
>> instead. Then you don't have to waste a register or assignment
>> operation.
>>
>> Division is expensive. Avoid it when you can.
>>
>> Here, for (a=0; a != count; a++) { temp =
>> PyFloat_AsDouble(PySequence_Fast_GET_ITEM(seq,a)); sumall += temp;
>> Again, save variables and operations. Write this as:
>>
>> for (a=0; a != count; a++) { sumall +=
>> PyFloat_AsDouble(PySequence_Fast_GET_ITEM(seq,a));
>>
>> Similar corrections in var()
>>
>> It's cheaper when you're using powers of two to just right or
>> left-shift:>> or<<. Since you want to increase by a power of two, do:
>>
>> (avg - PyFloat_AsDouble(PySequence_Fast_GET_ITEM(seq,a)<< 1; // This
>> means (...)^(2^1)
>>
>> Division by powers of two is>>. Note that these only works for powers of
>> two.
>>
>
> Oh please! You are seriously insulting my C compiler here. Believe me, it's
> a *lot* smarter than this.
>
> None of this is even faintly necessary.
>
> Stefan
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110526/7a502cb2/attachment-0001.html>
More information about the Tutor
mailing list