Re: count2.asm
- From: Frank Kotler <fbkotler@xxxxxxxxxxx>
- Date: Tue, 26 Jul 2005 11:44:12 -0400
¬a\/b wrote:
On Mon, 25 Jul 2005 13:58:46 -0400, Frank Kotler
this is my not tested one in C
#include <stdio.h> #include <time.h> #include <stdlib.h> #include <string.h> #include <ctype.h>
static char list[256]={0};
void init(void)
{char *p;
char *a1="aeiou", *a2="BCDFGHJKLMNPQRSTVWXZ",
*a3="bcdfghjklmnpqrstvwxz", *a4="0123456789", *a5="!\'#$%&', \"()*+,-./:;<=>?@[\\]^_`{|}~", *a6="AEIOU",
*a7="\t", *a8=" ", *a9="\n";
/***********************************/ for(p=a1; *p; ++p) list[*p]=1; for(p=a2; *p; ++p) list[*p]=2; for(p=a3; *p; ++p) list[*p]=3; for(p=a4; *p; ++p) list[*p]=4; for(p=a5; *p; ++p) list[*p]=5; for(p=a6; *p; ++p) list[*p]=6; list[*a7]=7; list[*a8]=8; list[*a9]=9; }
long unsigned contatori[11]={0}; long unsigned words=0;
int main(int x, char** a)
{char buff[1090], *to, cc,*p, o;
FILE *f_in;
int c, j;
unsigned long tmp, tmp1;
/**********************************/
if(x!=0 && a!=0 && a[0]!=0) to=a[1]; else to="";
if( !(x==2) || *to==0) {printf("USO:\n>nome_prog nome_file_in "); return 0;}
f_in=fopen(to, "r"); if(f_in==0) {printf("impossibile aprire il file %s\n", to);
return 0;
} init(); while( (c=fread(buff, 1, 1024, f_in)) > 0) {p=buff;
for(j=0, cc=' '; j<c; ++j)
{o=*p; /* this is not ok when "paro=\nla" */
if(isspace(o) && isalnum(cc)) ++words;
++contatori[list[o]];
cc=o; ++p;
} }
if( fclose(f_in)== EOF ) {printf("Errori nel leggere il file\n");
return 0;
}
printf("\nUpper case vowels: %lu\n",contatori[6]); printf("Lower case vowels: %lu\n", contatori[1]); printf("Total vowels: %lu\n", tmp=contatori[1]+contatori[6]); printf("Upper case consonants: %lu\n", contatori[2]); printf("Lower case consonants: %lu\n", contatori[3]); printf("Total consonants: %lu\n", tmp1=contatori[2]+contatori[3]);
printf("Total alpha characters: %lu\n", tmp+tmp1);
printf("Numerals: %lu\n", contatori[4]);
printf("Total punctuation: %lu\n", contatori[5]);
printf("Total words: %lu\n", words);
printf("Tabs: %lu\n", contatori[7]);
printf("Spaces: %lu\n", contatori[8]);
printf("Carriage Returns: %lu\n", contatori[9]);
/* what is linefeed ? */
for(j=0, tmp=0; j<11; ++j) tmp+= contatori[j];
printf("Total number of chars: %lu\n", tmp);
return 0;
}
Wow! As predicted, the executable is several times larger than the asm versions (not a big deal). In speed, it seems to run faster than the jump table version, but not as fast as the latest "count 'em afterwards" version. I'm impressed!!! I'll have to take a look at what gcc is producing for code - might be some good ideas I can steal :)
[The compiler can never win. If the compiler produces faster code, we use that. If the human produces faster code, we use that. The best the compiler can do is break even.]
One minor nit to pick... You seem to have solved the "Y question" by not counting 'y's as either vowels or consonants - you've removed it from the "vowel strings" (upper and lower), but didn't add it to the "consonant strings". Easy to fix. I should fix that in my own code!
Another thing I should probably fix... I don't have LF's CR's and tabs in my "punctuation string" - I *did* count them as punctuation in the first version. I don't really know which is "right" here...
Nice job! Thanks!
Best, Frank .
- Follow-Ups:
- Re: count2.asm
- From: Beth
- Re: count2.asm
- From: ¬a\\/b
- Re: count2.asm
- References:
- count2.asm
- From: Frank Kotler
- Re: count2.asm
- From: ¬a\\/b
- count2.asm
- Prev by Date: Re: HAY Betov, why won't you properly support GPL software.
- Next by Date: Re: HAY Betov, why won't you properly support GPL software.
- Previous by thread: Re: count2.asm
- Next by thread: Re: count2.asm
- Index(es):
Relevant Pages
|