我在C ++中使用system()命令执行Python脚本。为了计算执行Python脚本所需的时间,我在C ++代码中添加了以下几行:
auto systemExecutionStart = std::chrono::high_resolution_clock::now();
system(commandString.c_str());
auto systemExecutionEnd = std::chrono::high_resolution_clock::now();
auto systemExecutionDuration = std::chrono::duration_cast<std::chrono::microseconds>(systemExecutionEnd - systemExecutionStart).count();
执行期间,系统执行持续时间为5955678微秒或5.9秒。
但是,我还使用以下方法检查了在Python脚本中执行Python代码所需的时间,即由系统调用的脚本(commandString.c_str()):
import timeit
startC = timeit.default_timer() #at the beginning of Python code
stopC = timeit.default_timer() #at the end of Python code
print('Time Python Code: ', stopC - startC)
打印语句的输出为0.8500979244709015
为什么在0.85秒内完成Python代码后,系统命令需要5秒钟才能完成?