Path: csiph.com!xmission!news.alt.net!not-for-mail From: owl Newsgroups: comp.os.linux.advocacy Subject: Re: A maybe interesting code challenge Date: Tue, 26 Apr 2016 19:04:05 +0000 (UTC) Organization: O.W.L. Lines: 392 Message-ID: References: NNTP-Posting-Host: boom.rooftop.invalid User-Agent: tin/2.2.1-20140504 ("Tober an Righ") (UNIX) (Linux/3.16.0-4-amd64 (x86_64)) Xref: csiph.com comp.os.linux.advocacy:352013 DFS wrote: > On 4/23/2016 1:59 AM, owl wrote: >> DFS wrote: >>> On 4/23/2016 12:09 AM, Norman Peelman wrote: >>> >>>> So, (curiosity) what is wrong with it? >>> >>> >>> A bunch of stuff I didn't want to track down and fix. >>> >>> VBScript has some significant differences from VB/A. >>> >>> If you develop and test under VBScript you'll see. >>> >>> >>> >>>> Anyway, you do understand >>>> that you are doing unnecessary work by having: >>>> >>>> criteria >= 1 -- every time criteria is 1,2 or 3 these lines run >>>> criteria >= 2 -- every time criteria is 2 or 3 these lines run >>> >>> >>> Yes, I recognized that immediately after I posted it. The fix shaved >>> 0.014 seconds off run time. >>> >> >> Your gonna hate me. I figured the 2.7 sec was low for a C app. >> I went over the code and removed some stupid shit and now baby's >> on fire. The first one, "input_e", is the 6000 line input file >> that gave me the 2.7 sec. Now it's 0.048 sec. :) And a 600k >> line file gets burned up in 1.376 sec, generating 3.6 million >> lines of output. > > Nice. Code or it didn't happen :) > Below. > You're not actually writing 3.6M lines to a file, right? > Yes. I have since made some modifications to the code, so that it handles an an arbirary number of input lines and allocates memory based on file size. Before it was hardcoded in arrays of fixed size. (Still uses fixed max ending line length estimate of 128 bytes at this point, as well as fixed field count of 6 based on initial count of ';', so it's not completely robust yet). This resulted in a speed decrease to about 2-3 sec IIRC for writing a 3.6 million line file. Right now I have 8 instances of that md5/ip cracking thing running, and times are way, off so I can't show a realistic measure. I didn't use the regex.h stuff -- just walked the file and did byte comparisons. I figured added function calls with regex would be slower, but I might try it that way later and see what happens. ----------------------- begin code ------------------------ #include #include #include #define LINELENGTH 128 struct vars{ char field1[LINELENGTH]; char value1[LINELENGTH]; char field2[LINELENGTH]; char value2[LINELENGTH]; char field3[LINELENGTH]; char value3[LINELENGTH]; }; int main(int argc, char *argv[]) { int c=0; int i=0; int j=0; int k=0; long filesize=0; FILE *fp=NULL; int newlinecount=0; int semicount=0; struct vars *varsarr; const char s[2]=";"; char *token; char *sourcebuffer; char *targetbuffer; char *tempbuffer; if ( argc != 2 ) { fprintf(stderr,"need a filename\n"); exit(1); } char *inputfilename = argv[1]; if( (fp=fopen(inputfilename,"r")) == NULL ) { fprintf(stderr,"no file\n"); exit(1); } fseek(fp,0,SEEK_END); filesize=ftell(fp); fseek(fp,0,SEEK_SET); sourcebuffer = malloc(2 * filesize +1 ); if (sourcebuffer==NULL) { fprintf(stderr,"sourcebuffer malloc()\n"); exit(1); } targetbuffer = malloc(2 * filesize +1 ); if (targetbuffer==NULL) { fprintf(stderr,"targetbuffer malloc()\n"); exit(1); } while( (c=fgetc(fp))!= EOF ) { if ( c == '\n' ) { targetbuffer[i]=c; newlinecount++; } if ( c == ' ' ) { targetbuffer[i++]=c; c=fgetc(fp); if ( c == 'l' ) { c=fgetc(fp); if ( c == 'i' ) { c=fgetc(fp); if( c == 'k' ) { c=fgetc(fp); if( c == 'e' ) { c=fgetc(fp); if ( c == ' ' ) { targetbuffer[i++]='='; } else /* c != ' ' */ { targetbuffer[i++]=c; } } else /* c != 'e' */ { targetbuffer[i++]=c; } } else /* c != 'k' */ { targetbuffer[i++]=c; } } else /* c != 'i' */ { targetbuffer[i++]=c; } } else /* c != 'l' */ { targetbuffer[i++]=c; } } else /* c != ' ' */ { targetbuffer[i++]=c; } } /* while loop */ for (i=0;targetbuffer[i]!='\0';i++) { c=targetbuffer[i]; if(c=='=') { targetbuffer[i]=';'; } } tempbuffer=sourcebuffer; /* c=a */ sourcebuffer=targetbuffer; /* a=b */ targetbuffer=tempbuffer; /* b=c */ for (i=0,j=0;sourcebuffer[i]!='\0';i++,j++) { c=sourcebuffer[i]; targetbuffer[j]=c; if(c==';') { semicount++; } if (c=='\n') { if (semicount < 6) { for(k=semicount;k<6;k++) { targetbuffer[j++]=';'; } } semicount=0; targetbuffer[j]=c; } } tempbuffer=sourcebuffer; /* c=a */ sourcebuffer=targetbuffer; /* a=b */ targetbuffer=tempbuffer; /* b=c */ for (i=0,j=0;sourcebuffer[i]!='\0';i++,j++) { if(sourcebuffer[i]==' ') { if(sourcebuffer[i+1]==';') { targetbuffer[j]=sourcebuffer[i+1]; i++; } else { targetbuffer[j]=sourcebuffer[i]; } } else { targetbuffer[j]=sourcebuffer[i]; } } tempbuffer=sourcebuffer; /* c=a */ sourcebuffer=targetbuffer; /* a=b */ targetbuffer=tempbuffer; /* b=c */ for (i=0,j=0;sourcebuffer[i]!='\0';i++,j++) { if(sourcebuffer[i]==';') { targetbuffer[j]=sourcebuffer[i]; if(sourcebuffer[i+1]==' ') { i++; } } else { targetbuffer[j]=sourcebuffer[i]; } } targetbuffer[j]='\0'; tempbuffer=sourcebuffer; /* c=a */ sourcebuffer=targetbuffer; /* a=b */ targetbuffer=tempbuffer; /* b=c */ for (i=0,j=0;sourcebuffer[i]!='\0';i++,j++) { if(sourcebuffer[i]=='\n') { i++; targetbuffer[j]=sourcebuffer[i]; } else { targetbuffer[j]=sourcebuffer[i]; } } targetbuffer[j]='\0'; tempbuffer=sourcebuffer; /* c=a */ sourcebuffer=targetbuffer; /* a=b */ targetbuffer=tempbuffer; /* b=c */ semicount=0; targetbuffer[0]='\''; for (i=0,j=1;sourcebuffer[i]!='\0';i++,j++) { if(sourcebuffer[i]==';') { semicount++; targetbuffer[j]='\''; targetbuffer[++j]=sourcebuffer[i]; targetbuffer[++j]='\''; } else { targetbuffer[j]=sourcebuffer[i]; } } targetbuffer[--j]='\0'; tempbuffer=sourcebuffer; /* c=a */ sourcebuffer=targetbuffer; /* a=b */ targetbuffer=tempbuffer; /* b=c */ varsarr=malloc( newlinecount * sizeof(*varsarr)); if (varsarr==NULL) { fprintf(stderr,"varsarr malloc()\n"); exit(1); } token=strtok(sourcebuffer,s); for (i=0;i