Boost logo

Boost :

From: Alexander Grund (alexander.grund_at_[hidden])
Date: 2020-01-20 08:35:17


Am 18.01.20 um 07:57 schrieb Kemin Zhou via Boost:
> I am looking at the binomial.hpp file.
> I need to use the binomial object in a loop which requires to have high
> performance.
> I am thinking constructing the binomial object each time in the loop would
> be less efficient than if I construct a single object, the each time reset
> the p parameter and use the object.
>
> I am not sure why the setter method was not provided.
>
> 296 RealType success_fraction() const
> 297 { // Probability.
> 298 return m_p;
> 299 }
> 300 void set_success_fraction(RealType p) {
> 301 m_p=p;
> 302 }
> Line 296 is the getter method, I added the setter method at line 300.

As usual: Did you measure before making assumptions about performance?
Next: Did you check what the ctor does? What is your reasoning for your
statement?

This is not meant to sound harsh but rather spark usual scientific work
practices.

You'll see that the constructor does nothing but check invariants. Your
setter does not do so and hence is wrong (for some definition of wrong
as usual)
So the only overhead can be due to check of valid parameters. Depending
on how you pass in the arguments this can even be removed, so try it
first and measure where your performance suffers or use e.g. godbolt to
check the assembly to verify assumptions.

Then the better solution would be to provide a ctor that does not do
verification, probably the policy system can be used if it isn't
already. Again it needs to be argued why this would be required and how
much benefit it brings.

Regards, Alex




Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk