Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'interpreter': 0.04; 'clause': 0.07; 'executed': 0.07; 'exit': 0.07; 'parsing': 0.07; 'subject:question': 0.08; 'python': 0.09; '(0,': 0.09; 'agree,': 0.09; 'other,': 0.09; 'snippet': 0.09; 'way:': 0.09; 'whichever': 0.09; 'looked': 0.10; 'itself.': 0.11; '"from': 0.16; '0.03': 0.16; '1):': 0.16; 'bytecode': 0.16; 'measurement': 0.16; 'subject:coding': 0.16; 'write.': 0.16; 'wrote:': 0.17; 'else,': 0.17; 'example.': 0.17; 'module,': 0.17; 'handles': 0.18; 'jan': 0.18; 'module': 0.19; 'code.': 0.20; 'import': 0.21; 'assuming': 0.22; 'work,': 0.22; 'example': 0.23; 'statement': 0.23; 'task': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'subject:/': 0.28; 'run': 0.28; '"no': 0.29; 'comparison': 0.29; "d'aprano": 0.29; 'overhead': 0.29; 'steven': 0.29; 'unlikely': 0.29; "we're": 0.30; 'call.': 0.30; 'evaluation': 0.30; 'writes': 0.30; 'function': 0.30; 'code': 0.31; 'print': 0.32; 'to:addr:python-list': 0.33; 'code:': 0.33; 'times.': 0.33; 'clear': 0.35; 'whatever': 0.35; 'faster': 0.35; 'so,': 0.35; 'there': 0.35; 'but': 0.36; 'level.': 0.36; 'test': 0.36; 'should': 0.36; 'thank': 0.36; 'too': 0.36; 'enough': 0.36; 'two': 0.37; 'subject:: ': 0.38; 'easier': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'takes': 0.39; 'notice': 0.39; 'where': 0.40; 'skip:" 10': 0.40; 'your': 0.60; 'most': 0.61; 'lower': 0.61; 'between': 0.63; 'times': 0.63; 'more': 0.63; 'six': 0.65; 'offer': 0.65; 'header:Reply-To:1': 0.68; 'million': 0.72; 'reply-to:no real name:2**0': 0.72; '2.7.': 0.84; 'each,': 0.84; 'fare': 0.84; 'subject:Basic': 0.84; 'branch,': 0.91; 'confirms': 0.91; 'faster.': 0.91 Date: Sat, 21 Jul 2012 12:32:53 +0300 From: Jan Riechers User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:14.0) Gecko/20120713 Thunderbird/14.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Basic question about speed/coding style/memory References: <500a711f$0$29978$c3e8da3$5496439d@news.astraweb.com> In-Reply-To: <500a711f$0$29978$c3e8da3$5496439d@news.astraweb.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: janpeterr@freenet.de 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: 66 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1342863358 news.xs4all.nl 6952 [2001:888:2000:d::a6]:45971 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:25735 On 21.07.2012 12:06, Steven D'Aprano wrote: > > But in general, you're worrying too much about trivia. One way or the > other, any speed difference will be trivial. Write whatever style reads > and writes most naturally, and only worry about what's faster where it > actually counts. > > > Notice that I try to make each function do the same amount of work, so > that we're seeing only the difference between "else" vs "no else". > > Now let's test the speed difference with Python 2.7. Because this is > timing small code snippets, we should use the timeit module to time the > code: > > from timeit import Timer > setup = "from __main__ import with_else, without_else" > t1 = Timer("for i in (0, 1): result = with_else(i)", setup) > t2 = Timer("for i in (0, 1): result = without_else(i)", setup) > > Each snippet calls the function twice, once to take the if branch, then > to take the else branch. > > Now we time how long it takes to run each code snippet 1000000 times. We > do that six times each, and print the best (lowest) speed: > > py> min(t1.repeat(repeat=6)) > 0.9761919975280762 > py> min(t2.repeat(repeat=6)) > 0.9494419097900391 > > So there is approximately 0.03 second difference per TWO MILLION > if...else blocks, or about 15 nanoseconds each. This is highly unlikely > to be the bottleneck in your code. Assuming the difference is real, and > not just measurement error, the difference is insignificant. > > So, don't worry about which is faster. Write whichever is more natural, > easier to read and write. > > Hello Steven, very nice example and thank you very much for also for the Timeit test! Actually it confirms my assumption in some way: [SNIP myself] So if there is some overhead in some fashion in case we don't offer the else, assuming the interpreter has to exit the evaluation of the "if"-statement clause and return to a "normal parsing code"-state outside the if statement itself. [SNAP] Without having looked at Andrew's bytecode excecution hint, using the dis module, to see how the interpreter handles the task on lower level. But fare enough for me :) But I agree, the return in my example is misleading and it would be illegal outside of a function call. I just added it to make clear that the fellow code below the return should not be executed in comparison to the 2nd example. Thank you very much Jan