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.