繁体   English   中英

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

[英]Want compare time in bash and python within the same python script

Gday全部,

我试图做下一个:

我有一个由我自己实现的RSA密码(效果很好),没有使用任何库。 我的问题是,我如何在我的python脚本中执行bash时间并与python时间进行比较,然后将这两个结果都保存到文本文件中?

到目前为止,我得到了以下脚本:

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输出,这对我没有好处。

非常感谢。

-----更新(20/7/14 12:35 GMT -4)----

问题更新:

好的,第一个问题已经回答并且效果很好,但是我对此有更新。

现在我想在我的python脚本中添加一个bash for循环,我的bash for循环在shell中工作正常,但在我的python脚本中不起作用:

import os
import struct, types, sys, timeit
from time import time
sys.stdout = open("/home/spike/salida_pruebas_10.txt", "w")
print "First we will run blockciphers, many of the AES finalists"
print "I will call this other program called blowfish.py and test its time"

b1 = time()
for x in range(0, 10):
    os.system("python blowfish.py")
b2 = time()
b = b2 - b1
print "blowfish encryption is in %f seconds in %d iterations" % (b, x+1)
os.system("{ time bash -c 'for i in {1..10}; do python blowfish.py; done' }  2>> /home/spike/salida_pruebas_10.txt")

我的最终目标是在10次迭代中比较我的时间(以python,安静简单的方式计算)和linux时间(用户,真实,系统时间),因为我需要进行10万次迭代。

所以这是问题所在:

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

在shell中效果很好,但是当我在python脚本中调用该错误时(上面),我得到了这个错误:

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

再次非常感谢。

----更新(20/7/14 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")

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

干杯

从bash:

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

弯曲的括号是必要的,否则重定向的时间也将被测量。

2>仅重定向stderr(以防您的脚本产生一些输出),因为时间将其输出写入stderr。

从python脚本:

import os
os.system("{ time python rsa.py ; }  2>  /home/spike/tiempos_prueba.txt")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM