Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #128986

Re: Allocate variables within code blocks.

From "itsme.susila" <itsme.susila@gmx.com>
Newsgroups comp.lang.c
Subject Re: Allocate variables within code blocks.
Date 2018-04-09 11:54 +0800
Organization Aioe.org NNTP Server
Message-ID <paeo53$a2k$1@gioia.aioe.org> (permalink)
References <pacshc$18uv$1@gioia.aioe.org> <kfnlgdxe23l.fsf@x-alumni2.alumni.caltech.edu>

Show all headers | View raw


On 04/09/2018 06:59 AM, Tim Rentsch wrote:
> "itsme.susila" <itsme.susila@gmx.com> writes:
> 
>> Allocate variables within code blocks
>>
>> Within a function, I will have many for loops:
>> [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.
> 
> Your question doesn't have any simple answers.  In fact, these
> days most questions about improving performance don't have any
> simple answers.  What happens now inside compilers and inside
> computers is so complicated it is nearly impossible to analyze it
> or give any hard and fast rules.  However, unless you are an
> expert, any effect you get from moving variable declarations
> around is likely to be much smaller than effects from things like
> choice of data structure representation, algorithmic choices,
> speeding up common cases possibly at the expense of rare cases,
> etc.  Start by asking where the existing code is spending its
> time.  Which ever lines of code are the "hottest" is a good place
> to start looking for alternatives.  Code up two or three different
> ideas for cooling down the hot spot, then run those and measure
> what the effects of the different changes are.  Pick what looks
> like the best scheme and go around the cycle again, gradually
> reaching a more uniform code temperature.  Going through this
> process, without ever getting to micro-optimization, is very
> likely to give most of the performance gain that can be squeezed
> out of the program.  At some point you will reach a point of
> diminishing returns, where the additional performance gain is
> not worth the amount of effort involved;  you have to decide
> for yourself at what point that is.
> 
> (Disclaimer:  I am not a performance expert.  I have worked with
> some people who are scary good at performance tuning, and some
> of that has rubbed off, but I still wouldn't call myself an
> expert.)
> 
I think the advice given earlier "test! don't imagine" is correct. But I 
still like to have some good practice from the experts who know how our 
computer cpu actually operate.

In fact, chess programmers are a lot who talk about speed. I did go to 
talkchess.com (now no more) where there were much talk about coding 
practice. A simple example that novice may not know: use integer(4 
bytes) data rather than short(2 bytes)...chess programming is almost all 
with integer operations and there are overheads for modern cpu to 
operate on char  and short types.

In fact the experts would also do profiling (there is compiler auto 
profiling optimizations too) and then to optimize at the particular 
spots; but we already know it i recursive search which is the critical 
function. But I think it is correct to start off with what you say, good 
choice of data structures, etc...

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Allocate variables within code blocks. "itsme.susila" <itsme.susila@gmx.com> - 2018-04-08 18:56 +0800
  Re: Allocate variables within code blocks. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-04-08 12:29 +0100
    Re: Allocate variables within code blocks. Steven Petruzzellis <frelwizzen@gmail.com> - 2018-04-08 05:06 -0700
    Re: Allocate variables within code blocks. Spiros Bousbouras <spibou@gmail.com> - 2018-04-08 13:00 +0000
      Re: Allocate variables within code blocks. Steven Petruzzellis <frelwizzen@gmail.com> - 2018-04-08 08:14 -0700
    Re: Allocate variables within code blocks. GOTHIER Nathan <nathan.gothier@gmail.com> - 2018-04-08 16:37 +0200
    Re: Allocate variables within code blocks. Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2018-04-08 08:53 -0600
      Re: Allocate variables within code blocks. bartc <bc@freeuk.com> - 2018-04-08 16:32 +0100
        Re: Allocate variables within code blocks. Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2018-04-08 15:19 -0600
          Re: Allocate variables within code blocks. Tim Rentsch <txr@alumni.caltech.edu> - 2018-04-09 07:59 -0700
            Re: Allocate variables within code blocks. Tim Rentsch <txr@alumni.caltech.edu> - 2018-04-11 06:29 -0700
              Re: Allocate variables within code blocks. bartc <bc@freeuk.com> - 2018-04-11 15:58 +0100
                Re: Allocate variables within code blocks. Tim Rentsch <txr@alumni.caltech.edu> - 2018-04-11 11:28 -0700
                Re: Allocate variables within code blocks. Steven Petruzzellis <frelwizzen@gmail.com> - 2018-04-13 00:42 -0700
        Re: Allocate variables within code blocks. Jorgen Grahn <grahn+nntp@snipabacken.se> - 2018-04-11 10:25 +0000
    Re: Allocate variables within code blocks. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-04-08 19:27 +0100
  Re: Allocate variables within code blocks. James Kuyper <jameskuyper@verizon.net> - 2018-04-08 07:54 -0400
    Re: Allocate variables within code blocks. Spiros Bousbouras <spibou@gmail.com> - 2018-04-08 12:50 +0000
  Re: Allocate variables within code blocks. bartc <bc@freeuk.com> - 2018-04-08 13:42 +0100
  Re: Allocate variables within code blocks. "itsme.susila" <itsme.susila@gmx.com> - 2018-04-08 22:19 +0800
    Re: Allocate variables within code blocks. "itsme.susila" <itsme.susila@gmx.com> - 2018-04-08 22:27 +0800
      Re: Allocate variables within code blocks. Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2018-04-08 10:42 -0400
    Re: Allocate variables within code blocks. bartc <bc@freeuk.com> - 2018-04-08 15:37 +0100
    Re: Allocate variables within code blocks. David Brown <david.brown@hesbynett.no> - 2018-04-08 16:45 +0200
      Re: Allocate variables within code blocks. "itsme.susila" <itsme.susila@gmx.com> - 2018-04-08 22:55 +0800
        Re: Allocate variables within code blocks. GOTHIER Nathan <nathan.gothier@gmail.com> - 2018-04-08 17:52 +0200
    Re: Allocate variables within code blocks. Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2018-04-08 09:00 -0600
  Re: Allocate variables within code blocks. Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2018-04-08 08:51 -0600
  Re: Allocate variables within code blocks. Tim Rentsch <txr@alumni.caltech.edu> - 2018-04-08 15:59 -0700
    Re: Allocate variables within code blocks. "itsme.susila" <itsme.susila@gmx.com> - 2018-04-09 11:54 +0800
      Re: Allocate variables within code blocks. David Brown <david.brown@hesbynett.no> - 2018-04-09 09:31 +0200
      Re: Allocate variables within code blocks. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-04-09 12:17 +0100
        Re: Allocate variables within code blocks. "itsme.susila" <itsme.susila@gmx.com> - 2018-04-10 00:27 +0800
          Re: Allocate variables within code blocks. Steven Petruzzellis <frelwizzen@gmail.com> - 2018-04-09 09:33 -0700
          Re: Allocate variables within code blocks. Spiros Bousbouras <spibou@gmail.com> - 2018-04-09 16:52 +0000
            Re: Allocate variables within code blocks. "itsme.susila" <itsme.susila@gmx.com> - 2018-04-10 01:17 +0800
          Re: Allocate variables within code blocks. scott@slp53.sl.home (Scott Lurndal) - 2018-04-09 18:03 +0000
            Re: Allocate variables within code blocks. supercat@casperkitty.com - 2018-04-09 13:39 -0700
              Re: Allocate variables within code blocks. scott@slp53.sl.home (Scott Lurndal) - 2018-04-09 20:53 +0000
      Re: Allocate variables within code blocks. bartc <bc@freeuk.com> - 2018-04-09 12:23 +0100
        Re: Allocate variables within code blocks. "itsme.susila" <itsme.susila@gmx.com> - 2018-04-10 00:39 +0800
      Re: Allocate variables within code blocks. Tim Rentsch <txr@alumni.caltech.edu> - 2018-04-09 08:09 -0700
    Re: Allocate variables within code blocks. Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2018-04-09 17:07 -0600
      Re: Allocate variables within code blocks. supercat@casperkitty.com - 2018-04-09 16:28 -0700
      Re: Allocate variables within code blocks. Steven Petruzzellis <frelwizzen@gmail.com> - 2018-04-10 00:46 -0700
      Re: Allocate variables within code blocks. Tim Rentsch <txr@alumni.caltech.edu> - 2018-04-11 07:10 -0700

csiph-web