且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

这是在AWK打印的最快方法

更新时间:2022-12-27 19:52:08

原因你的程序很慢是因为印刷的不是。因为你调用 NAWK 的新副本 $ NUMBERS 的每一个元素,你的程序很慢。这是非常浪费的,你应该从一开始就重新考虑你的方案设计。您似乎大多是想看看哪些号码从一个列表在第二个列表存在。如果你想这样做的NAWK,你应该先阅读整个第一列表,第二个文件读取每个号码前在关联数组中的元素存储。

The reason your program is slow is not because of printing. Your program is slow because you invoke a new copy of nawk for every element of $NUMBERS. This is very wasteful and you should rethink your program design from the beginning. It appears you are mostly trying to see which numbers from one list exist in a second list. If you want to do this in nawk, you should read the entire first list first, and store the elements in an associative array before reading each number from the second file.

您也许可以更清晰地解决这个问题,使用加入的grep

You could probably solve this problem more cleanly using join or grep.

编辑:这是一个使用的grep 工作方案。这比你原来的 shellfun()

Here's a working solution using grep. It's at least 20x faster than your original shellfun().

shellfun2() {
    echo $XNUMBERS | tr ' ' '\n' | cut -d '|' -f1 \
        | grep -f <(echo $NUMBERS | tr ' ' '\n') | rev
}

它的工作方式是采取一切从 $ XNUMBERS 数字管道之前(因此 12 | 21 34 | 43 变成 12 \\ N34 ),然后通过管道那些的grep -f 参数为全 $号。这意味着我们搜索所有 $ XNUMBERS 的左侧面,和打印的比赛,我们在 $ NUMBERS内只需使用扭转他们。我们不需要 $ XNUMBERS 的右手边在所有(所以也许你甚至可以停在首位生成它们,节省更多的时间)。

The way it works is to take all the numbers from $XNUMBERS before the pipes (so 12|21 34|43 becomes 12\n34), then pipe those to grep with the -f argument being all of $NUMBERS. This means we search for all the left-hand sides of $XNUMBERS within $NUMBERS, and after printing the matches we simply use rev to reverse them. We don't need the right-hand sides of $XNUMBERS at all (so maybe you can even stop generating them in the first place, saving more time).

编辑>转上面这一点:

Since you've now told us you are running on Solaris instead of Linux, you don't have rev, so you can replace rev in the above with this:

sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'

和可以更换的grep 的/ usr / XPG4 /斌/ grep的来获得一个增强版支持 -f

And you can replace grep with /usr/xpg4/bin/grep to get an enhanced version that supports -f.