git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

【Regarding Performance of a Python Script....】


On 28Feb2020 19:24, Kenzi <kenzi.sml at gmail.com> wrote:
> I have a question regarding a simple code snippet in Python:
>
>from subprocess import check_output
>for i in range(1024):
>    check_output(['/bin/bash', '-c', 'echo 42'], close_fds=True)
>
>*I wonder why running it in Python 3.7 is much faster than Python 2.7? *
>(Python 3.7 is still faster, after I used *xrange * in Python 2.7)

On a lot of UNIX systems you can use the strace command to inspect what 
is happening. I am personally surprised there's a significant difference 
between the Pythons because I'd expect the expense of invoking bash to 
dominate the cost here (consider using /bin/sh, which on many systems 
isn't bash but something smaller/faster for noninteractive shell 
scripting).

Write a script running just the check_output() call one, and strace it:

    strace python -c "from subprocess import check_output; check_output(['/bin/bash', '-c', 'echo 42'], close_fds=True)"

See if your 2 Pythons are doing something difference at the system call 
level.

Cheers,
Cameron Simpson <cs at cskk.id.au>