[英] cat

查看:65
本文介绍了猫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了K& R'的ANSI C v2的部分内容,这就是他们的猫看起来像/ b $ b喜欢但是当我将这段代码的速度与gnu cat进行比较时,似乎

很慢。如何优化此速度以获得更高的速度?是否有一个

替代算法?


void catfile(FILE * in,FILE * out){

register int num_char;


/ *获取字符* /

while((num_char = getc(in))!= EOF){

/ *打印到标准输出* /

putc(num_char,out);

}

}


谢谢。

解决方案

Jag< ta ******* @ gmail.comwrites:


我已经阅读了部分K& R'的ANSI C v2,这就是他们的猫看起来像

喜欢但是当我将这段代码的速度与gnu进行比较时猫,看来好像很慢。如何优化此速度以获得更高的速度?是否有一个

替代算法?


void catfile(FILE * in,FILE * out){

register int num_char;


/ *获取字符* /

while((num_char = getc(in))!= EOF){

/ *打印到标准输出* /

putc(num_char,out);

}

}



这个例子的目的只是:一个例子。它给出了一个简单明了的复制方法。


如果你看看GNU cat的源代码,你会看到它无处可靠

近乎清晰简洁(这在很大程度上归功于事实

它必须做得比上述更多例如,

包括一些需要对输入进行一些处理的选项。


支持线程安全选项的POSIX系统必须锁定
每次调用getc()或putc()时,
I / O流。在这样的系统上,

getc_unlocked()和putc_unlocked()(POSIX选项,而不是标准C)

通常会明显更快。一些实现还提供了使用未锁定的其他方法。版本。


其他常见的技术是一次读取更大的块,

或者可能是为了避免标准C缓冲的I / O调用和使用POSIX

read(),write()(我不清楚这比使用setvbuf()关闭缓冲并使用fwrite更好()和fread(),

这是一个很好的选择,如果你想坚持标准C)。


看一看GNU cat和其他类似的源代码

实用程序,如dd,用于(特定于Unix)关于如何提高效率的想法

效率。


-

HTH,

Micah J. Cowan

程序员,音乐家,排版爱好者,游戏玩家......
http://micah.cowan.name/


Jag写道:


>

我已阅读部分K& R'的ANSI C v2,这是什么他们的猫

看起来像但是当我把这段代码的速度比作gnu

cat时,似乎很慢。我如何优化这个以获得更高的
速度?是否有替代算法?


void catfile(FILE * in,FILE * out){

寄存器int num_char;


/ *获取字符* /

while((num_char = getc(in))!= EOF){

/ *打印到标准输出* /

putc(num_char,out);

}

}



你可以更快找到以下内容:


void catfile(FILE * in,FILE * out){

int ch;


while(EOF!=(ch = getc(in)))putc(out,ch);

}


如果是这样,请考虑原因。


-

[邮件]:Chuck F(cinefalconer at maineline dot net)

[page]:< http ://cbfalconer.home.att.net>

尝试下载部分。


-

通过免费发布来自 http://www.teranews.com 的Usenet帐户


CBFalconer写道:

Jag写道:


>我已经阅读了K& R'的ANSI C v2的部分内容是他们的猫看起来像什么但是当我把这段代码的速度比作gnu
时,它似乎很慢。如何优化此速度以获得更高的速度?是否有替代算法?

void catfile(FILE * in,FILE * out){
注册int num_char;

/ *获取字符* /
while((num_char = getc(in))!= EOF){
/ *打印到标准输出* /
putc(num_char,out);
}
}



您可以更快地找到以下内容:


void catfile(FILE * in,FILE * out){

int ch;


while(EOF!=(ch = getc(in)))putc(out,ch);

}


如果是这样,请考虑原因。



我不明白为什么会更快。据我所知

告诉,唯一的实质性改变就是删除

`register'',这不太可能有所作为 -

如果它确实有所作为,它很可能会使修改后的代码更慢,而不是更快。


(嗯,那里另一个是速度提升,而且它可以是一个很大的代码:没有编译的代码使用非常少的执行时间!再看一下putc()

来电......)


-

Eric Sosman
es ***** @ ieee-dot-org.inva lid


I''ve read parts of K&R''s ANSI C v2 and this is what their cat looked
like but when I compared the speed of this code to gnu cat, it seems
very slow. How do I optimize this for greater speeds? is there an
alternative algorithm?

void catfile(FILE *in, FILE *out) {
register int num_char;

/*Get characters*/
while ((num_char = getc(in)) != EOF) {
/*Print to standard output*/
putc(num_char, out);
}
}

Thanks.

解决方案

Jag <ta*******@gmail.comwrites:

I''ve read parts of K&R''s ANSI C v2 and this is what their cat looked
like but when I compared the speed of this code to gnu cat, it seems
very slow. How do I optimize this for greater speeds? is there an
alternative algorithm?

void catfile(FILE *in, FILE *out) {
register int num_char;

/*Get characters*/
while ((num_char = getc(in)) != EOF) {
/*Print to standard output*/
putc(num_char, out);
}
}

This example was intended to be just that: an example. It gives a
clear, concise method for copying in to out.

If you look at GNU cat''s source code, you''ll see that it''s nowhere
near as clear and concise (this is due in no small part to the fact
that it has to do a good deal more than the above example does,
including some options that require a bit of processing on the input).

POSIX systems that support the thread-safety option have to lock the
I/O streams every time you call getc() or putc(). On such systems,
getc_unlocked() and putc_unlocked() (POSIX options, not Standard C)
will usually be notably faster. Some implementations also provide
other means for using the "unlocked" versions.

Other common techniques would be to read in larger blocks at a time,
or perhaps to avoid the Standard C buffered I/O calls and use POSIX
read(), write() (it''s unclear to me how much better that is than to
use setvbuf() to turn off buffering and use fwrite() and fread(),
which is a good option if you''d like to stick to Standard C).

Taking a look at the source code for GNU cat and other similar
utilities like dd, for (Unix-specific) ideas on how to improve
efficiency.

--
HTH,
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/


Jag wrote:

>
I''ve read parts of K&R''s ANSI C v2 and this is what their cat
looked like but when I compared the speed of this code to gnu
cat, it seems very slow. How do I optimize this for greater
speeds? is there an alternative algorithm?

void catfile(FILE *in, FILE *out) {
register int num_char;

/*Get characters*/
while ((num_char = getc(in)) != EOF) {
/*Print to standard output*/
putc(num_char, out);
}
}

You may find the following faster:

void catfile(FILE *in, FILE *out) {
int ch;

while (EOF != (ch = getc(in))) putc(out, ch);
}

If so, consider why.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com


CBFalconer wrote:

Jag wrote:

>I''ve read parts of K&R''s ANSI C v2 and this is what their cat
looked like but when I compared the speed of this code to gnu
cat, it seems very slow. How do I optimize this for greater
speeds? is there an alternative algorithm?

void catfile(FILE *in, FILE *out) {
register int num_char;

/*Get characters*/
while ((num_char = getc(in)) != EOF) {
/*Print to standard output*/
putc(num_char, out);
}
}


You may find the following faster:

void catfile(FILE *in, FILE *out) {
int ch;

while (EOF != (ch = getc(in))) putc(out, ch);
}

If so, consider why.

I don''t see why it would be faster. As far as I can
tell, the only substantive change is the removal of
`register'', which is unlikely to make a difference --
and if it does make a difference, it''s likely to make
the revised code slower, not faster.

(Well, there''s one other "speed improvement," and it
could be a large one: Code that doesn''t compile uses very
little execution time! Have another look at the putc()
call ...)

--
Eric Sosman
es*****@ieee-dot-org.invalid


这篇关于猫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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