1

我有一个用 C++ 编写的控制台应用程序,它需要通过 writeline 接受来自另一个进程的文本输入(然后是截止日期。)我假设这需要通过 STDIN 完成,但它也需要以最快的方式工作. 然后我的控制台应用程序需要回复该过程。

我已经有一段时间没有做过控制台编程了,但我记得在学校的 C 课上,有很多 C 类型的函数,比如fgets,getline等,但我记得它们似乎很慢。

那么有什么方法可以使用 WinAPI 进行这种交换(同样,“快速进入然后退出”)?

4

1 回答 1

1

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. On the other hand, they have generally been optimized for the platform, so unless you can find the optimal configuration yourself (e.g. 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.

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; 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. 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. 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.

In the end, I'd start by writing the application using std::getline and std::string. Once it's working, I'd see what its actual performance is, and only then, if necessary, consider ways of improving it.

于 2013-02-13T09:14:42.717 回答