c++ - A more efficient way than function reference? -


i have class uses reference function:

double u( const double& x, const double& y ) {   return x * y; }  class equation {   equation( double (&in_u)(const double&, const double&) ); //... protected:   double (&u)(const double&, const double&); } 

this function called 108 times during typical run.

the class goes library , function u defined user of library. cannot have function definition inside class.

i have read this:

(std::function) ... has disadvantage of introducing (very small) overhead when being called (so in performance-critical situation might problem in should not)

are there more efficient ways of passing function u class equation? , count "a performance-critical situation"?

edit

there seems bit of confusion. make clear, function u is known @ executables' compile time, not @ library's. getting function @ run-time feature consider in later versions of library, not now.

a function pointer (or reference, identical @ implementation level) work fine.

modern cpus very good @ branch prediction, after first couple calls cpu recognize "indirect" call goes same place, , use speculative execution keep pipeline full.

however, there still no optimization across function boundary. no inlining, no auto-vectorization.

if function being called 108 times, large number of in tight loop varying parameters. in case, suggest changing function prototype accept array of parameter values , output array of results. have loop inside function, compiler can perform optimizations such unrolling , auto-vectorization.

(this specific case of general principle deal interop cost reducing number of calls across boundary)

if isn't possible, pass parameters value. others have said, efficient const reference floating-point variables. lot more efficient, since calling conventions use floating-point registers (typically sse registers, on modern intel architectures, before used x87 stack) ready perform computations immediately. spilling values to/from ram in order pass reference quite costly, when function gets inlined pass-by-reference gets optimized away, won't happening here. still not passing entire array though.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -