Efficency [英] Efficency

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

问题描述

我意识到这不是新手或家庭作业的新闻组

问题所以这将是最后一篇文章,直到我变得更加熟悉

语言。 br />

我正在寻找优化一段代码来读取

文本文件中的字符并计算它们的频率。如果有人能够指出我在

方向的话,这将是一个很好的方向。我不是在寻找任何代码

只是几个好网站的链接因为我的时间非常有限

当下。

谢谢,如果这篇文章不合适,请道歉。

Chris

解决方案

Chris Hatton< ch *** ******@blueyonder.co.uk>写道:

我正在寻找优化一段代码来读取
文本文件中的字符并计算它们的频率。如果有人能够指出我在正确的方向上会很棒。我不是在寻找任何
代码只是指向好网站的几个链接,因为我的时间非常有限。




这样的代码,如果以任何合理的方式编写,可能会受到从输入

文件中读取数据的速度的限制。我怀疑优化的潜力很大。

-

int main(void){char p [] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\

\ n",* q =" kl BIcNBFr.NKEzjwCIxNJC" ;; int i = sizeof p / 2; char * strchr(); int putchar(\

); while(* q){i + = strchr(p,* q ++) - p; if(i> =(int)sizeof p)i- = sizeof p-1; putchar(p [i] \

);} return 0;}


在文章< 42 ******** @ nntphost.cis.strath.ac.uk>中,

Chris Hatton< ch ********* @ blueyonder.co.uk>写道:

我意识到这不是新手或家庭作业问题的新闻组,所以这将是最后一篇文章,直到我对语言变得更加熟悉。


好​​吧,这是关于C的讨论。我们不是来教你

语言或做功课,而是提问题(即使是简单的,

如果他们不是那么简单我们已经看到他们INT_MAX次

已经是)通常是一个很好的方法来获得一个有趣的讨论开始了。


我正在寻找优化一段代码来读取
文本文件中的字符并计算它们的频率。如果有人能指出我正确的方向,这将是伟大的。我不是在寻找任何代码
只是指向好网站的几个链接,因为我的时间非常有限。




如前所述,如果您的代码写得合理,那么您就不应该优先选择剩余的优惠。


标准库的'I' / O系统很可能已经尽可能高效地从文件中读取
并缓冲它为您的

字符一次读取代码,所以主要循环将做一些事情

像这样:

--------

从缓冲区获取角色

检查缓冲区是否需要重新填充(通常不会)

用字符做东西

--------

....和做与字符有关的东西部分必须非常好

效率低,因为这个时间足够大,因为与重新填充缓冲区所花费的时间相比,这是非常明显的。

必要。

一般规则是将微优化保留给编译器,

并保留任何其他优化,直到它''显示需要。

这就是说,如果你真的想确保你自己优化的部分能够尽可能高效地进行优化,那就是值得注意的是

字符只是小整数而且数组索引是

也是整数。我会让你弄明白从那里去的地方 - 如果你这么多b $ b还没有看到像我在想的那样,它可能会是

最终变得更有价值,作为一种练习,找到解决问题的新方法(并且希望能给你另一种工具用于以后的问题)

而不是作为优化练习。 (一定要告诉我们[1]你的代码

你已经完成了所以我们可以指出它中的所有问题。如果你是

只是学习语言,会有一堆。)

dave


[1]或者当地的大师,如果你有的话。或者,更好的是,两者。


-

Dave Vandervies dj ****** @ csclub.uwaterloo.ca

我不能接电话并要求Farsley double one one three。嗯,我可以,但我会饿着肚子。

- 可怕的魔鬼修道院中的彼得·科莱特


Chris Hatton写道:< blockquote class =post_quotes>
我意识到这不是新手或家庭作业问题的新闻组,所以这将是最后一篇文章,直到我对这种语言变得更加熟悉。

我正在寻找优化一段代码来读取
文本文件中的字符并计算它们的频率。如果有人能指出我正确的方向,这将是伟大的。我不是在寻找
任何代码只是指向好网站的几个链接,因为我的时间非常有限。




没有任何合适的包含等。需要限制和stdio

至少。


/ *自动归零* /

