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


Groups > comp.lang.python > #90306

Re: Calling a function is faster than not calling it?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!feeder2.ecngs.de!ecngs!feeder.ecngs.de!81.171.118.62.MISMATCH!peer02.fr7!news.highwinds-media.com!post02.fr7!fx29.am4.POSTED!not-for-mail
From BartC <bc@freeuk.com>
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0
MIME-Version 1.0
Newsgroups comp.lang.python
Subject Re: Calling a function is faster than not calling it?
References <554f2bb6$0$13011$c3e8da3$5496439d@news.astraweb.com>
In-Reply-To <554f2bb6$0$13011$c3e8da3$5496439d@news.astraweb.com>
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
Lines 39
Message-ID <fNP3x.641427$3i4.583649@fx29.am4> (permalink)
NNTP-Posting-Host 92.239.254.134
X-Complaints-To http://netreport.virginmedia.com
X-Trace 1431292107 92.239.254.134 (Sun, 10 May 2015 21:08:27 UTC)
NNTP-Posting-Date Sun, 10 May 2015 21:08:27 UTC
Organization virginmedia.com
Date Sun, 10 May 2015 22:08:25 +0100
X-Received-Body-CRC 3243205163
X-Received-Bytes 1739
Xref csiph.com comp.lang.python:90306

Show key headers only | View raw


On 10/05/2015 10:58, Steven D'Aprano wrote:
> from timeit import Timer
>
> def func():
>      a = 2
>      b = 3
>      c = 4
>      return (a+b)*(a-b)/(a*c + b*c)
>
>
> code = func.__code__
> assert func() == eval(code)
>
> t1 = Timer("eval; func()", setup="from __main__ import func")
> t2 = Timer("eval(code)", setup="from __main__ import code")
>
> # Best of 10 trials.
> print (min(t1.repeat(repeat=10)))
> print (min(t2.repeat(repeat=10)))

Maybe the overheads of using eval() are significant when calling a 
simple function such as your example.

When I made it do a bit more work:

def func():
     a = 2
     b = 3
     c = 4
     for i in range(50):
         x=(a+b)*(a-b)/(a*c + b*c)
     return (a+b)*(a-b)/(a*c + b*c)

Then the eval() call took only 3% longer rather than 100%.

-- 
Bartc

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


Thread

Calling a function is faster than not calling it? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-10 19:58 +1000
  Re: Calling a function is faster than not calling it? Christian Gollwitzer <auriocus@gmx.de> - 2015-05-10 12:34 +0200
    Re: Calling a function is faster than not calling it? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-11 01:04 +1000
  Re: Calling a function is faster than not calling it? Peter Otten <__peter__@web.de> - 2015-05-10 12:43 +0200
    Re: Calling a function is faster than not calling it? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-11 00:49 +1000
      Re: Calling a function is faster than not calling it? Peter Otten <__peter__@web.de> - 2015-05-10 18:14 +0200
      Re: Calling a function is faster than not calling it? Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-10 10:25 -0600
  Re: Calling a function is faster than not calling it? Terry Reedy <tjreedy@udel.edu> - 2015-05-10 12:37 -0400
  Re: Calling a function is faster than not calling it? BartC <bc@freeuk.com> - 2015-05-10 22:08 +0100
    Re: Calling a function is faster than not calling it? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-11 13:58 +1000
      Re: Calling a function is faster than not calling it? BartC <bc@freeuk.com> - 2015-05-11 10:50 +0100
        Re: Calling a function is faster than not calling it? Skip Montanaro <skip.montanaro@gmail.com> - 2015-05-11 09:12 -0500
          Re: Calling a function is faster than not calling it? BartC <bc@freeuk.com> - 2015-05-11 16:01 +0100
            Re: Calling a function is faster than not calling it? Skip Montanaro <skip.montanaro@gmail.com> - 2015-05-11 10:13 -0500
      Re: Calling a function is faster than not calling it? Tony the Tiger <tony@tiger.invalid> - 2015-05-15 01:35 +0000
  Re: Calling a function is faster than not calling it? Stefan Behnel <stefan_ml@behnel.de> - 2015-05-11 08:11 +0200
  Re: Calling a function is faster than not calling it? Piet van Oostrum <piet@vanoostrum.org> - 2015-06-22 23:49 +0200

csiph-web