读取一行输入比与fgets快? [英] Read a line of input faster than fgets?

查看:207
本文介绍了读取一行输入比与fgets快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个程序,性能是非常重要的,但不是关键。目前,我在文本由行 FILE * 行读,我用与fgets 来获取每一行。使用一些性能工具后,我发现我的应用程序运行时间的20%到30%,这里面与fgets

I'm writing a program where performance is quite important, but not critical. Currently I am reading in text from a FILE* line by line and I use fgets to obtain each line. After using some performance tools, I've found that 20% to 30% of the time my application is running, it is inside fgets.

有没有更快的方法来获得一行文本?我的应用程序是单线程的,没有打算使用多线程。输入可以是从标准输入或文件。先谢谢了。

Are there faster ways to get a line of text? My application is single-threaded with no intentions to use multiple threads. Input could be from stdin or from a file. Thanks in advance.

推荐答案

你不说你是哪个平台上,但如果它是类UNIX,那么你可能想尝试read()系统调用,不执行缓冲的额外层,它FGETS()等人做的。这可能会加快速度略有下降,而另一方面它很可能放慢改革的步伐 - 找出的唯一方法是吮吸它,看看

You don't say which platform you are on, but if it is UNIX-like, then you may want to try the read() system call, which does not perform the extra layer of buffering that fgets() et al do. This may speed things up slightly, on the other hand it may well slow things down - the only way to find out is to suck it and see.

这篇关于读取一行输入比与fgets快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