静态无符号长计数[UCHAR_MAX + 1];

....

int ch;

FILE * f;

....

/ *打开文件等* /

....

while(EOF!=(ch = getc( f)))计数[ch] ++;

....

/ *显示计数内容* /

-

"如果你想通过groups.google.com发布一个后续内容,请不要使用

破坏的回复链接在文章的底部。点击

" show options"在文章的顶部,然后点击

回复在文章标题的底部。 - Keith Thompson


I realize this is not a newsgroup for the total novice or homework
questions so this will be the last post until I become more familiar
with the language.

I''m looking to optimize a piece of code that reads characters from a
text file and counts the frequency of them. If someone could point me in
the right direction that would be great. I''m not looking for any code
just a couple of links to good websites as my time is very limited at
the moment.
thanks, and apologies if this post is not appropriate.
Chris

解决方案

Chris Hatton <ch*********@blueyonder.co.uk> writes:

I''m looking to optimize a piece of code that reads characters from a
text file and counts the frequency of them. If someone could point me
in the right direction that would be great. I''m not looking for any
code just a couple of links to good websites as my time is very
limited at the moment.



Such code, if written in any kind of sensible way, will probably
be limited by the speed that you can read data from the input
file. I doubt there''s much potential for optimization.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}


In article <42********@nntphost.cis.strath.ac.uk>,
Chris Hatton <ch*********@blueyonder.co.uk> wrote:

I realize this is not a newsgroup for the total novice or homework
questions so this will be the last post until I become more familiar
with the language.
Well, it''s for discussion about C. We''re not here to teach you the
language or do your homework, but asking questions (even simple ones,
if they''re not the ones that are so simple we''ve seen them INT_MAX times
already) is usually a good way to get an interesting discussion started.

I''m looking to optimize a piece of code that reads characters from a
text file and counts the frequency of them. If someone could point me in
the right direction that would be great. I''m not looking for any code
just a couple of links to good websites as my time is very limited at
the moment.



As noted already, if your code is written sensibly you shouldn''t have
much optimizing left to do.

The standard library''s I/O system will most likely already be reading
from the file as efficiently as possibly and buffering it for your
character-at-a-time read code, so the main loop will be doing something
like this:
--------
get character from buffer
check whether buffer needs to be refilled (it usually won''t)
do stuff with character
--------
....and the "do stuff with character" part would have to be pretty
inefficient for the time taken to do this to be large enough to be
noticeable compared to the time spent refilling the buffer when that''s
necessary.
The general rule is to leave the micro-optimization to the compiler,
and leave any other optimization until after it''s shown to be needed.
That said, if you really want to make sure that the parts that you
can optimize yourself are as efficient as possible, it''s worth noting
that characters are just small integers and that array indices are
also integers. I''ll let you figure out where to go from there - if you
haven''t already seen something like what I''m thinking of, it''ll probably
end up being more valuable as an exercise in finding new ways to solve a
problem (and, hopefully, give you another tool to use for later problems)
than as an optimization exercise. (Be sure to show us[1] your code once
you''ve finished so we can point out all the problems in it. If you''re
just learning the language, there WILL be a bunch.)
dave

[1] Or a local guru, if you have one. Or, better yet, both.

--
Dave Vandervies dj******@csclub.uwaterloo.ca
I can''t pick up the phone and ask for "Farsley double one double three"
whenever I fancy a pizza. Well, I could, but I''d stay hungry.
--Peter Corlett in the scary devil monastery


Chris Hatton wrote:


I realize this is not a newsgroup for the total novice or homework
questions so this will be the last post until I become more familiar
with the language.

I''m looking to optimize a piece of code that reads characters from a
text file and counts the frequency of them. If someone could point
me in the right direction that would be great. I''m not looking for
any code just a couple of links to good websites as my time is very
limited at the moment.



Without any suitable includes etc. Needs limits and stdio at
least.

/* automatically zeroed */
static unsigned long counts[UCHAR_MAX+1];
....
int ch;
FILE *f;
....
/* open files etc */
....
while (EOF != (ch = getc(f))) counts[ch]++;
....
/* display the content of counts */
--
"If you want to post a followup via groups.google.com, don''t use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


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

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