我期望从 Python 脚本生成的 dash 命令的执行时间应该大于从 dash 脚本生成的时间,这有什么问题吗?
我想知道的是:
从 Python 脚本中运行 dash 命令比从 dash 脚本中运行 dash 命令花费的时间更少,对此有何解释?
我的计算原理思维模型有什么问题,无法解释观察到的结果?
为什么使用 dash 打印比使用 Python 打印花费的时间长几个数量级?时间测量方法有问题吗? Python 是否会“欺骗”生成打印过程而不等待其完成,而 dash 不会?
~ $ bash _O--printWelcomeTo-oOo_DEMO--_oo--timing,shPrgLang--_oo0.sh
RUNNING /usr/bin/dash as script code interpreter:
dash printf : Welcome to oOo ! -> 0.001505162
dash echo -n: Welcome to oOo ! -> 0.001601468
python print: Welcome to oOo ! -> 0.035592121
0.041928319
0.043502767
~ $ python _O--printWelcomeTo-oOo_DEMO--_oo--timing,pyPrgLang--_oo0.py
RUNNING /usr/bin/python as script code interpreter:
python print: Welcome to oOo ! -> 0.000005164
dash printf : Welcome to oOo ! -> 0.001164751
dash echo -n: Welcome to oOo ! -> 0.001131456
0.002357228
0.002401434
~ $ cat _O--printWelcomeTo-oOo_DEMO--_oo--timing,shPrgLang--_oo0.sh
#!/usr/bin/dash
t000=$( date +"%s%N" )
T() { date +"%s%N"; } # big integer with time unit of nanosecond
echo " RUNNING /usr/bin/dash as script code interpreter: "
t0=$(T)
# --
printf "dash printf : Welcome to oOo ! -> "
# --
t1=$(T) # big integer with time unit of nanosecond
t00="$t0"
tdiff=$(( t1 - t0 ))
printf "%d.%09d\n" $(( tdiff / 1000000000 )) $(( tdiff % 1000000000 ))
# ==================================================
t0=$(T) # big integer with time unit of nanosecond
# --
echo -n "dash echo -n: Welcome to oOo ! -> "
# --
t1=$(T) # big integer with time unit of nanosecond
tdiff=$(( t1 - t0 ))
printf "%d.%09d\n" $(( tdiff / 1000000000 )) $(( tdiff % 1000000000 ))
# ==================================================
t0=$(T) # big integer with time unit of nanosecond
# --
python -c 'print( "python print: Welcome to oOo ! -> ", end ="" ) '
# --
t1=$(T) # big integer with time unit of nanosecond
tdiff=$(( t1 - t0 ))
printf "%d.%09d\n" $(( tdiff / 1000000000 )) $(( tdiff % 1000000000 ))
# ==================================================
tdiff=$(( t1 - t00 ))
printf " %d.%09d\n" $(( tdiff / 1000000000 )) $(( tdiff % 1000000000 ))
tdiff=$(( t1 - t000 ))
printf " %d.%09d\n" $(( tdiff / 1000000000 )) $(( tdiff % 1000000000 ))
~ $ cat _O--printWelcomeTo-oOo_DEMO--_oo--timing,pyPrgLang--_oo0.py
#!/usr/bin/python
from time import perf_counter as T
T0=T()
from os import system as shellCmd
print( " RUNNING /usr/bin/python as script code interpreter: " )
t0=T() # time as float with nanosecond precision
t00=t0
# --
print( "python print: Welcome to oOo ! -> ", end='')
# --
t1=T() # big integer with time unit of nanosecond
tdiff=t1 - t0
print( f'{tdiff:.9f}' )
# =====================================
t0=T() # time as float with nanosecond precision
# --
shellCmd( 'printf "dash printf : Welcome to oOo ! -> "' )
# --
t1=T() # big integer with time unit of nanosecond
tdiff=t1 - t0
print( f'{tdiff:.9f}' )
t0=T() # time as float with nanosecond precision
# --
shellCmd( 'echo -n "dash echo -n: Welcome to oOo ! -> "' )
# --
t1=T() # big integer with time unit of nanosecond
tdiff=t1 - t0
print( f'{tdiff:.9f}' )
tdiff=t1 - t00
print( f' {tdiff:.9f} ' )
tdiff=t1 - T0
print( f' {tdiff:.9f} ' )
~ $
我期望从 Python 脚本生成的 dash 命令的执行时间应该大于从 dash 脚本生成的时间,这有什么问题吗?
这个期望并没有错。
从 Python 脚本中运行 dash 命令比从 dash 脚本中运行 dash 命令花费的时间更少,对此有何解释?
您假设您实际上是在破折号中测量命令执行的时间,这是不正确的。您在提供的代码中实际测量的是
date
命令本身所花费的时间,与 print
和/或 echo
所花费的时间相比,它非常慢。如果您通过测量两个后续 date
调用之间的时间跨度来补偿此时间跨度,您会发现通过时间增量校正结果获得的结果在一定程度上有所不同,使得 print
和 echo
的时间测量毫无用处(正如Charles Duffy对问题的评论中提到的)。所以没什么好解释的。与从 Python 脚本 os.system()
调用运行相比,由 dash 解释器运行的 dash 命令实际上运行得更快。