Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
想要在同一个python脚本中比较bash和python中的时间吗_Python_Linux_Bash_Time - Fatal编程技术网

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

想要在同一个python脚本中比较bash和python中的时间吗,python,linux,bash,time,Python,Linux,Bash,Time,好的 我正试着做下一件事: 我自己实现了一个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

好的

我正试着做下一件事:

我自己实现了一个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")
import os
os.system("{ time python rsa.py ; }  2>  /home/spike/tiempos_prueba.txt")
我的最终目标是在10次迭代中比较我的时间(用python计算,quiet-simple)和linux时间(user,real,sys-time),这只是一个例子,因为我需要在100K次迭代中进行比较

这就是问题所在:

time bash -c 'for i in {1..10}; do python blowfish.py; done' 
在shell中工作得很好,但在python脚本中调用它时出现了以下错误(如上所述):

再次感谢

----更新(20/7/1418: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(以防脚本产生一些输出),因为time将其输出写入stderr

从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")
import os
os.system("{ time python rsa.py ; }  2>  /home/spike/tiempos_prueba.txt")

使用
timeit
模块。谢谢,我真的希望至少有来自bash的user+sys时间,我希望在python脚本中比较bash结果和python结果。我认为在调用subprocess.Popen时应该添加“shell=True”,因为重定向是来自shell的函数。