简体   繁体   English

使用C ++将STDIN数据读入控制台应用程序的最快方法是什么

[英]What is the fastest method to read STDIN data into console app with C++

I have a console app written with C++ that needs to accept text input from another process via writeline (followed by a deadline.) I'm assuming that such needs to be done via STDIN, but it also needs to work in the fastest way possible. 我有一个用C ++编写的控制台应用程序,该应用程序需要通过writeline接受来自另一个进程的文本输入(最后期限)。我假设需要通过STDIN完成,但它也需要以最快的方式工作。 My console app then needs to reply to the process back. 然后,我的控制台应用程序需要回复该过程。

I haven't done console programming for awhile now, but I remember from C classes at school that there's a lot of C-type functions, like fgets , getline , etc. but what I remember is that they seemed to be quite slow. 我已经有一段时间没有做控制台编程了,但是我记得在学校的C类中,有很多C型函数,例如fgetsgetline等,但是我记得它们似乎很慢。

So is there any way to do this exchange (again, "quick in and then out") with WinAPIs? 那么,有什么方法可以与WinAPI进行这种交换(再次“先快速然后退出”)?

The fastest method in theory will almost certainly be the system level input routines, since both the stdin (in C, but also available in C++) and std::cin build on these. 理论上最快的方法几乎可以肯定是系统级输入例程,因为stdin (在C中,但在C ++中也可用)和std::cin基于这些。 On the other hand, they have generally been optimized for the platform, so unless you can find the optimal configuration yourself (eg things like buffer size), you might not gain much, if anything: calling read (Unix) or ReadFile (Windows) for each character will probably be slower than using something like std::getline . 另一方面,它们通常已针对平台进行了优化,因此,除非您自己找到最佳配置(例如,缓冲区大小之类的东西),否则您可能不会获得太大收益:调用read (Unix)或ReadFile (Windows)每个字符可能比使用std::getline这样的东西要慢。

The other question is what you plan on doing with the data after you've read it. 另一个问题是读取数据后您打算如何处理这些数据。 Functions like read or ReadLine give you a buffer ( char[] ) with a certain number of characters; 诸如readReadLine类的函数为您提供了具有一定数量char[]的缓冲区( char[] )。 you then have to analyse it, break it into lines, etc. Functions like std::getline give you an std::string with the line in it. 然后,您必须对其进行分析,将其分成几行,等等。诸如std::getline类的函数会为您提供带有其中一行的std::string If you're a really skilled C++ programmer, you can probably organize things so that the actual data are never moved from the char[] , but this would require re-implementing a lot of things that are already implemented in the standard library. 如果您是一位真正熟练的C ++程序员,则可以组织一些事情,以使实际数据永远不会从char[]移出,但这将需要重新实现许多已经在标准库中实现的事情。 The use of templates in the standard library means that you don't have to implement nearly as much as you would have to otherwise, but you'll still need to create an equivalent to std::string which maintains two iterators ( char const* ) rather than the data itself. 在标准库中使用模板意味着您不必像以前那样实现那么多的实现,但是您仍然需要创建一个与std::string等效的东西,它维护着两个迭代器( char const* ),而不是数据本身。

In the end, I'd start by writing the application using std::getline and std::string . 最后,我将从使用std::getlinestd::string编写应用程序开始。 Once it's working, I'd see what its actual performance is, and only then, if necessary, consider ways of improving it. 一旦开始运行,我就会看到它的实际性能,只有在必要时,才考虑改进它的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM