Linux time command會計算process執行時間,並輸出下列格式的程式執行資訊

           real %e
           user %U
           sys %S

%e, %U, %S會以分鐘+時間的方式輸出

如果要重覆計算同一個程式的多次執行時間並分析,輸出成csv並且統一單位會更方便

這是一個可以輸入一個有多筆time結果的輸入文字檔並將結果轉乘csv的表格的parser

表格的格式如下(單位都為分鐘)

real, user, sys

%e, %U, %S

...

 

程式碼

<pre class="brush: bash">

#!/bin/bash

rm -rf temp.csv

# preprocess source file
    echo -ne `cat $1` > temp
    echo `cat temp` | sed 's/real/\nreal/g' | sed 's/sys/xyx/g' | sed 's/s /,s/g' | sed 's/m/,m/g' | sed 's/[a-z]*//g' | sed 's/ //g' | sed '/^$/d' >> temp.csv

# create result.csv
rm -rf result.csv
echo "real, user, sys" >> speed_result.csv

while read -r line; do

    real_time_m=$(echo $line | cut -d , -f1)
    real_time_s=$(echo $line | cut -d , -f2)
    real_time=$(echo "$(echo "scale=4; $real_time_s / 60.0" | bc) + real_time_m" | bc)

    user_time_m=$(echo $line | cut -d , -f3)
    user_time_s=$(echo $line | cut -d , -f4)
    user_time=$(echo "$(echo "scale=4; $user_time_s / 60.0" | bc) + user_time_m" | bc)

    sys_time_m=$(echo $line | cut -d , -f5)
    sys_time_s=$(echo $line | cut -d , -f6)
    sys_time=$(echo "$(echo "scale=4; $sys_time_s / 60.0" | bc) + sys_time_m" | bc)

    echo "$real-time,$user_time,$sys_time" >> result.csv
done < temp.csv
rm -rf temp.csv

<pre>

參考資料

 

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 夏蟲語冰 的頭像
    夏蟲語冰

    坐井觀天

    夏蟲語冰 發表在 痞客邦 留言(0) 人氣()