命令行实用程序可以比 C++ 更快吗?

发布于 2024-11-18 13:52:18 字数 158 浏览 2 评论 0原文

我有一个项目想要操作某些输出文件。

这可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

与往事干杯 2024-11-25 13:52:18

从技术角度来看,一个编写良好的独立 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...

顾铮苏瑾 2024-11-25 13:52:18

如果我是您,请使用已有的,因为这些可能已经存在很长时间并且已经过测试和尝试。自己编写一个新程序来完成同样的事情似乎是一种重新发明轮子类型的操作,并且很容易出错。

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.

彩虹直至黑白 2024-11-25 13:52:18

如果您确实需要比管道更快的性能,您可以下载 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.

○愚か者の日 2024-11-25 13:52:18

如果您是一名非常优秀的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文