簡體   English   中英

如何編寫腳本多次運行程序並將時間存儲在文件中?

[英]How to write a script to run a program multiple times and store the time in a file?

我試圖運行一個腳本來運行我用C編寫的程序,這只是一個簡單的排序程序,可以將其與UNIX中的內置函數進行比較。 我想將用戶時間從sort(unix)和我的sort函數中抽出100次,並將其放在excel,csv文件中以比較算法。

當我手動運行它時,我的函數和sort函數都可以工作,但是我不知道如何編寫代碼以自動執行此過程100次。

#!/bin/sh
for i in {1..100}
do
     for t in 01
     do
     echo ===Test $t ====
     time sort -n <tests/$t > tests/$t.expected
     time ./useIntList <tests/$t> tests/$t.observed
     done
done
rm tests/*.expected tests/*.obsereved

我使程序運行了100次,但我不知道如何將用戶時間放入數組並將其打印到文件中。

time將其輸出寫入stderr而不是stdout 它也是一個內置的shell,它在定時之后執行命令,包括I / O重定向。 因此,您需要將time命令放在一個塊中,然后重定向到該塊之外。

您還應該使用#!/bin/bash {1..100}這樣的語法是bash擴展名,當您使用#!/bin/sh時,它可能不可用。

#!/bin/sh
for i in {1..100}
do
     for t in 01
     do
     echo ===Test $t ====
     { time sort -n <tests/$t > tests/$t.expected; } 2>tests/$t.time.expected
     { time ./useIntList <tests/$t> tests/$t.observed; } 2>tests/$t.time.observed
     done
done
rm tests/*.expected tests/*.obsereved

*.time.*文件將包含time的輸出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM