Nearest color algorithm efficiency Ok so my name is qqqiiiiiiiifdff and I made this code that simulates a perceptual color picker algorithm that selects the nearest color by iterating over the color array and selecting the minimum error, processing the calculations in a perceptual manner.
This is an example:
#include &lt;stdint.h&gt;
#include &lt;math.h&gt;

inline double square(double x){return x*x;}

inline double sRGBtolinear(double input){
    if(input &lt; 0) return -sRGBtolinear(-input);
    else if(input &gt; 0.0404482362771082) return pow((input+0.055)/1.055, 2.4);
    else return input/12.92;
}

inline double lineartosRGB(double input){
    if(input &lt; 0) return -lineartosRGB(-input);
    else if(input &gt; 0.00313066844250063) return pow(input, 0.4166666666666667)*1.055-0.055;
    else return input*12.92;
}

inline double lineartosRGBderivative(double input){
    if(input &lt; 0) return lineartosRGBderivative(-input);
    else if(input &gt; 0.00313066844250063) return pow(input, -0.5833333333333334)*0.4166666666666667*1.055;
    else return 12.92;
}

uint64_t selectnearest(uint32_t* palette, uint64_t palettelength, uint32_t color){
    uint64_t index = 0;
    double maxerror = 1.0/0.0;
    for(uint64_t i=0; i&lt;palettelength; i++){
        double color1[3];
        double color2[3];
        color1[0] = sRGBtolinear((color/65536%256)/255.0);
        color1[1] = sRGBtolinear((color/256%256)/255.0);
        color1[2] = sRGBtolinear((color%256)/255.0);
        color2[0] = sRGBtolinear((palette[i]/65536%256)/255.0);
        color2[1] = sRGBtolinear((palette[i]/256%256)/255.0);
        color2[2] = sRGBtolinear((palette[i]%256)/255.0);
        double initdistance = sqrt((square(212671.0*(color1[0]-color2[0]))+square(715160.0*(color1[1]-color2[1]))+square(72169.0*(color1[2]-color2[2])))/561891144402.0);
        double brightness1 = ((212671.0*color1[0])+(715160.0*color1[1])+(72169.0*color1[2]))/1000000.0;
        double brightness2 = ((212671.0*color2[0])+(715160.0*color2[1])+(72169.0*color2[2]))/1000000.0;
        double sRGBbrightness1 = lineartosRGB(brightness1);
        double sRGBbrightness2 = lineartosRGB(brightness2);
        double brightnessdifference = brightness2 - brightness1;
        double sRGBbrightnessdifference = sRGBbrightness2 - sRGBbrightness1;
        if(brightness1 == brightness2){
            brightnessdifference = 1;
            sRGBbrightnessdifference = lineartosRGBderivative(brightness1);
        }
        double result = initdistance*(sRGBbrightnessdifference/brightnessdifference);
        if(result &lt; maxerror){
            index = i;
            maxerror = result;
        }
    }
    return index;
}

int main(){
    const uint64_t amountofcolors = 216;
    uint32_t palette[amountofcolors];
    for(uint64_t i=0; i&lt;216; i++){
        palette[i] = i%6*0x000033 + i/6%6*0x003300 + i/36%6*0x330000;
    } // sample palette
    volatile uint32_t color;
    for(uint64_t i=0; i&lt;65536; i++){
        color = (rand()%4096)+(rand()%4096*4096);
        color = palette[selectnearest(palette, amountofcolors, color)];
    }
}


To a person who does not know any code, this does not appear inefficient until it is run. What happened here? The process returned zero, and the execution time is 31.141 seconds. Now, it might seem like this is quite fast for 65536 colors to quantize to the palette, however you could imagine how a dynamic script to play a 256×256 movie in a reduced palette will play in 0.03 fps. Could this be a processing error and I have to try again? No, because the next time it is not much better at 31.085 seconds, and again at 31.110 seconds. Now, you could argue that a different color picker algorithm would be faster, however that is not the point because they don't pick colors the same way, the point is to make this specific algorithm faster, but still pick the colors about the same way. What should be done in order to improve the efficiency of this method?
There are a lot of performance aspects that can be improved in your code. I will cover general suggestions assuming that this code is for Win32 target. I will use minGw compiler with c11 standard.
Compiler Optimizations:
Without optimization code runs about 12.8 seconds on my PC. With -O3 optimization flag it takes about 10.6 seconds (about 17% faster).
Data types and arithmetic operations:
double type and constants with high precision are used in the code. Do you really need this precision? Moreover infinity is used in maxerror variable: double maxerror = 1.0/0.0; Consider to use DBL_MAX instead of inf. If performance is important then general algorythm of data types selection is following:

Can I use integer arithmetics? Prefereably int/uint types with logical/shift operations.
If not, then can I use float?
If not, then chose double or more precise types.
If floating point data types are used then consider NOT using floating point exceptions, NaN, Inf and denormal values. All these things can be very slow.

For example, you extract color components in the following way:
color1[0] = sRGBtolinear((color/65536%256)/255.0);

Here integer division can be replaced by bitwise/shift operations which is much faster. Assembly code will be almost 3 times shorter.
color1[0] = sRGBtolinear(((color &gt;&gt; 16) &amp; 0xff)/255.0);

Inlining:
As it has been already mentioned inline keyword is confusing nowadays. Compilers have attributes or pragmas to always_inline/force_inline or noinline functions, because it is very important in some cases. E.g. if you want to insert assembly code with label in function wich compiler decided to inline. In this case compilation will fail. Only prohibiting inlining can help. In our case functions lineartosRGBOrig and lineartosRGBderivativeOrig can't be inlined because they are recursive. By the way, this recursion is not needed because input to these functions is always positive in current algorithm.
Algorithm changes and code rearrangment:

It is better to move variables declarations from the loop.
Values related to color in function selectnearest can be calculated only once istead of recalculation palettelength times.
RGBtolinear data can be precalculated as a table with 255 elements to significantly improve performance.
initdistance and brightness values are used only in comparison which allows us to remove sqrt operation and some divisions because we don't need exact values. Only relation is important.

Some brief optimizations:
I have made some optimizations that improved performance by 86% to original code compiled with -O3 flag and by 89% to code compiled without optimization. The code is not perfect but it is much faster.
#pragma GCC push_options
#pragma GCC optimize (&quot;O3&quot;)

double gRgbToLinearTable[255];

void InitRgbToLinearTable(){
    for (size_t i = 0u; i &lt; 255u; ++i)
    {
        double val = i / 255.0;
        gRgbToLinearTable[i] = (val &gt; 0.0404482362771082 ? pow((val+0.055)/1.055, 2.4) : val/12.92);
    }
}

[[gnu::always_inline]] inline double square(double x) { return x*x; }

[[gnu::always_inline, gnu::flatten]] inline void sRGBtolinear(double* outComponents, uint32_t color){
    outComponents[0] = gRgbToLinearTable[(color &gt;&gt; 16) &amp; 0xff];
    outComponents[1] = gRgbToLinearTable[(color &gt;&gt; 8) &amp; 0xff];
    outComponents[2] = gRgbToLinearTable[color &amp; 0xff];
}

[[gnu::always_inline, gnu::flatten]] inline double lineartosRGB(double input){
    if (input &gt; 0.00313066844250063)
        return (pow(input, 0.4166666666666667)*1.055-0.055);
    else
        return input*12.92;
}

[[gnu::always_inline, gnu::flatten]] inline double lineartosRGBderivative(double input){
    if(std::abs(input) &gt; 0.00313066844250063) return pow(input, -0.5833333333333334)*0.4166666666666667*1.055;
    else return 12.92;
}

size_t selectnearest(const uint32_t* palette, size_t palettelength, uint32_t color)
{
    size_t index = 0;
    double maxerror = DBL_MAX;
    double colors[2][3];
    double initdistance, brightness[2], rgbBrightness[2], brightnessdifference, sRGBbrightnessdifference;
    sRGBtolinear(colors[0], color);
    brightness[0] = (0.212671*colors[0][0])+(0.715160*colors[0][1])+(0.072169*colors[0][2]);
    rgbBrightness[0] = lineartosRGB(brightness[0]);

    for (size_t i = 0; i &lt; palettelength; i++)
    {
        if (palette[i] != color)
        {
            sRGBtolinear(colors[1], palette[i]);

            initdistance = (square(0.212671*(colors[0][0]-colors[1][0]))+square(0.212671*(colors[0][1]-colors[1][1]))+square(0.072169*(colors[0][2]-colors[1][2])));
            brightness[1] = (0.212671*colors[1][0])+(0.715160*colors[1][1])+(0.072169*colors[1][2]);
            rgbBrightness[1] = lineartosRGB(brightness[1]);

            if(brightness[0] != brightness[1])
            {
                brightnessdifference = brightness[1] - brightness[0];
                sRGBbrightnessdifference = rgbBrightness[1] - rgbBrightness[0];
            }
            else
            {
                brightnessdifference = 1.0 ;
                sRGBbrightnessdifference = lineartosRGBderivative(brightness[0]);
            }

            double result = initdistance*(sRGBbrightnessdifference/brightnessdifference);
            if(result &lt; maxerror){
                index = i;
                maxerror = result;
            }
        }
        else
        {
            return i;
        }
    }

    return index;
}

uint32_t* MakeSamplePalette(size_t amountOfColors)
{
    uint32_t* pResult = new uint32_t[amountOfColors];
    for(size_t i = 0; i &lt; amountOfColors; i++)
    {
        pResult[i] = i%6*0x000033 + i/6%6*0x003300 + i/36%6*0x330000;
    }

    return pResult;
}

int main()
{
    const size_t amountofcolors = 216u;
    uint32_t* palette = MakeSamplePalette(amountofcolors);
    volatile uint32_t color;

    InitRgbToLinearTable();
    for(size_t i = 0; i &lt; 65536u; i++){
        color = (static_cast&lt;unsigned int&gt;(rand()) &amp; 0xfff)+(static_cast&lt;unsigned int&gt;((rand()) &amp; 0xfff) &lt;&lt; 12);
        color = palette[selectnearest(palette, amountofcolors, color)];
    }

    delete[] palette;
    return color;
}
#pragma GCC pop_options