lundi 29 février 2016

Reason for `std::bind` being more effective than a lambda

I have this piece of code:

float volume;

// version #1: (bind method)
auto times_vol = std::bind(std::multiplies<float>(),
                           volume, std::placeholders::_1);

// version #2: (lambda method)
auto times_vol = [volume] (float sample) -> float {
    return volume*sample;
};

The times_vol is a function that returns volume*x, where x is its argument. It will be used in a std::transform later.

I have measured some times for these two options. My program runs a lot of std::transform's and std::copy's, making a total of 51200 calls to times_vol. When times_vol is first version (the bind version), the times for the program are usually between 50 ms and 80 ms, with a mean of 76 ms. When I use the second version (lambda version), the times are usually between 40 ms and 60 ms, with a mean of 59 ms. (sometimes it takes 100+ ms).

Anyway, I'm pretty confident that the bind version is better. Are there any reasons for this?

Aucun commentaire:

Enregistrer un commentaire