Python - 交互模式与解释器的正常调用

发布于 2024-12-07 20:47:02 字数 92 浏览 0 评论 0原文

两种模式在资源(尤其是内存)方面有区别吗?我在标题中指的是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若无相欠,怎会相见 2024-12-14 20:47:02

看起来交互式进程确实使用了更多的内存:

malkovich@malkovich:/etc$
malkovich@malkovich:/etc$ python -c 'import time; time.sleep(20000)' &
[1] 3559
malkovich@malkovich:/etc$ pidstat -r -p $!
Linux 2.6... (malkovich)        11-10-01        _x86_64_        (4 CPU)

08:11:41 PM       PID  minflt/s  majflt/s     VSZ    RSS   %MEM  Command
08:11:41 PM      3559      0.00      0.00   27872   4412   0.12  python
malkovich@malkovich:/etc$ kill %1
malkovich@malkovich:/etc$
[1]+  Terminated              python -c 'import time; time.sleep(20000)'

malkovich@malkovich:/etc$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> time.sleep(20000)
^Z
[1]+  Stopped                 python
malkovich@malkovich:/etc$ jobs -p
3881
malkovich@malkovich:/etc$ pidstat -r -p 3881
Linux 2.6... (malkovich)        11-10-01        _x86_64_        (4 CPU)

08:16:10 PM       PID  minflt/s  majflt/s     VSZ    RSS   %MEM  Command
08:16:10 PM      3881      0.00      0.00   34856   5072   0.14  python

RSS(驻留内存使用量)值相比,这是一个有趣的值:交互式进程多了大约 650 kB。

我预计这个值(差异)会随着使用而有所增加,但不会显着增加,这只是因为交互式会话中提供的命令历史记录和其他细节。我认为这不会有显着差异,但您可能想针对您的特定情况运行类似的测试。要使正在运行的解释会话后台运行,您只需按 ^Z (CTRL-Z)。

但总的来说,我认为差异不会很大,除非您在只有几 MB RAM 的嵌入式系统上运行。

请注意,如果您将代码编写为模块然后导入它,它将被编译为字节码并保存。我相信这会减少内存消耗,并减少后续调用的启动时间。您可能需要运行一些测试来了解其中的差异。

It looks like an interactive process does use somewhat more memory: compare

malkovich@malkovich:/etc$
malkovich@malkovich:/etc$ python -c 'import time; time.sleep(20000)' &
[1] 3559
malkovich@malkovich:/etc$ pidstat -r -p $!
Linux 2.6... (malkovich)        11-10-01        _x86_64_        (4 CPU)

08:11:41 PM       PID  minflt/s  majflt/s     VSZ    RSS   %MEM  Command
08:11:41 PM      3559      0.00      0.00   27872   4412   0.12  python
malkovich@malkovich:/etc$ kill %1
malkovich@malkovich:/etc$
[1]+  Terminated              python -c 'import time; time.sleep(20000)'

with

malkovich@malkovich:/etc$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> time.sleep(20000)
^Z
[1]+  Stopped                 python
malkovich@malkovich:/etc$ jobs -p
3881
malkovich@malkovich:/etc$ pidstat -r -p 3881
Linux 2.6... (malkovich)        11-10-01        _x86_64_        (4 CPU)

08:16:10 PM       PID  minflt/s  majflt/s     VSZ    RSS   %MEM  Command
08:16:10 PM      3881      0.00      0.00   34856   5072   0.14  python

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文