Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #25725 > unrolled thread

Basic question about speed/coding style/memory

Started byJan Riechers <janpeterr@freenet.de>
First post2012-07-21 10:33 +0300
Last post2012-07-23 14:42 -0700
Articles 7 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Basic question about speed/coding style/memory Jan Riechers <janpeterr@freenet.de> - 2012-07-21 10:33 +0300
    Re: Basic question about speed/coding style/memory Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-21 09:06 +0000
      Re: Basic question about speed/coding style/memory Jan Riechers <janpeterr@freenet.de> - 2012-07-21 12:32 +0300
      Re: Basic question about speed/coding style/memory Devin Jeanpierre <jeanpierreda@gmail.com> - 2012-07-21 21:31 -0400
    Re: Basic question about speed/coding style/memory Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-07-21 21:19 +0200
    Re: Basic question about speed/coding style/memory 88888 Dihedral <dihedral88888@googlemail.com> - 2012-07-23 14:42 -0700
    Re: Basic question about speed/coding style/memory 88888 Dihedral <dihedral88888@googlemail.com> - 2012-07-23 14:42 -0700

#25725 — Basic question about speed/coding style/memory

FromJan Riechers <janpeterr@freenet.de>
Date2012-07-21 10:33 +0300
SubjectBasic question about speed/coding style/memory
Message-ID<mailman.2364.1342856185.4697.python-list@python.org>
Hello Pythonlist,

I have one very basic question about speed,memory friendly coding, and 
coding style of the following easy "if"-statement in Python 2.7, but Im 
sure its also the same in Python 3.x

Block
#----------------------------------
if statemente_true:
	doSomething()
else:
	doSomethingElseInstead()

#----------------------------------

versus this block:
#----------------------------------
if statement_true:
	doSomething()
	return

doSomethingElseInstead()

#----------------------------------


I understand the first pattern that I tell the interpreter to do:
Check if the conditional is true, run "doSomething()" else go inside the 
else block and "doSomethingElseInstead()".

while the 2nd does only checks:
doSomething() if statement_true, if not, just go directly to 
"doSomethingElseInstead()


Now, very briefly, what is the better way to proceed in terms of 
execution speed, readability, coding style?
Letting out the fact that, in order to prevent 
"doSomethingElseInstead"-Block to execute, a return has to provided.

Thank you for reading and hope someone brings light into that.

Your fellow python programmer
Jan

[toc] | [next] | [standalone]


#25734

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-07-21 09:06 +0000
Message-ID<500a711f$0$29978$c3e8da3$5496439d@news.astraweb.com>
In reply to#25725
On Sat, 21 Jul 2012 10:33:27 +0300, Jan Riechers wrote:

> Hello Pythonlist,
> 
> I have one very basic question about speed,memory friendly coding, and
> coding style of the following easy "if"-statement in Python 2.7, but Im
> sure its also the same in Python 3.x

I assume that the following is meant to be inside a function, otherwise 
the return in the second example is illegal.

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.

To give it an analogy that might be clear, this question is not too far 
from worrying about whether your car will be faster with the radio aerial 
up or down. Yes, technically the car will be slower with the aerial up, 
due to air resistance, but you'd have a job measuring it, and it makes no 
difference whether you are zooming down the highway at 120mph or stuck in 
traffic crawling along at 5mph.


Here's a minimal example:


def with_else(x):
    if x:
        a = x
    else:
        a = x+1
    return a


def without_else(x):
    if x:
        a = x
        return a
    a = x+1
    return a


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.


> Block
> #----------------------------------
> if statemente_true:
> 	doSomething()
> else:
> 	doSomethingElseInstead()

This style is especially recommended when the two clauses are equal in 
importance.


> versus this block:
> #----------------------------------
> if statement_true:
> 	doSomething()
> 	return
> doSomethingElseInstead()

This style is especially recommended when the doSomethingElseInstead() 
block is the "normal" procedure, and the doSomething() block is a special 
case. Not necessarily rare, but nevertheless special in some sense.

Of course, the decision as to which is the "special" case and which is 
the "normal" case is often entirely arbitrary.



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#25735

FromJan Riechers <janpeterr@freenet.de>
Date2012-07-21 12:32 +0300
Message-ID<mailman.2373.1342863358.4697.python-list@python.org>
In reply to#25734
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

[toc] | [prev] | [next] | [standalone]


#25786

FromDevin Jeanpierre <jeanpierreda@gmail.com>
Date2012-07-21 21:31 -0400
Message-ID<mailman.2410.1342920734.4697.python-list@python.org>
In reply to#25734
On Sat, Jul 21, 2012 at 5:06 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> 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.

It's probably real. For if-else, the true case needs to make a jump
before it returns, but for if-return, there's no jump and the return
is inlined.

-- Devin

> So, don't worry about which is faster. Write whichever is more natural,
> easier to read and write.

The most important advice. Even when it's a larger difference! :)

-- Devin

[toc] | [prev] | [next] | [standalone]


#25762

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-07-21 21:19 +0200
Message-ID<10519940.DbTeoADQlL@PointedEars.de>
In reply to#25725
Jan Riechers wrote:

