我有一個腳本在一個星期內運行了幾百萬次,它只是簡單地找到一個名爲file.csv
的CSV文件中的第一個單元格,它準確地匹配$word
,並打印出整行,例如, CSV:如何確定在BASH中哪一行腳本選擇更快?
[email protected]@a machine that does automated work
[email protected]@an animal that lives in the sea
[email protected]@a plant that grows in the forest
如果一個搜索 「樹」,那麼這將是印:
[email protected]@a plant that grows in the forest
這兩種方法得到相同的結果:
awk [email protected] -v pattern="$word" '$1 ~ "^" pattern "$" {print; exit}' file.csv
grep ^[email protected] file.csv | head -1
同樣,這可能是用於檢查CSV第二列中的完全匹配,假設有3列:
awk [email protected] -v pattern="$word" '$2 ~ "^" pattern "$" {print; exit}' file.csv
grep ^.*@[email protected]*@.*$ file.csv | head -1
如果選擇了兩個腳本(例如上面的這個例子),它們總能產生完全相同的輸出結果,那麼我怎樣才能快速確定哪個腳本更快?
這裏應該指出'grep -m 1'肯定會比'grep |在大多數情況下,頭-n 1'。 – tripleee 2014-10-27 11:05:52
此外,第二個'grep'的更好的正則表達式應該是''^ [^ @] * @ $ word @「',它更好地匹配Awk表達式。 – tripleee 2014-10-27 11:08:36