Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.01; 'subject:code': 0.07; 'tests,': 0.07; 'python': 0.08; 'declarations': 0.09; 'modifies': 0.09; 'paradigms': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'c++': 0.12; 'structures': 0.15; 'accelerator': 0.16; 'algorithmic': 0.16; 'from:addr:behnel.de': 0.16; 'from:addr:stefan_ml': 0.16; 'from:name:stefan behnel': 0.16; 'non-python': 0.16; 'optimizing': 0.16; 'received:188.174': 0.16; 'received:mnet-online.de': 0.16; 'such.': 0.16; 'prototype': 0.18; 'rewrite': 0.18; 'written': 0.20; 'later': 0.21; '(or': 0.22; "doesn't": 0.22; 'header:In- Reply-To:1': 0.22; 'required.': 0.23; 'so-called': 0.23; 'static': 0.24; 'stefan': 0.24; 'modify': 0.25; 'code': 0.25; 'helpful': 0.26; 'module': 0.26; "i'm": 0.26; 'import': 0.27; 'code,': 0.27; 'mainly': 0.28; 'compare': 0.28; 'separate': 0.28; 'compile': 0.29; 'module.': 0.29; 'replaced': 0.29; 'yield': 0.29; 'usual': 0.29; '"good': 0.30; 'ran': 0.30; 'subject:Writing': 0.30; 'usually': 0.31; 'pure': 0.32; 'wondering': 0.32; 'idea': 0.32; "can't": 0.32; 'header:User-Agent:1': 0.33; 'header:X-Complaints- To:1': 0.33; 'instead': 0.33; 'there': 0.33; 'decide': 0.33; 'to:addr:python-list': 0.34; 'file.': 0.34; 'things': 0.34; 'apply': 0.35; 'external': 0.35; 'file': 0.36; 'two': 0.37; 'but': 0.37; 'run': 0.37; 'think': 0.37; 'enough': 0.38; 'using': 0.38; 'received:org': 0.38; 'i.e.': 0.39; 'received:de': 0.39; 'either': 0.39; 'being': 0.39; '(with': 0.39; "it's": 0.40; 'to:addr:python.org': 0.40; 'type': 0.61; 'types': 0.61; 'worth': 0.61; 'your': 0.61; 'order': 0.62; 'stop': 0.63; 'received:188': 0.68; 'gain': 0.75; 'liked': 0.80; 'long-term': 0.80; 'hot': 0.84; 'benchmark': 0.84; 'optimisation': 0.84; 'parts,': 0.84; 'complexity': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Stefan Behnel Subject: Re: Writing code to be optimizable Date: Wed, 23 Nov 2011 08:37:53 +0100 References: <63e78437-c76b-4a9e-9a62-bfea8d078208@v5g2000yqn.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: ppp-188-174-54-136.dynamic.mnet-online.de User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.23) Gecko/20110921 Lightning/1.0b2 Thunderbird/3.1.15 In-Reply-To: <63e78437-c76b-4a9e-9a62-bfea8d078208@v5g2000yqn.googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1322033890 news.xs4all.nl 6884 [2001:888:2000:d::a6]:49671 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:16098 snorble, 23.11.2011 06:19: > Sometimes I want to prototype a program in Python, with the idea of > optimizing it later by rewriting parts of it in C or Cython. But I > usually find that in order to rewrite the slow parts, I end up writing > those parts very much like C or C++ anyway, and I end up wondering > what is the point of using Python in such a project. > > I liked the idea of Cython, mainly the idea of being able to write in > pure Python, then create a file that modifies the pure Python file > (with type declarations and such), but I ran into the same problems. > In order to be able to modify the code, I can't make a lot of use of > the really helpful things in Python like dicts and such. I have to > dumb down Python to where I'm basically writing C code in Python, so > again I ask myself what is the point? > > Is it reasonable to prototype an application in Python that will > require performance? Are there any recommendations on how to write > code in such a way that it can later be optimized or replaced with a > module written in C or Cython? I think the usual approach is to write it in Python and make sure it works by writing "enough" tests, then benchmark it, then decide if a non-Python level optimisation is required. If it's required, Cython compile the entire module and add static types to your hot spots, either in Python notation ("pure mode") or in an external .pxd file. If that doesn't yield enough performance, copy code into a separate Cython module and optimise it there using C paradigms instead of Python paradigms, i.e. apply algorithmic optimisations by dropping data structures into C. Then use an import to use the code from your so-called accelerator module in your original module. Try to provide both a pure Python implementation and a Cython implementation, as that will allow you to run your code in other Python implementations directly and to compare your two implementations for equivalence and performance. The advantage of stepping down into C-ish Cython code incrementally is that you will have a working implementation at each step and can decide to stop optimising because it is "good enough". If you started 'optimising' your code while still writing it, it will take longer to write it and you can never be sure that the complexity you add to the long-term maintenance by writing C-ish code is truly worth the performance gain (or if there even is any gain). Stefan