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


Groups > comp.lang.python > #34293

Re: why does dead code costs time?

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder1.news.weretis.net!news1.tnib.de!feed.news.tnib.de!news.tnib.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python.ml.bruno.dupuis@lisael.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.040
X-Spam-Evidence '*H*': 0.92; '*S*': 0.00; 'subject:code': 0.07; 'python': 0.09; 'here?': 0.09; 'def': 0.10; 'dec': 0.15; 'compilers': 0.16; 'loops': 0.16; 'point!': 0.16; 'subject:dead': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'compilation': 0.17; '(in': 0.18; 'skip:p 30': 0.20; 'import': 0.21; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'wonder': 0.27; 'all.': 0.28; 'noticed': 0.28; 'case).': 0.29; 'url:mailman': 0.29; 'case,': 0.29; "i'm": 0.29; 'maybe': 0.29; 'that.': 0.30; 'keyword': 0.30; 'function': 0.30; 'code': 0.31; 'url:python': 0.32; 'room': 0.32; 'url:listinfo': 0.32; 'loading': 0.33; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'subject:?': 0.35; 'there': 0.35; 'received:org': 0.36; 'but': 0.36; 'url:org': 0.36; "didn't": 0.36; "i'll": 0.36; 'should': 0.36; 'charset:us-ascii': 0.36; 'possible': 0.37; 'uses': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'gives': 0.39; 'to:addr:python.org': 0.39; 'takes': 0.39; 'where': 0.40; 'url:mail': 0.40; 'content-disposition:inline': 0.60; 'dead': 0.62; 'between': 0.63; 'skip:n 10': 0.63; '10000': 0.65; 'study': 0.66; 'header:Reply-To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'gap': 0.84; 'neil': 0.84; 'obvious,': 0.91
Date Wed, 5 Dec 2012 17:40:51 +0100
From Bruno Dupuis <python.ml.bruno.dupuis@lisael.org>
To python-list@python.org
Subject Re: why does dead code costs time?
References <mailman.496.1354722393.29569.python-list@python.org> <ai9a9vFaup4U2@mid.individual.net>
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Disposition inline
In-Reply-To <ai9a9vFaup4U2@mid.individual.net>
User-Agent Mutt/1.5.21 (2010-09-15)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To python.ml.bruno.dupuis@lisael.org
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.500.1354725646.29569.python-list@python.org> (permalink)
Lines 68
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1354725646 news.xs4all.nl 6849 [2001:888:2000:d::a6]:39078
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:34293

Show key headers only | View raw


On Wed, Dec 05, 2012 at 04:15:59PM +0000, Neil Cerutti wrote:
> On 2012-12-05, Bruno Dupuis <python.ml.bruno.dupuis@lisael.org> wrote:
> > Hi,
> >
> > I'm interested in compilers optimizations, so I study python
> > compilation process
> >
> > I ran that script:
> >
> >     import timeit
> >
> >     def f(x):
> >         return None
> >
> >     def g(x):
> >         return None
> >         print(x)
> >
> >     number = 10000
> >
> >     print(timeit.timeit('f(1)',setup="from __main__ import f", number=number))
> >     print(timeit.timeit('g(1)',setup="from __main__ import g", number=number))   
> >
> >     print(dis.dis(f))
> >     print(dis.dis(g))
> >
> > It gives this output:
> >
> >     0.003460251959040761
> >     0.004164454061537981
> >      17           0 LOAD_CONST               0 (None) 
> >                   3 RETURN_VALUE         
> >     None
> >      20           0 LOAD_GLOBAL              0 (None) 
> >                   3 RETURN_VALUE         
> >
> >      21           4 LOAD_GLOBAL              1 (print) 
> >                   7 LOAD_FAST                0 (x) 
> >                  10 CALL_FUNCTION            1 (1 positional, 0 keyword pair) 
> >                  13 POP_TOP              
> >     None
> >
> > I do not understand why the dead code `print(x)` takes time (~20% in
> > that case). As we see in the opcode, a call to g(1) returns immediately, so
> > there should be no delay at all. Where am i wrong?
> >
> > mmhh... it comes to me now that the gap must be in function loading time...
> > I'll check ceval.c
> >
> > However, isn't there a room for a slight optim here? (in this case, the
> > dead code is obvious, but it may be hidden by complex loops and
> > conditions)
> 
> Maybe it's the difference between LOAD_CONST and LOAD_GLOBAL. We
> can wonder why g uses the latter.

Good point! I didn't even noticed that. It's weird... Maybe the
difference comes from a peehole optim on f which is not possible on g as
g is to complex.

> 
> -- 
> Neil Cerutti
> -- 
> http://mail.python.org/mailman/listinfo/python-list

-- 
Bruno Dupuis

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

why does dead code costs time? Bruno Dupuis <python.ml.bruno.dupuis@lisael.org> - 2012-12-05 16:46 +0100
  Re: why does dead code costs time? Neil Cerutti <neilc@norwich.edu> - 2012-12-05 16:15 +0000
    Re: why does dead code costs time? Bruno Dupuis <python.ml.bruno.dupuis@lisael.org> - 2012-12-05 17:40 +0100
      Re: why does dead code costs time? Ramchandra Apte <maniandram01@gmail.com> - 2012-12-09 06:11 -0800
        Re: why does dead code costs time? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-12-09 16:47 +0000
          Re: why does dead code costs time? Ramchandra Apte <maniandram01@gmail.com> - 2012-12-12 21:23 -0800
            Re: why does dead code costs time? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-13 06:01 +0000
              Re: why does dead code costs time? rusi <rustompmody@gmail.com> - 2012-12-12 22:27 -0800
                Re: why does dead code costs time? Cameron Simpson <cs@zip.com.au> - 2012-12-13 17:51 +1100
                Re: why does dead code costs time? rusi <rustompmody@gmail.com> - 2012-12-12 22:59 -0800
                Re: why does dead code costs time? Chris Angelico <rosuav@gmail.com> - 2012-12-13 18:03 +1100
          Re: why does dead code costs time? Ramchandra Apte <maniandram01@gmail.com> - 2012-12-12 21:23 -0800
    Re: why does dead code costs time? Bruno Dupuis <python.ml.bruno.dupuis@lisael.org> - 2012-12-05 18:19 +0100
      Re: why does dead code costs time? Tim Roberts <timr@probo.com> - 2012-12-05 21:20 -0800
  Re: why does dead code costs time? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-05 17:34 +0000
    Re: why does dead code costs time? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-05 17:39 +0000
    Re: why does dead code costs time? Ian Kelly <ian.g.kelly@gmail.com> - 2012-12-05 10:59 -0700
    Re: why does dead code costs time? Bruno Dupuis <python.ml.bruno.dupuis@lisael.org> - 2012-12-05 19:24 +0100
    Re: why does dead code costs time? Terry Reedy <tjreedy@udel.edu> - 2012-12-05 15:41 -0500
    Re: why does dead code costs time? Chris Kaynor <ckaynor@zindagigames.com> - 2012-12-05 12:49 -0800
    Re: why does dead code costs time? Bruno Dupuis <python.ml.bruno.dupuis@lisael.org> - 2012-12-05 21:50 +0100
    Re: why does dead code costs time? Bruno Dupuis <python.ml.bruno.dupuis@lisael.org> - 2012-12-05 22:58 +0100

csiph-web