From owner-freebsd-hackers Tue Oct 22 11:53:26 2002 Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 2363237B401 for ; Tue, 22 Oct 2002 11:53:24 -0700 (PDT) Received: from math.missouri.edu (math.missouri.edu [128.206.49.180]) by mx1.FreeBSD.org (Postfix) with ESMTP id 0046143E42 for ; Tue, 22 Oct 2002 11:53:23 -0700 (PDT) (envelope-from stephen@math.missouri.edu) Received: from math.missouri.edu (cauchy.math.missouri.edu [128.206.49.166]) by math.missouri.edu (8.11.6/8.11.6) with ESMTP id g9MIqY207598; Tue, 22 Oct 2002 13:52:35 -0500 (CDT) (envelope-from stephen@math.missouri.edu) Message-ID: <3DB59E72.2080305@math.missouri.edu> Date: Tue, 22 Oct 2002 13:52:34 -0500 From: Stephen Montgomery-Smith Organization: University of Missouri User-Agent: Mozilla/5.0 (X11; U; Linux i386; en-US; rv:1.2b) Gecko/20021016 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Brooks Davis Cc: Terry Lambert , Danny Braniss , Poul-Henning Kamp , freebsd-hackers@FreeBSD.ORG Subject: Re: malloc References: <3DB50A5A.F87EDA78@mindspring.com> <20021022110159.A1513@Odin.AC.HMC.Edu> In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG Brooks Davis wrote: > > The user may also see a performance gain on Linux if they use a less > stupid allocation scheme. I ran into some code once that read strings > one character at a time via getc() and did a realloc for each read. > Needless to say, performance was truly awful since a typical run > required parsing over 600MB of text. I saw a better then 50% speedup on > Alpha Linux when I fixed that mess. > > -- Brooks > As an amateur programmer, I wrote a program something like this. But it allocates memory 1000 bytes at a time in order to save some of this overhead. I would be interested in getting feedback about this little function I wrote as to whether it could be greatly improved. (It works great for me, but probably I haven't really pushed it very hard.) #include #include /* Get a string from the stream f, allocating space to s as required. */ char *fgetsalloc(char **s, FILE *f) { int c; int len; int allocated; allocated = 1000; if ((*s = realloc(*s,1000))==NULL) { fprintf(stderr,"Allocation error\n"); exit(1); } len = 0; while (1) { c=getc(f); if (c==EOF) break; if (len>=allocated-1) { allocated += 1000; if ((*s = realloc(*s,allocated))==NULL) { fprintf(stderr,"Allocation error\n"); exit(1); } } (*s)[len] = c; len++; if (c=='\n') break; } if (len!=0) { (*s)[len] = '\0'; if ((*s = realloc(*s,len+1))==NULL) { fprintf(stderr,"Allocation error\n"); exit(1); } } else { if (*s!=NULL) free(*s); *s = NULL; } return *s; } -- Stephen Montgomery-Smith stephen@math.missouri.edu http://www.math.missouri.edu/~stephen To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message