<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div>On Mar 11, 2008, at Mar 11:8:11 PM, David MacQuigg wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">At 03:12 PM 3/11/2008 -0700, Rob Malouf wrote:</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><br></div> <blockquote type="cite"><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">On Mar 11, 2008, at 2:57 PM, David MacQuigg wrote:</div> <blockquote type="cite"><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">I guess what I should conclude is that when performance is <span class="Apple-converted-space"> </span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">important, don't bother trying to optimize Python.<span class="Apple-converted-space">  </span>Go straight to <span class="Apple-converted-space"> </span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">C, and get 10 or 100X improvement.</div> </blockquote><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><br></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">That hasn't always been my experience.<span class="Apple-converted-space">  </span>I found that using psyco and <span class="Apple-converted-space"> </span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">numpy (along with careful profiling and optimization), I can often get <span class="Apple-converted-space"> </span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">essentially the same performance from pure Python that I can get from <span class="Apple-converted-space"> </span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">mixed Python/C solutions, and with much less trouble.</div> </blockquote><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><br></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">I agree that the Python/C interface is more trouble than it should be, especially for freshman engineering students and technical professionals who don't have time for messy programming details.<span class="Apple-converted-space">  </span>What I would like to see is something like a simple "directive" I could put in my Python code to say "The following function is in C", and have Python set up the linkage for me.</div></blockquote><div><br class="webkit-block-placeholder"></div><div>One of the things that drew me to Python for Numerical work was Pyrex (and now the Cython project).  Here is your code in Pyrex (sans-doc string)</div><div><br class="webkit-block-placeholder"></div><div><div>def getpts(double cx0,double cy0,double dx): </div><div>    vals = []</div><div>    cdef int p,i</div><div>    cdef double cx,zx,zy,zx2,zy2</div><div>    for p from 0&lt;=p&lt;100:     # 100 points</div><div>        cx = cx0 + p * dx        #  moving right    x . . . . . . </div><div>        zx = zy = 0.0</div><div>        for i from 0&lt;=i&lt;1000:     # 100 points</div><div>            zx2 = zx*zx</div><div>            zy2 = zy*zy</div><div>            if (zx2 + zy2) &gt; 4.0 : break  # escape</div><div>            zy = 2*zx*zy + cy0</div><div>            zx = zx2 - zy2 + cx</div><div>                        </div><div>        vals.append(i)</div><div><br class="webkit-block-placeholder"></div><div>    return vals</div><div><br class="webkit-block-placeholder"></div></div></div><div>This code gets compiled to C, and returns this unit test:</div><div><br class="webkit-block-placeholder"></div><div><div>Expected:</div><div>    1a) Python speed = 787 points/sec</div><div>    1b) C speed      = 125500 points/sec</div><div>    2a) Python speed = 823 points/sec</div><div>    2b) C      speed = 134300 points/sec</div><div>Got:</div><div>    1a) Python speed = 1214 points/sec</div><div>    1b) C speed      = 125700 points/sec</div><div>    2a) Python speed = 1287 points/sec</div><div>    2b) C      speed = 134500 points/sec</div><div><br class="webkit-block-placeholder"></div><div><br class="webkit-block-placeholder"></div></div><div>not too shabby!  And for only putting the types of the variable into the existing python code, and a slight modification of the for-loop syntax, the conversion of a slow python function into a fast extension module is trivial.  One of the best things here is that for debugging, you can put in arbitrary python code.  You take the performance hit, but you don't care during debugging, and you take it out again for production. </div><div><br class="webkit-block-placeholder"></div><div><br class="webkit-block-placeholder"></div><div><br class="webkit-block-placeholder"></div><div><span class="Apple-tab-span" style="white-space:pre">                                </span>bb<br class="webkit-block-placeholder"></div><div><br class="webkit-block-placeholder"></div><br><div> <span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px 0px; color: rgb(0, 0, 0); font-family: Monaco; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: auto; -khtml-text-decorations-in-effect: none; text-indent: 0px; -apple-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><div>-- </div><div>Brian Blais</div><div><a href="mailto:bblais@bryant.edu">bblais@bryant.edu</a></div><div><a href="http://web.bryant.edu/~bblais">http://web.bryant.edu/~bblais</a></div><div><br class="khtml-block-placeholder"></div><br class="Apple-interchange-newline"></span> </div><br></body></html>