> I have one very basic question about speed,memory friendly coding, and
> coding style of the following easy "if"-statement in Python 2.7, but Im
> sure its also the same in Python 3.x
> 
> Block
> #----------------------------------
> if statemente_true:
> doSomething()
> else:
> doSomethingElseInstead()
> 
> #----------------------------------
> 
> versus this block:
> #----------------------------------
> if statement_true:
> doSomething()
> return
> 
> doSomethingElseInstead()
> 
> #----------------------------------
> 
> 
> I understand the first pattern that I tell the interpreter to do:

A common misconception.  As a writer of Python source code, (usually) you 
never tell the (CPython) interpreter anything (but to start working on the 
source code).  Python source code is automatically *compiled* into bytecode 
by the (CPython) interpreter, and that bytecode is executed by a virtual 
machine.¹  So at most, you are telling that virtual machine to do something, 
through the bytecode created from your source code.

> Check if the conditional is true, run "doSomething()" else go inside the
> else block and "doSomethingElseInstead()".
> 
> while the 2nd does only checks:
> doSomething() if statement_true, if not, just go directly to
> "doSomethingElseInstead()
> 
> 
> Now, very briefly, what is the better way to proceed in terms of
> execution speed, readability, coding style?

Since this is comp.lang.python, you just need to check against the Zen of 
Python to know what you should do ;-)

<http://www.python.org/dev/peps/pep-0020/>

For me, this boils down in this case to the common recommendation "return 
early, return often" as "explicit is better than implicit" and "readability 
counts".  If there is nothing else than the `else' block in the function, 
there is no use for you to continue in the function, so you should return 
explicitly at this point.

On the other hand, if you can *avoid repeating code* in each branch by _not_ 
returning in the first branch, you should do that instead ("practicality 
beats purity").

HTH

_____
¹  This is not unlike in other so-called "scripting languages"; although for
   reasons that escape me, the software that compiles the source code – the
   compiler – is called the (C)Python *interpreter*, even in
   <http://docs.python.org/faq/general.html>.
-- 
PointedEars

Please do not Cc: me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#25927

From88888 Dihedral <dihedral88888@googlemail.com>
Date2012-07-23 14:42 -0700
Message-ID<4c65a8f9-91a9-4306-b794-0289db0be84d@googlegroups.com>
In reply to#25725
Jan Riechers於 2012年7月21日星期六UTC+8下午3時33分27秒寫道:
> Hello Pythonlist,
> 
> I have one very basic question about speed,memory friendly coding, and 
> coding style of the following easy &quot;if&quot;-statement in Python 2.7, but Im 
> sure its also the same in Python 3.x
> 
> Block
> #----------------------------------
> if statemente_true:

if an evaluated expression result is non-zero, then 


> 	doSomething()

> else: 
# execute this block if  the expression evaluated as zero
 
> 	doSomethingElseInstead()
> 
> #----------------------------------
> 
> versus this block:
> #----------------------------------
> if statement_true:
> 	doSomething()
> 	return
> 
> doSomethingElseInstead()
> 
> #----------------------------------
> 
> 
> I understand the first pattern that I tell the interpreter to do:
> Check if the conditional is true, run &quot;doSomething()&quot; else go inside the 
> else block and &quot;doSomethingElseInstead()&quot;.
> 
> while the 2nd does only checks:
> doSomething() if statement_true, if not, just go directly to 
> &quot;doSomethingElseInstead()
> 
> 
> Now, very briefly, what is the better way to proceed in terms of 
> execution speed, readability, coding style?
> Letting out the fact that, in order to prevent 
> &quot;doSomethingElseInstead&quot;-Block to execute, a return has to provided.
> 
> Thank you for reading and hope someone brings light into that.
> 
> Your fellow python programmer
> Jan

Well, the C-style branching is inherited in python.

Expressions and statements are different.

[toc] | [prev] | [next] | [standalone]


#25928

From88888 Dihedral <dihedral88888@googlemail.com>
Date2012-07-23 14:42 -0700
Message-ID<mailman.2503.1343079777.4697.python-list@python.org>
In reply to#25725
Jan Riechers於 2012年7月21日星期六UTC+8下午3時33分27秒寫道:
> Hello Pythonlist,
> 
> I have one very basic question about speed,memory friendly coding, and 
> coding style of the following easy &quot;if&quot;-statement in Python 2.7, but Im 
> sure its also the same in Python 3.x
> 
> Block
> #----------------------------------
> if statemente_true:

if an evaluated expression result is non-zero, then 


> 	doSomething()

> else: 
# execute this block if  the expression evaluated as zero
 
> 	doSomethingElseInstead()
> 
> #----------------------------------
> 
> versus this block:
> #----------------------------------
> if statement_true:
> 	doSomething()
> 	return
> 
> doSomethingElseInstead()
> 
> #----------------------------------
> 
> 
> I understand the first pattern that I tell the interpreter to do:
> Check if the conditional is true, run &quot;doSomething()&quot; else go inside the 
> else block and &quot;doSomethingElseInstead()&quot;.
> 
> while the 2nd does only checks:
> doSomething() if statement_true, if not, just go directly to 
> &quot;doSomethingElseInstead()
> 
> 
> Now, very briefly, what is the better way to proceed in terms of 
> execution speed, readability, coding style?
> Letting out the fact that, in order to prevent 
> &quot;doSomethingElseInstead&quot;-Block to execute, a return has to provided.
> 
> Thank you for reading and hope someone brings light into that.
> 
> Your fellow python programmer
> Jan

Well, the C-style branching is inherited in python.

Expressions and statements are different.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web