tag:blogger.com,1999:blog-1700157236206200597.post2556746310926583047..comments2008-03-29T15:21:57.755-07:00Comments on Valued Lessons: Why are my monads so slow?Peter Thatcherhttp://www.blogger.com/profile/01092342988993218446noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-1700157236206200597.post-88932141948678815492008-03-29T15:21:00.000-07:002008-03-29T15:21:00.000-07:00When I was optimizing pyparsing, I found the @prof...When I was optimizing pyparsing, I found the @profile decorator described on the Python wiki to be very helpful.<BR/><BR/>The most primitive monads (such as pyparsing's Literal and Word) are those that benefit most from code optimization.<BR/><BR/>Function calls are peformance death in python, but some operations are more expensive then function calls. When optimizing Literal, I found that replacing<BR/><BR/>if instring[curLoc:curLoc+literalLength]==literalstring:<BR/><BR/>with<BR/><BR/>if literalstring.startswith(instring, curloc):<BR/><BR/>was quite a healthy win. I also apparently invented a cacheing system for exceptions that is unseen elsewhere in Python. Exceptions are used everywhere to signal when one of several parsing options doesn't match - this means that thousands of Exception instances are created and almost immediately discarded. Each monad ends up raising the same exception (with some minor, er, exceptions), so I create the Exception at monad creation time, and then just keep raising the cached Exception instead of creating a new one.Paul McGuirehttp://pyparsing.wikispaces.comnoreply@blogger.comtag:blogger.com,1999:blog-1700157236206200597.post-67127059831664750052008-03-25T13:23:00.000-07:002008-03-25T13:23:00.000-07:00AFAIK, decorators only work at function creation t...AFAIK, decorators only work at function creation time and do not penalize actual calls, except for an extra function call.<BR/>Function calls are relatively expensive in Python, though.9000http://www.blogger.com/profile/06887762560560522733noreply@blogger.comtag:blogger.com,1999:blog-1700157236206200597.post-64188403443228764272008-03-20T06:55:00.000-07:002008-03-20T06:55:00.000-07:00Python doesn't do any optimization for them at all...Python doesn't do any optimization for them at all. Monads are incredibly flexible, but as any decent C.S. major knows, everything comes at a price. Monads give a high level of abstraction and can model a huge variety of computations. There's no reason to expect them to be on par with simpler, to-the-point styles of programming.<BR/><BR/>Monads have their place in programming, but that place is Haskell and other pure languages.Tac-Ticshttp://www.blogger.com/profile/14070765709653635739noreply@blogger.comtag:blogger.com,1999:blog-1700157236206200597.post-82338720384257884122008-03-20T05:09:00.000-07:002008-03-20T05:09:00.000-07:00Very interesting. D language version 2.x has true ...Very interesting. D language version 2.x has true closures, I'd like to see how your code works there.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1700157236206200597.post-20742836662345465162008-03-20T04:51:00.000-07:002008-03-20T04:51:00.000-07:00One thing worth keeping in mind about Python is th...One thing worth keeping in mind about Python is that function calls are expensive in time, more so than most other languages. So to get the fastest Python code, write in an imperative style and rely heavily on builtins.<BR/><BR/>Not good news for someone who wants to write fast functional code in Python. :DShubhttp://www.blogger.com/profile/11846533071533338593noreply@blogger.comtag:blogger.com,1999:blog-1700157236206200597.post-86521839008972583132008-03-19T17:31:00.000-07:002008-03-19T17:31:00.000-07:00I'd be curious to know if the JIT optimization per...I'd be curious to know if the JIT optimization performed by Psyco reduces the relative penalty for working with immutable objects.<BR/><BR/>Seeing what PyPy does to this code would be even more interesting, but that project is so bleeding edge, I have no idea what other issues you would stumble on.Stan Seibertnoreply@blogger.com