Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; ':-)': 0.06; 'compiler': 0.07; 'python': 0.08; 'counting': 0.09; 'integers': 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; 'subject:" ': 0.10; 'algorithm': 0.13; 'andreas': 0.16; 'computation': 0.16; 'subject:=': 0.16; 'language': 0.17; 'header:In-Reply-To:1': 0.22; 'code': 0.25; '(in': 0.26; 'code.': 0.26; 'loop': 0.28; 'problem': 0.28; 'bound': 0.29; 'do.': 0.30; 'operations': 0.30; 'operation.': 0.30; 'implement': 0.32; 'to:addr:python-list': 0.33; 'header:User-Agent:1': 0.34; 'integer': 0.34; 'languages.': 0.34; 'shorter': 0.34; 'header:X-Complaints-To:1': 0.35; 'reference': 0.35; 'object': 0.35; 'cheap': 0.37; 'several': 0.37; 'but': 0.37; 'something': 0.37; 'two': 0.37; 'think': 0.38; 'received:org': 0.38; 'subject:: ': 0.39; 'e.g.': 0.39; 'expensive': 0.39; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'subject: (': 0.39; "it's": 0.40; 'easily': 0.61; 'your': 0.61; 'ever': 0.65; 'here.': 0.66; 'high': 0.67; 'subject:+': 0.73; 'low': 0.73; 'gain': 0.74; 'schrieb': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Christian Heimes Subject: Re: relative speed of incremention syntaxes (or "i=i+1" VS "i+=1") Date: Mon, 22 Aug 2011 04:04:08 +0200 References: <4e513ceb$0$23863$e4fe514c@news2.news.xs4all.nl> <1313947658.3424.3.camel@thegeorge> <747a8223-0a9b-4691-8886-0a04433e54dc@glegroupsg2000goo.googlegroups.com> <1313969134.3135.21.camel@thegeorge> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: f049204197.adsl.alicedsl.de User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110617 Lightning/1.0b2.102ipre2 Thunderbird/3.1.11 In-Reply-To: <1313969134.3135.21.camel@thegeorge> X-Enigmail-Version: 1.1.2 OpenPGP: id=AD16AB1B; url=http://cheimes.de/heimes.asc 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: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1313978662 news.xs4all.nl 23919 [2001:888:2000:d::a6]:47306 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:11983 Am 22.08.2011 01:25, schrieb Andreas Löscher: > It's not as bad as you think. The addition of two integers is a cheap > task (in terms of computation power). If you have two way's to to it, > every little think (jumps in the code etc. ) will have a larger impact > on the execution time than on an expensive operation. > > But every improvement on your algorithm will easily result in a > significant shorter execution time than replaceing a+=1 with a=a+1 will > ever do. :-) You can learn an important lesson here. Since Python is a high level language without a JIT (yet) integer operations are much slower than in C or other low level languages. In general it's not a problem for most people. However if your algorithm is speed bound to integer operations or has a tight inner for loop than you can gain a considerable amount of speedup with C code. Reference counting and Python object creation need several CPU cycles. Also a good algorithm and a modern C compiler can make use of SIMD instructions, too. If you ever feel the need to implement something fast e.g. an encryption algorithm then you'd better off with a C implementation. Cython is my favourite tool for the job. Christian