>>14Not portable and is only x1.001-x1.008 faster(file caching already mmaps everything).
But if you need that extra performance, here it is.
//uwordcount.c utility by FrozenVoid
//UnixWordCount:Requires mmap support,Unix system assumed.
//Count number of words/lines/bytes in file(text-only).
#include <ctype.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/io.h>
#include <sys/mman.h>
int main(int argc,char**argv){
int in; int64_t words=0,lines=0,bytes=0;
struct stat filestat;
if(argc==2){
in=open (argv[1], O_RDONLY);
}else{in=fileno(stdin);}
if(in==-1){printf("Wrong filename:%s",argc==2?&argv[1][0]:
"Only one optional argument expected:wordcount [Filename]");
;perror("");return 1;}
fstat(in,&filestat);
char* buffer=mmap(NULL, filestat.st_size, PROT_READ, MAP_PRIVATE, in, 0);
int lastspace=1,curspace=0;
if(argc==2){
bytes+=filestat.st_size;
for(size_t i=0;i<filestat.st_size;i++){
char c=buffer[i];
curspace=isspace(c)!=0;
lines+=c=='\n';
words+=lastspace^curspace;
lastspace=curspace;
}
}else{//handle pipes/stdin
FILE* infile=stdin;
while(!feof(infile)){bytes++;
char c=(char)fgetc(infile);
curspace=!!isspace(c);
lines+=c=='\n';
words+=lastspace^curspace;
lastspace=curspace;
;}}
printf("%"PRIu64" %"PRIu64 " %"PRIu64"\n",lines,(words/2),bytes-(argc!=2));return 0;}