Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #61178
| Path | csiph.com!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.004 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'algorithm': 0.04; 'syntax': 0.04; 'interpreter': 0.05; 'subject:Python': 0.06; 'binary': 0.07; 'differently': 0.07; 'computed': 0.09; 'method,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Does': 0.09; 'used.': 0.09; 'python': 0.11; '2),': 0.16; 'excerpt': 0.16; 'itself,': 0.16; 'kern': 0.16; 'powers.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'underlying': 0.16; 'wrote:': 0.18; 'numerical': 0.19; '(in': 0.22; 'header:User-Agent:1': 0.23; 'accommodate': 0.24; 'interpret': 0.24; 'skip': 0.24; 'question': 0.24; 'equivalent': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'robert': 0.30; 'comments': 0.31; 'operators': 0.31; 'trivial': 0.31; 'this.': 0.32; 'proceed': 0.33; 'case,': 0.35; 'computing': 0.35; 'objects': 0.35; 'but': 0.35; 'doing': 0.36; 'subject:?': 0.36; 'so,': 0.37; 'two': 0.37; 'implement': 0.38; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'expensive': 0.39; 'itself': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'algorithms': 0.60; 'free': 0.61; 'url:about': 0.61; 'john': 0.61; 'skip:* 10': 0.61; 'first': 0.61; 'choose': 0.64; 'our': 0.64; 'different': 0.65; 'taking': 0.65; 'worth': 0.66; 'world': 0.66; 'believe': 0.68; 'url:pdf': 0.68; 'power': 0.76; 'eco': 0.84; 'multiplying': 0.84; 'terrible': 0.84; 'received:86': 0.91; 'steps.': 0.91; 'choice.': 0.93 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Robert Kern <robert.kern@gmail.com> |
| Subject | Re: Does Python optimize low-power functions? |
| Date | Fri, 06 Dec 2013 19:12:30 +0000 |
| References | <5ea86e1b-f5b5-49d1-acfb-22ee4d9a1f16@googlegroups.com> <l7t6u6$4ef$1@ger.gmane.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | cpc2-cmbg17-2-0-cust347.5-4.cable.virginm.net |
| User-Agent | Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.1.1 |
| In-Reply-To | <l7t6u6$4ef$1@ger.gmane.org> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://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 | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3661.1386357170.18130.python-list@python.org> (permalink) |
| Lines | 46 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1386357170 news.xs4all.nl 2940 [2001:888:2000:d::a6]:34180 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:61178 |
Show key headers only | View raw
On 2013-12-06 19:01, Neil Cerutti wrote:
> On 2013-12-06, John Ladasky <john_ladasky@sbcglobal.net> wrote:
>> The following two functions return the same result:
>>
>> x**2
>> x*x
>>
>> But they may be computed in different ways. The first choice
>> can accommodate non-integer powers and so it would logically
>> proceed by taking a logarithm, multiplying by the power (in
>> this case, 2), and then taking the anti-logarithm. But for a
>> trivial value for the power like 2, this is clearly a wasteful
>> choice. Just multiply x by itself, and skip the expensive log
>> and anti-log steps.
>>
>> My question is, what do Python interpreters do with power
>> operators where the power is a small constant, like 2? Do they
>> know to take the shortcut?
>
> It uses a couple of fast algorithms for computing powers. Here's
> the excerpt with the comments identifying the algorithms used.
> From longobject.c:
>
> 2873 if (Py_SIZE(b) <= FIVEARY_CUTOFF) {
> 2874 /* Left-to-right binary exponentiation (HAC Algorithm 14.79) */
> 2875 /* http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf */
> ...
> 2886 else {
> 2887 /* Left-to-right 5-ary exponentiation (HAC Algorithm 14.82) */
It's worth noting that the *interpreter* per se is not doing this. The
implementation of the `long` object does this in its implementation of the
`__pow__` method, which the interpreter invokes. Other objects may implement
this differently and use whatever optimizations they like. They may even (ab)use
the syntax for things other than numerical exponentiation where `x**2` is not
equivalent to `x*x`. Since objects are free to do so, the interpreter itself
cannot choose to optimize that exponentiation down to multiplication.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Does Python optimize low-power functions? John Ladasky <john_ladasky@sbcglobal.net> - 2013-12-06 10:16 -0800
Re: Does Python optimize low-power functions? Neil Cerutti <neilc@norwich.edu> - 2013-12-06 19:01 +0000
Re: Does Python optimize low-power functions? Robert Kern <robert.kern@gmail.com> - 2013-12-06 19:12 +0000
RE: Does Python optimize low-power functions? Nick Cash <nick.cash@npcinternational.com> - 2013-12-06 19:32 +0000
Re: Does Python optimize low-power functions? John Ladasky <john_ladasky@sbcglobal.net> - 2013-12-06 11:43 -0800
Re: Does Python optimize low-power functions? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-12-06 20:57 +0000
Re: Does Python optimize low-power functions? Michael Torrie <torriem@gmail.com> - 2013-12-07 19:00 -0700
csiph-web