sys.stdin.readline() 和 input():读取输入行时哪个更快,为什么? [英] sys.stdin.readline() and input(): which one is faster when reading lines of input, and why?

查看:65
本文介绍了sys.stdin.readline() 和 input():读取输入行时哪个更快,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我需要从 STDIN 获取输入行时,我正在尝试决定使用哪一个,所以我想知道在不同情况下我需要如何选择它们.

I'm trying to decide which one to use when I need to acquire lines of input from STDIN, so I wonder how I need to choose them in different situations.

我找到了以前的帖子(https://codereview.stackexchange.com/questions/23981/how-to-optimize-this-simple-python-program) 说:

I found a previous post (https://codereview.stackexchange.com/questions/23981/how-to-optimize-this-simple-python-program) saying that:

如何在使用的时间和内存方面优化此代码?请注意,我使用不同的函数来读取输入,因为 sys.stdin.readline() 在读取字符串时是最快的,而在读取整数时是 input().

How can I optimize this code in terms of time and memory used? Note that I'm using different function to read the input, as sys.stdin.readline() is the fastest one when reading strings and input() when reading integers.

那句话是真的吗?

推荐答案

内置的 inputsys.stdin.readline 函数做的事情并不完全一样,哪个更快可能取决于您正在做什么的细节.正如 aruisdante 所评论的,Python 3 中的差异比 Python 2 中的要小,当您提供的引用来自,但仍有一些差异.

The builtin input and sys.stdin.readline functions don't do exactly the same thing, and which one is faster may depend on the details of exactly what you're doing. As aruisdante commented, the difference is less in Python 3 than it was in Python 2, when the quote you provide was from, but there are still some differences.

第一个区别是 input 有一个可选的提示参数,如果解释器以交互方式运行,将显示该参数.这会导致一些开销,即使提示为空(默认).另一方面,如果您确实需要提示,它可能比在每次 readline 调用之前执行 print 更快.​​

The first difference is that input has an optional prompt parameter that will be displayed if the interpreter is running interactively. This leads to some overhead, even if the prompt is empty (the default). On the other hand, it may be faster than doing a print before each readline call, if you do want a prompt.

下一个区别是 input 从输入的末尾去掉任何换行符.如果你无论如何都要去掉它,让 input 为你做这件事可能会更快,而不是做 sys.stdin.readline().strip().

The next difference is that input strips off any newline from the end of the input. If you're going to strip that anyway, it may be faster to let input do it for you, rather than doing sys.stdin.readline().strip().

最后一个区别是如何指示输入的结尾.如果没有更多输入(stdin 在另一端已关闭),当您调用它时 input 将引发 EOFError.sys.stdin.readline 另一方面,将在 EOF 处返回一个空字符串,您需要知道该字符串进行检查.

A final difference is how the end of the input is indicated. input will raise an EOFError when you call it if there is no more input (stdin has been closed on the other end). sys.stdin.readline on the other hand will return an empty string at EOF, which you need to know to check for.

还有第三种选择,使用 sys.stdin 上的文件迭代协议.这可能很像调用 readline,但它的逻辑可能更好.

There's also a third option, using the file iteration protocol on sys.stdin. This is likely to be much like calling readline, but perhaps nicer logic to it.

我怀疑虽然您的各种选项之间可能存在性能差异,但它们很可能比简单地从磁盘读取文件(如果它很大)并执行您正在做的任何事情的时间成本要小.我建议你避免过早优化的陷阱,只做对你的问题最自然的事情,如果程序太慢(太慢"是非常主观的),你做一些分析,看看什么是最重要的时间.除非真的很重要,否则不要花太多精力在不同的输入方式之间做出决定.

I suspect that while differences in performance between your various options may exist, they're liky to be smaller than the time cost of simply reading the file from the disk (if it is large) and doing whatever you are doing with it. I suggest that you avoid the trap of premature optimization and just do what is most natural for your problem, and if the program is too slow (where "too slow" is very subjective), you do some profiling to see what is taking the most time. Don't put a whole lot of effort into deciding between the different ways of taking input unless it actually matters.

这篇关于sys.stdin.readline() 和 input():读取输入行时哪个更快,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