lundi 4 juillet 2016

What is the fastest way to update a variable on a condition?


I have a pointer, ptr, and a condition, cond. I need the fastest possible way to reset ptr if cond is true, or keep ptr unchanged if cond is false. The current implementation is, trivially:

void reset_if_true(void*& ptr, bool cond)
{
    if (cond)
        ptr = nullptr;
}

I'm aware that the above code performance is good, and I cannot expect a major performance boost optimizing it. However, this code is called several millions of times per second and every little nanosecond saved is relevant.

I was thinking about something that get rid of the branch, e.g.:

void* p[] = { ptr, nullptr };
ptr = p[cond];

but I'm not sure it's the best way to proceed.


Aucun commentaire:

Enregistrer un commentaire