Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse Newsgroups: comp.lang.c Subject: Re: Allocate variables within code blocks. Date: Sun, 08 Apr 2018 12:29:44 +0100 Organization: A noiseless patient Spider Lines: 39 Message-ID: <87bmeu7x7b.fsf@bsb.me.uk> References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: reader02.eternal-september.org; posting-host="e8f35059632993b7d2dff1dfb5ce8a64"; logging-data="17806"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18dg/11CtWA3jd+iS9ES5o0QgX97T6Go80=" Cancel-Lock: sha1:oa8pFkyZVAyjXNzHruELo+HZFIc= sha1:RrhlOH4otI65zD1vXPpoIURYjLE= X-BSB-Auth: 1.c33282fdaa1b02d81168.20180408122944BST.87bmeu7x7b.fsf@bsb.me.uk Xref: csiph.com comp.lang.c:128927 "itsme.susila" writes: > Allocate variables within code blocks > > Within a function, I will have many for loops: I'd reconsider that design if I were you. I suppose it depends on what the program is, but short single-purpose function generally make for better code. > [code] > void myfn(){ > int, x, y, z; > for (i = 0,...){ > u64 u, v, w; > > u = 3; > x = u * 6; > ...... > > > } > > > return; > } > [/code] > > Which is the better practice (giving faster codes): > Should I allocate u,v,w at the start of myfn(); I know beforehand they > would be used many times over. Modern compilers will generate good code in both cases. It's almost always better to write for clarity rather than generated code. Once you have a clear program, measure the performance to see what parts are taking up the time. -- Ben.