[Tutor] Python Extensions in C
Stefan Behnel
stefan_ml at behnel.de
Thu May 26 20:55:50 CEST 2011
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
More information about the Tutor
mailing list