Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Looks like <code>struct</code> is the faster way to do it:</p> <pre><code>11:01:56 ~/try &gt; g++ -std=c++11 main.cpp ; ./a.out in 2265 ms, functor as a struct result = 1.5708e+16 in 31855 ms, functor through bind result = 1.5708e+16 11:02:33 ~/try &gt; clang++ -std=c++11 main.cpp ; ./a.out in 3484 ms, functor as a struct result = 1.5708e+16 in 21081 ms, functor through bind result = 1.5708e+16 </code></pre> <p>the code:</p> <pre><code>#include &lt;iostream&gt; #include &lt;functional&gt; #include &lt;chrono&gt; using namespace std; using namespace std::placeholders; using namespace std::chrono; struct fs { double s; fs(double state) : s(state) {} double operator()(double x) { return x*s; } }; double fb(double x, double state) { return x*state; } int main(int argc, char const *argv[]) { double state=3.1415926; const auto stp1 = system_clock::now(); fs fstruct(state); double sresult; for(double x=0.0; x&lt; 1.0e8; ++x) { sresult += fstruct(x); } const auto stp2 = high_resolution_clock::now(); const auto sd = duration_cast&lt;milliseconds&gt;(stp2 - stp1); cout &lt;&lt; "in " &lt;&lt; sd.count() &lt;&lt; " ms, "; cout &lt;&lt; "functor as a struct result = " &lt;&lt; sresult &lt;&lt; endl; const auto btp1 = system_clock::now(); auto fbind = bind(fb, _1, state); double bresult; for(double x=0.0; x&lt; 1.0e8; ++x) { bresult += fbind(x); } const auto btp2 = high_resolution_clock::now(); const auto bd = duration_cast&lt;milliseconds&gt;(btp2 - btp1); cout &lt;&lt; "in " &lt;&lt; bd.count() &lt;&lt; " ms, "; cout &lt;&lt; "functor through bind result = " &lt;&lt; bresult &lt;&lt; endl; return 0; } </code></pre> <p><strong>Update (1)</strong></p> <p>one can also have function as a function object:</p> <pre><code>struct fbs { double operator()(double x, double state) const { return x*state; } }; </code></pre> <p>and in the main.cpp:</p> <pre><code> const auto bstp1 = system_clock::now(); auto fbindstruct = bind(fbs(), _1, state); double bsresult; for(double x=0.0; x&lt; 1.0e8; ++x) { bsresult += fbindstruct(x); } const auto bstp2 = high_resolution_clock::now(); const auto bsd = duration_cast&lt;milliseconds&gt;(bstp2 - bstp1); cout &lt;&lt; "in " &lt;&lt; bsd.count() &lt;&lt; " ms, "; cout &lt;&lt; "functor through bind-struct result = " &lt;&lt; bsresult &lt;&lt; endl; </code></pre> <p>didn't change the speed:</p> <pre><code>&gt; g++ -std=c++11 main.cpp ; ./a.out hi in 2178 ms, functor as a struct result = 1.5708e+16 in 31972 ms, functor through bind result = 1.5708e+16 in 32083 ms, functor through bind-struct result = 1.5708e+16 12:15:27 ~/try &gt; clang++ -std=c++11 main.cpp ; ./a.out hi in 3758 ms, functor as a struct result = 1.5708e+16 in 23503 ms, functor through bind result = 1.5708e+16 in 23508 ms, functor through bind-struct result = 1.5708e+16 </code></pre> <p><strong>Update (2)</strong></p> <p>Adding optimization results in similar times:</p> <pre><code>&gt; g++ -std=c++11 -O2 main.cpp ; ./a.out hi in 536 ms, functor as a struct result = 1.5708e+16 in 510 ms, functor through bind result = 1.5708e+16 in 472 ms, functor through bind-struct result = 1.5708e+16 12:31:33 ~/try &gt; clang++ -std=c++11 -O2 main.cpp ; ./a.out hi in 388 ms, functor as a struct result = 1.5708e+16 in 419 ms, functor through bind result = 1.5708e+16 in 456 ms, functor through bind-struct result = 3.14159e+16 </code></pre> <p>GCC 4.8.1 and Clang 3.3</p> <p><strong>note</strong> Clang 3.3 gives a wrong result for "bind-struct" case</p> <p><strong>Update(3)</strong></p> <p>there are more performance testing at <a href="https://stackoverflow.com/questions/18175309/is-there-a-macro-based-adapter-to-make-a-functor-from-a-class">Is there a macro-based adapter to make a functor from a class?</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload