想要在同一python脚本中比较bash和python中的时间吗

2024-04-25 16:53:27 发布

您现在位置:Python中文网/ 问答频道 /正文

一切都好

好吧,我想做下一个:

我自己实现了一个RSA密码(它很好用),不使用任何库。我的问题是,如何在python脚本中执行bash-time,并将其与python-time进行比较,并将两个结果都保存到一个文本文件中?在

到目前为止,我得到了这个剧本:

import os
import struct, types, sys, timeit
from time import time
sys.stdout = open("/home/spike/salida_pruebas.txt", "w")
print "I will call this rsa.py"
orsa1 = time()
os.system("python rsa.py")
orsa2 = time()
orsa = orsa2 - orsa1
bashCommand = "time python rsa.py >> /home/spike/tiempos_prueba.txt"
import subprocess
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output = process.communicate()
print output
print "RSA (other algorithm implementarion) encryption is in %f seconds" % (orsa)

但是当我运行这个脚本时,我没有得到时间输出,我得到rsa.py公司输出对我没有好处。在

非常感谢。在

格林尼治标准时间14:24----更新时间:

问题更新:

好吧,第一个问题已经回答了,效果很好,但我有一个关于它的更新。在

现在,我想在python脚本中添加bash for loop,我的bash for loop在shell中可以正常工作,但在python脚本中不起作用:

^{pr2}$

我的最终目标是比较我的时间(用python计算,quiet simple)和linux时间(user,real,sys time)在10次迭代中进行比较,因为我需要进行100K次迭代。在

所以问题是:

time bash -c 'for i in {1..10}; do python blowfish.py; done' 

在shell中工作得很好,但是当我在python脚本(上面)中调用它时,我得到了一个错误:

sh: 1: Syntax error: "do" unexpected (expecting "}")

再次感谢。在

----更新(2014年7月20日18:15 GMT-4)---

发现错误,我忘记在for循环行的末尾添加分号(;):

os.system("{ time bash -c 'for i in {1..10}; do python aes.py; done' ; }  2>> /home/spike/salida_pruebas_10bash.txt")

希望所有这些对其他人有帮助。在

干杯


Tags: pyimporttxt脚本bashhomefortime
1条回答
网友
1楼 · 发布于 2024-04-25 16:53:27

来自bash:

{ time python rsa.py ; }  2>  /home/spike/tiempos_prueba.txt

必须使用花括号,否则还将测量重定向的时间。在

2>;只重定向stderr(以防脚本生成某些输出),因为time会将其输出写入stderr。在

从python脚本:

^{pr2}$

相关问题 更多 >

    热门问题