[Tutor] Python Extensions in C

Rachel-Mikel ArceJaeger arcejaeger at gmail.com
Thu May 26 17:46:35 CEST 2011


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.

Now I haven't worked with pointers in a long time and didn't fully trace this out so I'm probably wrong, but it doesn't seem like you ever have your pointers in stat_avg() point to an object. Therefore wouldn't they always be Null?


R.M. ArceJaeger
Author/Publisher, Platypus Press

Contact: arcejaeger at gmail.com
Website: http://rmarcejaeger.com





On May 26, 2011, at 8:22 AM, James Reynolds wrote:

> Hello All:
> 
> As an intellectual exercise, I wanted to try my hand at writing some extensions in C.
> 
> I was wondering if you all could look over my code and give some feedback.
> 
> Here is the link for the code: http://pastebin.com/jw3ihfsN
> 
> I have zero experience coding in C (and not much more coding in Python!). Being a kinetic learner, I thought this would be a good exercise to teach me some of the underpinnings of Python, how it works, why it works the way it does, and as an added bonus, skills to actually write my own extensions if I ever wanted to.
> 
> I had to learn about pointers to do this, and I'm still not 100% on if I used them correctly herein.
> 
> I am also very concerned with memory management because I am not sure when I should be calling the memory allocation macros to decref or incref when needed.
> 
> I would also like to get feedback on how I am constructing C algorithms.
> 
> As far as the module itself goes, I was able to compile and use it on a windows machine compiling with mingw (I use distutils to do the work, so for me I do "python setup.py build" in my CMD.
> 
> There are three functions, stats.mean, stats.var, stats.stdev (and they do what you would expect). One thing though, these are the "population" statistics and not "sample" in case you want to test it out.
> 
> Also, anything else that you think would be worthwile pointing out, tips and tricks, common pitfalls, etc.
> 
> Thanks in advance for you feedback.
> 
> _______________________________________________
> 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/b5493dc8/attachment-0001.html>


More information about the Tutor mailing list