Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

6 May, 2024: The networking issue during the past two days has been identified and appears to be fixed. Will keep monitoring.


devel / comp.lang.python / Quick question about CPython interpreter

SubjectAuthor
* Quick question about CPython interpreterDFS
+- Re: Quick question about CPython interpreterMichael Torrie
+- Re: Quick question about CPython interpreterDavid Lowry-Duda
`* Re: Quick question about CPython interpreterMRAB
 `* Re: Quick question about CPython interpreterStefan Ram
  `- Re: Quick question about CPython interpreterChris Angelico

1
Quick question about CPython interpreter

<lll2L.218851$3AK7.48447@fx35.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19906&group=comp.lang.python#19906

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx35.iad.POSTED!not-for-mail
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.3.3
Newsgroups: comp.lang.python
Content-Language: en-US
From: nos...@dfs.com (DFS)
Subject: Quick question about CPython interpreter
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 21
Message-ID: <lll2L.218851$3AK7.48447@fx35.iad>
X-Complaints-To: abuse@blocknews.net
NNTP-Posting-Date: Fri, 14 Oct 2022 22:25:21 UTC
Organization: blocknews - www.blocknews.net
Date: Fri, 14 Oct 2022 18:25:23 -0400
X-Received-Bytes: 1249
 by: DFS - Fri, 14 Oct 2022 22:25 UTC

---------------------------------------------------------------------
this does a str() conversion in the loop
---------------------------------------------------------------------
for i in range(cells.count()):
if text == str(ID):
break

---------------------------------------------------------
this does one str() conversion before the loop
---------------------------------------------------------
strID = str(ID)
for i in range(cells.count()):
if text == strID:
break

But does CPython interpret the str() conversion away and essentially do
it for me in the first example?

Re: Quick question about CPython interpreter

<mailman.739.1666021141.20444.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19942&group=comp.lang.python#19942

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: torr...@gmail.com (Michael Torrie)
Newsgroups: comp.lang.python
Subject: Re: Quick question about CPython interpreter
Date: Mon, 17 Oct 2022 09:38:56 -0600
Lines: 24
Message-ID: <mailman.739.1666021141.20444.python-list@python.org>
References: <lll2L.218851$3AK7.48447@fx35.iad>
<930918a6-556f-2cdf-710b-e333703a9ca6@gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de PrcJtT4oPiRhWSZeHtT6aAmppv7uoyMXMMgD1OtqWpeQ==
Return-Path: <torriem@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=F2fjLcSm;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.064
X-Spam-Evidence: '*H*': 0.87; '*S*': 0.00; 'loop': 0.07; 'conversion':
0.16; 'cpython': 0.16; 'hood.': 0.16; 'subject:CPython': 0.16;
'subject:interpreter': 0.16; 'wrote:': 0.16; 'subject:question':
0.17; 'message-id:@gmail.com': 0.18; 'to:addr:python-list': 0.20;
'header:User-Agent:1': 0.30; 'module': 0.31; 'skip:- 60': 0.32;
'but': 0.32; 'header:In-Reply-To:1': 0.34; 'received:google.com':
0.34; 'from:addr:gmail.com': 0.35; 'received:209.85': 0.37;
'received:192.168': 0.37; 'received:209': 0.39; 'text': 0.39;
'use': 0.39; 'break': 0.39; 'skip:r 20': 0.64; 'away': 0.67;
'skip:- 50': 0.76; 'subject:Quick': 0.76; 'dis': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=content-transfer-encoding:in-reply-to:from:references:to
:content-language:subject:user-agent:mime-version:date:message-id
:from:to:cc:subject:date:message-id:reply-to;
bh=Co5ZjsFeFhS4RjHxDnox1XKMy48mlFu/mj7uz9FAYUQ=;
b=F2fjLcSmpFgpvCAK9ufr8dFAqn/dwsX1bZqzUgTVbYGvAL9PbEsgs4EY4I+fByfJUk
Rlfgqlr5n2x2HxybOw7KZwLCqbjj7pVH/i4PqziwTJIH6gAKDX63gdmjGBH+ta+zQUa6
jblZe5T93hwTkUlNPHhRLdAPIKK4P//41qeSrk1v6nlzldvMM5DSOTajQJbrLjXHGxse
Cf1e1kAyBLRSCMjk99+fWvMIiEgJ1EA8HIqzK2eZnvZ9gdiT5qC3acGGVaZuZvtikx5X
AnIXSFMHhsFr7cAj/yQa0zNhNM8AOhvmaQmVq2OyFJqrQBrAvpa3tceqHAYGyM1HNkQk
63YA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=content-transfer-encoding:in-reply-to:from:references:to
:content-language:subject:user-agent:mime-version:date:message-id
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
bh=Co5ZjsFeFhS4RjHxDnox1XKMy48mlFu/mj7uz9FAYUQ=;
b=Aws9q6t5uHSkHgVel6l0ej6IDReCVPm2kKdFOIf+XxxkTG8bnCwj+usXcSalUkQCe0
UskJyztJMsYjoQlOyfkk/ojljAW6PgS0Antug5WtzFn8oG3n+gExeztwsL0eiRqfdKiO
VVd1PZNp0IyxBVPtPejsOSVVFdPAalXvvWWyAztyw+g+c9WHe/gG4eUMtTY1tRjLBoq2
DubTjfSi+thytJ5zlrHmiah85EmUzxBg1ux8E8MwXVFMI21702gMdSw4hmvzK3eiR3gp
vSPB10Y6URBCWFbVGj/o9z/Yf9YoQSMISX7DFcwuFH9U8eb5VywMdPJUutwrw0/VyNkE
G/8g==
X-Gm-Message-State: ACrzQf1FOzworb1vUV2l5lPzbj/HbMXkfZRgwQ8xrVWZKxE0g5nEfAfE
1yDBeM/AS+XYpZ5Fssh/534RlsoVj88=
X-Google-Smtp-Source: AMsMyM4Wg7D7CKs/uhYGj0mxoGe0YxRJlhBmZ4sB72YITlbUDEo4yeyRZB+uG/uoT0ADzdeu/4r6DA==
X-Received: by 2002:a17:902:8a97:b0:178:1939:c721 with SMTP id
p23-20020a1709028a9700b001781939c721mr12716550plo.108.1666021138044;
Mon, 17 Oct 2022 08:38:58 -0700 (PDT)
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.10.0
Content-Language: en-US
In-Reply-To: <lll2L.218851$3AK7.48447@fx35.iad>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
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: <https://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>
X-Mailman-Original-Message-ID: <930918a6-556f-2cdf-710b-e333703a9ca6@gmail.com>
X-Mailman-Original-References: <lll2L.218851$3AK7.48447@fx35.iad>
 by: Michael Torrie - Mon, 17 Oct 2022 15:38 UTC

On 10/14/22 16:25, DFS wrote:
> ---------------------------------------------------------------------
> this does a str() conversion in the loop
> ---------------------------------------------------------------------
> for i in range(cells.count()):
> if text == str(ID):
> break
>
>
> ---------------------------------------------------------
> this does one str() conversion before the loop
> ---------------------------------------------------------
> strID = str(ID)
> for i in range(cells.count()):
> if text == strID:
> break
>
>
> But does CPython interpret the str() conversion away and essentially do
> it for me in the first example?

No.

You can use the dis module to show you what CPython is doing under the hood.

Re: Quick question about CPython interpreter

<mailman.740.1666021452.20444.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19943&group=comp.lang.python#19943

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!aioe.org!news.mb-net.net!open-news-network.org!news.mind.de!bolzen.all.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: dav...@lowryduda.com (David Lowry-Duda)
Newsgroups: comp.lang.python
Subject: Re: Quick question about CPython interpreter
Date: Mon, 17 Oct 2022 11:43:59 -0400
Lines: 88
Message-ID: <mailman.740.1666021452.20444.python-list@python.org>
References: <lll2L.218851$3AK7.48447@fx35.iad>
<Y014PxsyRtA7eaa/@icerm-dld>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii; format=flowed
X-Trace: news.uni-berlin.de 8hEI2LHhltlA8h2/TxUTDAusfdGpu/wP9oi4eqomjBAg==
Return-Path: <david@lowryduda.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="1024-bit key; unprotected key"
header.d=lowryduda.com header.i=@lowryduda.com header.b=i2DsS2Qj;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.024
X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'generated': 0.03; '(to':
0.07; '(i)': 0.16; 'cpython': 0.16; 'generates': 0.16;
'interpreter': 0.16; 'subject:CPython': 0.16;
'subject:interpreter': 0.16; 'python': 0.16; 'subject:question':
0.17; 'calls': 0.19; 'to:addr:python-list': 0.20; 'seems': 0.26;
'perform': 0.26; 'function': 0.27; 'done': 0.28; 'module': 0.31;
"doesn't": 0.32; 'header:In-Reply-To:1': 0.34; 'file': 0.38;
'use': 0.39; 'break': 0.39; 'here.': 0.61; 'skip:o 10': 0.61;
'short,': 0.76; 'subject:Quick': 0.76; 'received:23.106': 0.84;
'optimization': 0.93
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lowryduda.com;
s=dkim; t=1666021442;
h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
to:to:cc:mime-version:mime-version:content-type:content-type:
in-reply-to:in-reply-to:references:references;
bh=8GY6+U6Qt71YsRKMn1LsVYGUlpiWqCRYNGEvnwKWnro=;
b=i2DsS2QjEq7HUXzz6sAZc+4WQgK+CxdVvODGU7wsirzsPrMqfw/bvcSMdFUWklfPn6YaiN
OX9u21AVHgSbwPMt/Ok69cblJcfGbXKtGrz3XYgxvz/duZQru1fyW8leEWzf3RYQeKFq5P
1xqb0sFSVtyzkrE768M/dlc8Vm02w3c=
Mail-Followup-To: python-list@python.org
Content-Disposition: inline
In-Reply-To: <lll2L.218851$3AK7.48447@fx35.iad>
X-Spam-Status: No, score=-0.88
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
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: <https://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>
X-Mailman-Original-Message-ID: <Y014PxsyRtA7eaa/@icerm-dld>
X-Mailman-Original-References: <lll2L.218851$3AK7.48447@fx35.iad>
 by: David Lowry-Duda - Mon, 17 Oct 2022 15:43 UTC

One can use the `dis` module and investigate the generated python
bytecode. For me, I get

# file "dis1.py"
thing = 123
for i in range(10):
if "hi" == str(thing):
print("found")
break

The bytecode is then

1 0 LOAD_CONST 0 (123)
2 STORE_NAME 0 (thing)

2 4 LOAD_NAME 1 (range)
6 LOAD_CONST 1 (10)
8 CALL_FUNCTION 1
10 GET_ITER
>> 12 FOR_ITER 28 (to 42)
14 STORE_NAME 2 (i)

3 16 LOAD_CONST 2 ('hi')
18 LOAD_NAME 3 (str)
20 LOAD_NAME 0 (thing)
22 CALL_FUNCTION 1
24 COMPARE_OP 2 (==)
26 POP_JUMP_IF_FALSE 12

4 28 LOAD_NAME 4 (print)
30 LOAD_CONST 3 ('found')
32 CALL_FUNCTION 1
34 POP_TOP

5 36 POP_TOP
38 JUMP_ABSOLUTE 42
40 JUMP_ABSOLUTE 12
>> 42 LOAD_CONST 4 (None)
44 RETURN_VALUE

I note that line 22 calls the function str repeatedly, and no
optimization is done here.

# file "dis2.py"
thing = 123
strthing = str(thing)
for i in range(10):
if "hi" == strthing:
print("found")
break

This generates bytecode

1 0 LOAD_CONST 0 (123)
2 STORE_NAME 0 (thing)

2 4 LOAD_NAME 1 (str)
6 LOAD_NAME 0 (thing)
8 CALL_FUNCTION 1
10 STORE_NAME 2 (strthing)

3 12 LOAD_NAME 3 (range)
14 LOAD_CONST 1 (10)
16 CALL_FUNCTION 1
18 GET_ITER
>> 20 FOR_ITER 24 (to 46)
22 STORE_NAME 4 (i)

4 24 LOAD_CONST 2 ('hi')
26 LOAD_NAME 2 (strthing)
28 COMPARE_OP 2 (==)
30 POP_JUMP_IF_FALSE 20

5 32 LOAD_NAME 5 (print)
34 LOAD_CONST 3 ('found')
36 CALL_FUNCTION 1
38 POP_TOP

6 40 POP_TOP
42 JUMP_ABSOLUTE 46
44 JUMP_ABSOLUTE 20
>> 46 LOAD_CONST 4 (None)
48 RETURN_VALUE

In short, it seems the cpython interpreter doesn't (currently) perform
this sort of optimization.

- DLD

Re: Quick question about CPython interpreter

<mailman.741.1666023944.20444.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19944&group=comp.lang.python#19944

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!aioe.org!news.mb-net.net!open-news-network.org!news.mind.de!bolzen.all.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: pyt...@mrabarnett.plus.com (MRAB)
Newsgroups: comp.lang.python
Subject: Re: Quick question about CPython interpreter
Date: Mon, 17 Oct 2022 17:25:34 +0100
Lines: 90
Message-ID: <mailman.741.1666023944.20444.python-list@python.org>
References: <lll2L.218851$3AK7.48447@fx35.iad> <Y014PxsyRtA7eaa/@icerm-dld>
<a14f7ff0-ad1f-503f-017a-2e58f27801e4@mrabarnett.plus.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de K9HE8eqxjDQXr2BPTX3GsQwagIyrekAkmf8cEZ/8HFug==
Return-Path: <python@mrabarnett.plus.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=plus.com header.i=@plus.com header.b=SWlgHv2y;
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.002
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'generated': 0.03; '(to':
0.07; 'from:addr:python': 0.09; 'received:192.168.1.64': 0.09;
'(i)': 0.16; 'because,': 0.16; 'cpython': 0.16;
'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16;
'generates': 0.16; 'interpreter': 0.16; 'message-
id:@mrabarnett.plus.com': 0.16; 'received:84.93': 0.16;
'received:84.93.230': 0.16; 'received:plus.net': 0.16;
'subject:CPython': 0.16; 'subject:interpreter': 0.16; 'wrote:':
0.16; 'python': 0.16; 'subject:question': 0.17; "can't": 0.17;
'calls': 0.19; 'to:addr:python-list': 0.20; 'seems': 0.26;
'perform': 0.26; 'function': 0.27; 'done': 0.28; 'header:User-
Agent:1': 0.30; 'module': 0.31; "doesn't": 0.32;
'received:192.168.1': 0.32; 'header:In-Reply-To:1': 0.34;
'received:192.168': 0.37; 'file': 0.38; 'use': 0.39; 'break':
0.39; 'here.': 0.61; 'skip:o 10': 0.61; 'short,': 0.76;
'subject:Quick': 0.76; 'optimise': 0.84; 'say,': 0.84;
'optimization': 0.93
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=plus.com; s=042019;
t=1666023936; bh=8AzXqXpbwMVOos7HhQCS65/aAxiQwy9+jlSv93WlMtE=;
h=Date:Subject:To:References:From:In-Reply-To;
b=SWlgHv2y6pFQouGgRD2VhfSLwbvnW6Liizto/s1sCN58D5icyX+oWLlYtzS1ZXzfA
Iu6Ak2vkVmMf/C+xcytB3QrY3hxYB5IQX7HSlzpzhdyCExtu5MQSIuNWqR0RvsPuuY
AoQ3xJK25bTbYQTNzBkisRW/FsqoO55d1+/Lv/StiWwUTz9uLq0rc0wGOGi1F6j8QJ
yOp3kTTvIVj6i+baNmKFq22sycWiAgGmhnhYZRbEPJ4GNTxhcfo3PNq0P41Y2fktTP
TpeGQxnNdhV4YkhYk/I7tW6hNo4cn+wTrBQyG20dngS8OSXW1xSxEKUbnLIZMeC28a
0q1KoXP/TWShA==
X-Clacks-Overhead: "GNU Terry Pratchett"
X-CM-Score: 0.00
X-CNFS-Analysis: v=2.4 cv=cpKILn0i c=1 sm=1 tr=0 ts=634d8200
a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17
a=IkcTkHD0fZMA:10 a=8Qc8u5eYUv-ZzTy7pkoA:9 a=QEXdDO2ut3YA:10
X-AUTH: mrabarnett@:2500
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.3.3
Content-Language: en-GB
In-Reply-To: <Y014PxsyRtA7eaa/@icerm-dld>
X-CMAE-Envelope: MS4xfAgk+fX7FjzkZMkpwq9R8RQwdyYF2ZvBi/c7aG6x1xZz8t9G5gT9TbMCdUfR45YrOkFLil+v26DAXxkMEpyiq93czmSUjbvAsGHCwrvaq/+681LFTKUo
lgVwD2gVdpsuAL9KFFa91w3nlXuA3bZEG+MO68ay5IaljBPkzTwirBC3yPpgxvIpQJLJsIBA4ESPDRt8V454V29etB8CwbvSfsQ=
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
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: <https://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>
X-Mailman-Original-Message-ID: <a14f7ff0-ad1f-503f-017a-2e58f27801e4@mrabarnett.plus.com>
X-Mailman-Original-References: <lll2L.218851$3AK7.48447@fx35.iad>
<Y014PxsyRtA7eaa/@icerm-dld>
 by: MRAB - Mon, 17 Oct 2022 16:25 UTC

On 2022-10-17 16:43, David Lowry-Duda wrote:
> One can use the `dis` module and investigate the generated python
> bytecode. For me, I get
>
> # file "dis1.py"
> thing = 123
> for i in range(10):
> if "hi" == str(thing):
> print("found")
> break
>
> The bytecode is then
>
> 1 0 LOAD_CONST 0 (123)
> 2 STORE_NAME 0 (thing)
>
> 2 4 LOAD_NAME 1 (range)
> 6 LOAD_CONST 1 (10)
> 8 CALL_FUNCTION 1
> 10 GET_ITER
> >> 12 FOR_ITER 28 (to 42)
> 14 STORE_NAME 2 (i)
>
> 3 16 LOAD_CONST 2 ('hi')
> 18 LOAD_NAME 3 (str)
> 20 LOAD_NAME 0 (thing)
> 22 CALL_FUNCTION 1
> 24 COMPARE_OP 2 (==)
> 26 POP_JUMP_IF_FALSE 12
>
> 4 28 LOAD_NAME 4 (print)
> 30 LOAD_CONST 3 ('found')
> 32 CALL_FUNCTION 1
> 34 POP_TOP
>
> 5 36 POP_TOP
> 38 JUMP_ABSOLUTE 42
> 40 JUMP_ABSOLUTE 12
> >> 42 LOAD_CONST 4 (None)
> 44 RETURN_VALUE
>
> I note that line 22 calls the function str repeatedly, and no
> optimization is done here.
>
> # file "dis2.py"
> thing = 123
> strthing = str(thing)
> for i in range(10):
> if "hi" == strthing:
> print("found")
> break
>
> This generates bytecode
>
> 1 0 LOAD_CONST 0 (123)
> 2 STORE_NAME 0 (thing)
>
> 2 4 LOAD_NAME 1 (str)
> 6 LOAD_NAME 0 (thing)
> 8 CALL_FUNCTION 1
> 10 STORE_NAME 2 (strthing)
>
> 3 12 LOAD_NAME 3 (range)
> 14 LOAD_CONST 1 (10)
> 16 CALL_FUNCTION 1
> 18 GET_ITER
> >> 20 FOR_ITER 24 (to 46)
> 22 STORE_NAME 4 (i)
>
> 4 24 LOAD_CONST 2 ('hi')
> 26 LOAD_NAME 2 (strthing)
> 28 COMPARE_OP 2 (==)
> 30 POP_JUMP_IF_FALSE 20
>
> 5 32 LOAD_NAME 5 (print)
> 34 LOAD_CONST 3 ('found')
> 36 CALL_FUNCTION 1
> 38 POP_TOP
>
> 6 40 POP_TOP
> 42 JUMP_ABSOLUTE 46
> 44 JUMP_ABSOLUTE 20
> >> 46 LOAD_CONST 4 (None)
> 48 RETURN_VALUE
>
> In short, it seems the cpython interpreter doesn't (currently) perform
> this sort of optimization.
>
It can't optimise that because, say, 'print' could've been bound to a
function that rebinds 'str'.

Re: Quick question about CPython interpreter

<optimization-20221017174026@ram.dialup.fu-berlin.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19945&group=comp.lang.python#19945

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: Quick question about CPython interpreter
Date: 17 Oct 2022 16:41:40 GMT
Organization: Stefan Ram
Lines: 36
Expires: 1 Sep 2023 11:59:58 GMT
Message-ID: <optimization-20221017174026@ram.dialup.fu-berlin.de>
References: <lll2L.218851$3AK7.48447@fx35.iad> <Y014PxsyRtA7eaa/@icerm-dld> <a14f7ff0-ad1f-503f-017a-2e58f27801e4@mrabarnett.plus.com> <mailman.741.1666023944.20444.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de ugbSFDW0kIReUsl5zQqtBgDx2ii++BCKIi0pzwGgrA2PQD
X-Copyright: (C) Copyright 2022 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Mon, 17 Oct 2022 16:41 UTC

MRAB <python@mrabarnett.plus.com> writes:
>It can't optimise that because, say, 'print' could've been bound to a
>function that rebinds 'str'.

It would be possible to find out whether a call of a function
named "print" is to the standard function, but the overhead
to do this in the end might slow down the execution.

In general, it can be possible that there could be optimizer
stages after compilation. So, one might write a small micro-
benchmark to be sure.

import timeit

s = "string text"
dt0 = dt1 = 0

for i in range( 100 ):

start_time = timeit.default_timer()
for _ in range( 1000 ):
l = len( s )
pass
dt0 += timeit.default_timer() - start_time

start_time = timeit.default_timer()
l = len( s )
for _ in range( 1000 ):
pass
dt1 += timeit.default_timer() - start_time

print( f'{dt0 = :.10f} # with "len" in the loop' )
print( f'{dt1 = :.10f} # with "len" outside the loop' )
print( f'{dt0/dt1 = :.2f}' )

Re: Quick question about CPython interpreter

<mailman.743.1666029558.20444.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=19947&group=comp.lang.python#19947

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ros...@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: Quick question about CPython interpreter
Date: Tue, 18 Oct 2022 04:59:05 +1100
Lines: 21
Message-ID: <mailman.743.1666029558.20444.python-list@python.org>
References: <lll2L.218851$3AK7.48447@fx35.iad> <Y014PxsyRtA7eaa/@icerm-dld>
<a14f7ff0-ad1f-503f-017a-2e58f27801e4@mrabarnett.plus.com>
<mailman.741.1666023944.20444.python-list@python.org>
<optimization-20221017174026@ram.dialup.fu-berlin.de>
<CAPTjJmrMznRa_Yk56+wGgdJ_fcU_Swmj7qhpuaOF2n1YtAz6JA@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de O2xV6mHMAgsI6HUNIpcKWQMX7AgnBebuafsSilRxHLig==
Return-Path: <rosuav@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=UjPveyC0;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.003
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '(which': 0.04; '2022':
0.05; 'ram': 0.07; 'general,': 0.09; 'overhead': 0.09; 'writes:':
0.09; 'because,': 0.16; 'chrisa': 0.16; 'easiest': 0.16;
'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16;
'received:209.85.218': 0.16; 'slow': 0.16; 'subject:CPython':
0.16; 'subject:interpreter': 0.16; 'wrote:': 0.16;
'subject:question': 0.17; "can't": 0.17; 'tue,': 0.19; 'to:addr
:python-list': 0.20; 'stefan': 0.26; "isn't": 0.27; 'function':
0.27; "doesn't": 0.32; 'message-id:@mail.gmail.com': 0.32; 'but':
0.32; 'there': 0.33; 'core': 0.34; 'header:In-Reply-To:1': 0.34;
'received:google.com': 0.34; 'from:addr:gmail.com': 0.35;
'change': 0.36; "it's": 0.37; 'received:209.85': 0.37; 'way':
0.38; 'could': 0.38; 'received:209': 0.39; "you'd": 0.64; 'down':
0.64; 'named': 0.65; 'subject:Quick': 0.76; 'optimise': 0.84;
'say,': 0.84; 'sure.': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=to:subject:message-id:date:from:in-reply-to:references:mime-version
:from:to:cc:subject:date:message-id:reply-to;
bh=Wadg+X9WwCMNN5800/hDtQwsO8dv8BTBsxXtfZJQihA=;
b=UjPveyC0EWR4vbpnOToe/q1I5RzMHCS76ou/SmHJ6i89G4lifhuTJ9esKtP6f6H/5L
rb8yQKVPnIcHx8nUypfHrjgPoIqiiNrI+OG5XX860U0sr+WbyuEv1CRuRgOvvq8WR4lh
2OoRClrIB1fBnLcvwJUfI4xhxIpmDRe8s5+n12ggvgo6q9B0ujIpyLMrnbYElRKHEB3L
Z0C+9kyFxCgG8XL1hrlVsSeU7Urw4xbrSd3PtooMX8dtOdYxBd5EtN3C2Dasa2Pu38DV
IBIONg1JLr06ZjIv61xGFY0QQ5II/uVKKBqKbEuMTk4Gqik5FzcD+wh2tkmY2IphqlN8
2W0Q==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=to:subject:message-id:date:from:in-reply-to:references:mime-version
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
bh=Wadg+X9WwCMNN5800/hDtQwsO8dv8BTBsxXtfZJQihA=;
b=lhBVrFd6GqRDgy7owLMm+HngNLq4hCoWtWcQ2bcHyi61J40nWtJegP9eUOhezxiUUi
8KkgwkzqgEhkrWy7OXwDDMImpu5Tu2NDbg1aqg/KSlOYP401boYNJLM08ueIufascsiA
fiFNgTke83IffIG2kE/2POw3jYQOeTRM7KtyPd+tHm1eBbeozYk0ME3jSJZyGO2C4TOM
lnlVfruwGi3t0TQbUiQmhXKduD1HihiBKKnFqsbNEOYTWHFSGREzSxbCcqXQYjxa3Dm1
8iVNah/Ny66SckGOZeLVp3JPQ++LtvT2hhTkN8bU9PnHewiQ2ux+Pm2MDw4prplgoP1n
LNQg==
X-Gm-Message-State: ACrzQf1EXwdUXu3mE/wmOerAqaN/5gemvp32kGhAkFs2RRjsximW4aa5
L3mJ1Nbq/MVYg/vw+cvwhd0zz3YRP3Mv8KJE4589PtqF
X-Google-Smtp-Source: AMsMyM5muLa2HnKT2GZgoqmmp/P4ZIfZpzz22tBpnTL+FLSP7OouoqFD12aIL4Tb3adtjoqRlCMNnHoNd6LLN+jT7y8=
X-Received: by 2002:a17:906:7313:b0:78e:c2a:a3fa with SMTP id
di19-20020a170906731300b0078e0c2aa3famr9343979ejc.581.1666029556695; Mon, 17
Oct 2022 10:59:16 -0700 (PDT)
In-Reply-To: <optimization-20221017174026@ram.dialup.fu-berlin.de>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
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: <https://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>
X-Mailman-Original-Message-ID: <CAPTjJmrMznRa_Yk56+wGgdJ_fcU_Swmj7qhpuaOF2n1YtAz6JA@mail.gmail.com>
X-Mailman-Original-References: <lll2L.218851$3AK7.48447@fx35.iad>
<Y014PxsyRtA7eaa/@icerm-dld>
<a14f7ff0-ad1f-503f-017a-2e58f27801e4@mrabarnett.plus.com>
<mailman.741.1666023944.20444.python-list@python.org>
<optimization-20221017174026@ram.dialup.fu-berlin.de>
 by: Chris Angelico - Mon, 17 Oct 2022 17:59 UTC

On Tue, 18 Oct 2022 at 03:51, Stefan Ram <ram@zedat.fu-berlin.de> wrote:
>
> MRAB <python@mrabarnett.plus.com> writes:
> >It can't optimise that because, say, 'print' could've been bound to a
> >function that rebinds 'str'.
>
> It would be possible to find out whether a call of a function
> named "print" is to the standard function, but the overhead
> to do this in the end might slow down the execution.
>
> In general, it can be possible that there could be optimizer
> stages after compilation. So, one might write a small micro-
> benchmark to be sure.
>

You'd also have to ensure that the stringification of the ID doesn't
change (which it can it it isn't a core data type), and the easiest
way to do THAT is to call str() on the ID every time and see if it's
the same...

ChrisA

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor