To get the most improvement you need to load as much of the file into memory as possible and then do it. Most of the loop's time is from reading the harddrive.
Example:
Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
void init (int *, int);
int main (void)
{
int size_read, fid, count;
char buffer[1048576];
int freq[256];
init(freq, sizeof freq/sizeof freq[0]);
fid=open("test.txt", O_RDONLY);
if ( fid == -1 )
{
puts("Could not open file");
return 1;
}
while ( (size_read = read(fid, buffer, sizeof buffer)) != 0 )
{
for(count=0; count<size_read; count++)
freq[(int)buffer[count]]++;
}
for (count=0; count<sizeof freq/sizeof freq[0]; count++)
printf("ASCII %3d: %3d\n", count, freq[count]);
return 0;
}
void init (int *x, int y)
{
int count;
for (count=0; count < y; count++)
x[count] = 0;
}