使用 Grep 在 AC/C++ 中进行模式匹配计划是否可取?
我必须编写一个 C/C++ 程序来处理一堆文本文件(大约 100 个)并找到一个模式(通常是字符串)。由于我要运行的平台是 unix,我想为什么不在我的程序中使用 grep 系统命令,因为它非常快速且有效。但是,我的朋友说在程序中使用 system("grep...") 是不可取的。他建议我使用字符串模式匹配算法,我觉得这会减慢程序速度。
所以,我想就此得到一些建议。帮帮我吧。
I have to write a C/C++ program to process a bunch of text files (around 100) and find a pattern (commonly a string). Since the platform I am going to run this will be unix, I thought why wouldn't I make use of the grep system command within my program as it is very fast and effective. But, my friend says using system("grep...") within a program is not advisable. He suggests me to use string pattern matching algorithm which I feel will slow down the program.
So, I want some advice over this. Help me out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果不知道你的程序要做什么,就很难说。但是通过 system() 运行命令会显着降低你的程序速度,尽管这可能并不重要。无论您做什么,如果正则表达式可以解决问题,就不要编写自己的字符串匹配代码 - 使用许多现有的正则表达式库之一。如果您的大部分问题可以使用 grep 解决,请考虑编写 shell 脚本,或使用 Python 等脚本语言而不是 C++ 程序。
Without knowing what your program is going to do, it's hard to say. But running commands via system() will slow your program, down considerably, though this may not be important. Whatever you do, don't write your own string-matching code if regular expressions can solve the problem - use one of the many existing regex libraries. And if most of your problem could be solved using grep, consider writing a shell script, or using a scripting language like Python instead of a C++ program.
您的两个主要选择是 (a) 使用 grep,或 (b) 使用链接到 C 或 C++ 程序的库,该库提供正则表达式。
使用 grep 意味着您的程序很快就能运行,因为您无需学习太多内容。使用正则表达式库意味着您的程序运行得更快。
快多少?主要的速度提升是因为您没有为这 100 个文件中的每一个设置新进程并运行新程序。这种速度节省有多重要?
答案取决于每个文件的大小。如果它们非常大,那么使用哪种方法不会产生太大的速度差异。如果很小的话,会的。
如果您决定使用正则表达式库,我的猜测是它们的速度都差不多。我选择了我熟悉的东西,因为我了解 Perl:Perl 兼容的正则表达式库。
Your two major alternatives are (a) to use grep, or (b) to use a library, linked to your C or C++ program, which provides regular expressions.
Using grep means you get your program running very soon, because you don't have much to learn. Using a regular expression library means your program runs faster.
How much faster? The major speed increase is because you're not setting up a new process and running a new program for each of those 100 files. How significant is this speed saving?
The answer depends on how large each of those files is. If they're very large, it won't make much speed difference which method you use. If small, it will.
If you decide to go with a regular expression library, my guess is that they're all about the same speed. I chose something I was familiar with, since I know Perl: the Perl compatible regular expression library.
make forking 并使用 exec 系列命令使用 grep 并将其结果保存在文件中。
在主要等待过程结束。
然后在 main 中打开文件并使用结果。
make forking and using exec family of command use grep and save its result in a file.
in main wait for process to end.
then in main open the file and use the result.