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


Groups > comp.lang.python > #34291

Re: why does dead code costs time?

From Neil Cerutti <neilc@norwich.edu>
Newsgroups comp.lang.python
Subject Re: why does dead code costs time?
Date 2012-12-05 16:15 +0000
Organization Norwich University
Message-ID <ai9a9vFaup4U2@mid.individual.net> (permalink)
References <mailman.496.1354722393.29569.python-list@python.org>

Show all headers | View raw


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.

-- 
Neil Cerutti

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