命令行实用程序可以比 C++ 更快吗?
我有一个项目想要操作某些输出文件。
这可以使用 grep 和 sed 的组合以及使用 | 进行管道来完成。
或者,我也可以编写一个 C++ 程序来完成同样的事情。
既然 grep 和 sed 应该已经得到相当好的优化,是否有关于哪种方法更快的决定性答案?
I have a project where I want to manipulate certain output files.
This can be accomplished using a combination of grep and sed and piping with |
Alternatively, I can also write a C++ program to do the same thing.
Is there a conclusive answer on which method will be faster since grep and sed should already be fairly well optimised?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从技术角度来看,一个编写良好的独立 C++ 程序可以完成您需要的一切,比使用通过管道互连的两个(或更多)shell 命令更快,因为不会有 IPC 开销,并且它们可以定制- 根据您的具体需求进行制作和优化。
但是,除非您正在编写一个将 24/7 运行多年的程序,否则您永远不会注意到足够的收益值得付出努力。
预优化的标准规则适用......
From a technical standpoint, a well-written self-contained C++ program that does everything you need will be faster than using two (or more) shell commands interconnected with a pipe, simply because there will be no IPC overhead, and they can be tailor-made and optimized for your exact needs.
But unless you're writing a program that will be run 24/7 for years, you'll never notice enough gain to be worth the effort.
And the standard rules for pre-optimization apply...
如果我是您,请使用已有的,因为这些可能已经存在很长时间并且已经过测试和尝试。自己编写一个新程序来完成同样的事情似乎是一种重新发明轮子类型的操作,并且很容易出错。
If I were you, use what is already out there as these have likely been around a long time and have been tested and tried. Writing a new program yourself to do the same thing seems like a reinventing the wheel type action and is prone to error.
如果您确实需要比管道更快的性能,您可以下载 grep 和 sed 的源代码,并在一个应用程序中根据您的需要对其进行定制(如果您计划分发代码,请注意许可证)。如果您注意到管道的开销(如 Flimzy 提到的),我会感到非常惊讶,所以如果事情真的那么慢,我会开始分析您的应用程序。
If you really need faster performance than you'll get with piping, you can download the source for grep and sed and tailor it to your needs in one application (be wary of licenses if you plan on distributing your code). I'd be highly surprised if you'd even notice the overhead of piping (like Flimzy mentioned), so if things are really that slow I'd start profiling your app.
如果您是一名非常优秀的 C/C++ 程序员并花费很多时间,那么您将能够编写比您的管道更快的程序正在想。但除非在这种情况下性能非常重要以至于您绝对必须这样做,否则您应该使用管道。
It is likely that if you are a very good C/C++ programmer and spend a lot of time, that you will be able to write a program that's faster than the pipeline you're thinking of. But unless performance is so critical in this case that you absolutely must do it this way you should use the pipeline.