开发者

Python - Interactive mode vs. normal invocation of the interpreter

开发者 https://www.devze.com 2023-04-10 04:15 出处:网络
Is there a difference between the two modes in terms of resources, especially memory? I\'m referring to Python in the titl开发者_JS百科e but if there is a common explanation to many interpreted langua

Is there a difference between the two modes in terms of resources, especially memory? I'm referring to Python in the titl开发者_JS百科e but if there is a common explanation to many interpreted languages (Octave, etc...) that would be very helpful.

Thanks


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号