This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Move casting prompt after its validation in _raw_input()
Type: performance Stage: resolved
Components: IO Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: myungsekyo, steven.daprano, taleinat
Priority: normal Keywords: patch

Created on 2019-07-01 09:35 by myungsekyo, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 14502 closed python-dev, 2019-07-01 09:39
Messages (5)
msg346986 - (view) Author: myungsekyo (myungsekyo) * Date: 2019-07-01 09:35
I think it is more efficient to cast `variable prompt` into string after its validation in _raw_input().
Because If it is None or empty string then it is not used. 

```
import timeit

MAX = 10000000


start = timeit.default_timer()
for i in range(MAX):
    _raw_input()

end = timeit.default_timer()

print('estimated : {}'.format(end - start))

```
I tested on 10 millions inputs with above code.
and the result is as follows.

Before:
    estimated : 5.060587857999053
    estimated : 5.0425375679988065
    estimated : 4.850400277999142
    estimated : 4.888060468998447
    estimated : 4.849542597999971
    estimated : 4.822679259999859
    estimated : 5.053791769001691
    estimated : 4.914149145999545
    estimated : 4.9584080040003755
    estimated : 4.944711199001176

After:
    estimated : 4.014042392998817
    estimated : 3.987057284997718
    estimated : 4.081281360999128
    estimated : 4.06813505899845
    estimated : 4.040622504999192
    estimated : 4.1239150339970365
    estimated : 4.174400065003283
    estimated : 4.015272281998477
    estimated : 4.034917910001241
    estimated : 4.08582956799728
msg346989 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2019-07-01 09:59
This is a subtle change with the potential to affect more than just performance. Also, in this case, the performance is likely negligible in every actual use-case.

This might actually be more correct, though. For example, one would expect passing prompt=None to result in no prompt displayed, rather than "None".
msg346990 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2019-07-01 10:08
I don't think that will work. If the user passes a non-string which is falsey, your patch will attempt to write it directly to the stream without converting it to a string.

Try ``_raw_input(0)`` as an example.

Tal Einat:
> one would expect passing prompt=None to result in no prompt displayed

I wouldn't. That's not how getpass works now, or input.

By the way, myungsekyo, that's not the best way to use timeit to time code snippets. You are timing the overhead of the loop, ten million lookups of the name "_raw_input", and most importantly, ten million times that _raw_input halts waiting for input from the user. Surely you didn't sit there hitting Enter over and over again?
msg347003 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2019-07-01 11:28
>> one would expect passing prompt=None to result in no prompt displayed

> I wouldn't. That's not how getpass works now, or input.

Ah, thanks for the input, Steve. In that case, this should likely not be changed.
msg347173 - (view) Author: myungsekyo (myungsekyo) * Date: 2019-07-03 01:02
Thanks to your reviews!
I understood what you mean.
This patch is unnecessary.
I will close this issue.
History
Date User Action Args
2022-04-11 14:59:17adminsetgithub: 81647
2019-07-03 01:02:12myungsekyosetstatus: open -> closed
resolution: not a bug
messages: + msg347173

stage: patch review -> resolved
2019-07-01 11:28:48taleinatsetmessages: + msg347003
2019-07-01 10:08:20steven.dapranosetnosy: + steven.daprano
messages: + msg346990
2019-07-01 09:59:11taleinatsetnosy: + taleinat
messages: + msg346989
2019-07-01 09:39:19python-devsetkeywords: + patch
stage: patch review
pull_requests: + pull_request14318
2019-07-01 09:35:33myungsekyocreate