md optimization (was Re: Canon vs CHDK histograms) - General Help and Assistance on using CHDK stable releases - CHDK Forum  

md optimization (was Re: Canon vs CHDK histograms)

  • 2 Replies
  • 970 Views
*

Offline Caefix

  • *****
  • 945
  • Sorry, busy deleting test shots...
md optimization (was Re: Canon vs CHDK histograms)
« on: 30 / June / 2021, 15:31:03 »
Advertisements
 :-* Just a question about micro timing: In my md_detect I wrote:
Code: [Select]
short clip(int v);
inline short clip(int v)
{
    if (v<0) return 0;
    if (v>255) return 255;
    return v;
}

while histogram.c has
// Histogram calculation functions

// Clip value to byte range (for YUV -> RGB conversion)
static int clip(int v)
{
    if (v<0) v=0;
    else if (v>255) v=255;
    return v;
}

So,  is int faster calculated or short, static or inline?
« Last Edit: 30 / June / 2021, 15:38:34 by Caefix »
All lifetime is a loan from eternity.

*

Offline reyalp

  • ******
  • 14080
Re: md optimization (was Re: Canon vs CHDK histograms)
« Reply #1 on: 30 / June / 2021, 16:33:46 »
:-* Just a question about micro timing: In my md_detect I wrote:

So,  is int faster calculated or short, static or inline?
@Caefix
I split this from the histogram thread, because it's really off-topic for that one.

1) It probably doesn't matter. I should stop here.

2) If you want to know for sure, you should make a test case to measure it accurately. Some code I used for histogram is here, but understanding is required to use it.
In general, "optimizing" without measurement is not a very useful activity. I should really stop here.

3) In general, the camera ARM processors work on 32 bit ints, so shorts will be tend be slower and result in additional instructions. Making individual function return values, parameters and variables shorts will usually have a negative impact. However, this depends on the specific case, and when you're storing a lot of values, using shorts can save memory, which is also important in CHDK.  To really understand the impact, disassembly can be informative.

4) Inline could be good, neutral or bad for performance, but will increase code size if the compiler actually inlines it.

5) Things that are only used in a given file should be static, but this should generally not affect performance of functions and is unrelated to inline.
Don't forget what the H stands for.

*

Offline Caefix

  • *****
  • 945
  • Sorry, busy deleting test shots...
Re: md optimization (was Re: Canon vs CHDK histograms)
« Reply #2 on: 01 / July / 2021, 15:55:14 »
Compiled for M3  :)
Orig. md_ 3004, histo 3052
vs
Code: [Select]
int clip(int v);
inline int clip(int v)
{
    if (v<1) return 0;
    if (v>255) return 255;
    return v;
}
md_detect 3000, histo 3100
All lifetime is a loan from eternity.