Python - 交互模式与解释器的正常调用
两种模式在资源(尤其是内存)方面有区别吗?我在标题中指的是 Python,但如果对许多解释语言(Octave 等)有一个通用的解释,那将非常有帮助。
谢谢
Is there a difference between the two modes in terms of resources, especially memory? I'm referring to Python in the title but if there is a common explanation to many interpreted languages (Octave, etc...) that would be very helpful.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来交互式进程确实使用了更多的内存:
与
RSS(驻留内存使用量)值相比,这是一个有趣的值:交互式进程多了大约 650 kB。
我预计这个值(差异)会随着使用而有所增加,但不会显着增加,这只是因为交互式会话中提供的命令历史记录和其他细节。我认为这不会有显着差异,但您可能想针对您的特定情况运行类似的测试。要使正在运行的解释会话后台运行,您只需按
^Z
(CTRL-Z)。但总的来说,我认为差异不会很大,除非您在只有几 MB RAM 的嵌入式系统上运行。
请注意,如果您将代码编写为模块然后导入它,它将被编译为字节码并保存。我相信这会减少内存消耗,并减少后续调用的启动时间。您可能需要运行一些测试来了解其中的差异。
It looks like an interactive process does use somewhat more memory: compare
with
The RSS (resident memory usage) value is the one that's interesting: about 650 kB more for the interactive process.
I would expect this value (the difference) to increase somewhat, but not significantly, with use, only because of the command history and other niceties provided in an interactive session. I don't think it would ever be a significant difference, but you may want to run similar tests for your particular situation. To background the running interpretive session, you literally press
^Z
(CTRL-Z).But overall, I don't think that the difference will be significant unless you are running on an embedded system with only a few MB of RAM.
Note that if you write your code as a module and then import it, it will be compiled to bytecode and saved. This will I believe reduce memory consumption and also decrease startup time on subsequent invocations. You might want to run some tests to get an idea of the difference.