c++ - Most cpu-efficient way to use std:: algorithms with arguments to a variadic function template? -


You have a variadic function template that takes a sequence of functor and homogeneous types, and you Std :: Accumulate sequence fold, like:

  template & lt; Typename BinaryFuncType, typename ... ArgTypes & gt; Do_something (const BinaryFuncType & amp; F, CONST ERTTYPE & amp; ... objects) {// ... // use std :: to collect 'objects' fold 'F' // ...}  

What variation algorithms ( std :: accumulate ) directly pass variadic logic ( objects )

Itemprop = "text">

Apparently yes, l In what twisted way. Consider the following code:

  #include & lt; Algorithm & gt; # Include & lt; Array & gt; # Include & lt; Cstdio & gt; #include & lt; Iterator & gt; Template & lt; Typename ... Ts & gt; Int sum (ts ... number) {std :: arrays & lt; Int, sizeof ... (number) & gt; List {{number ...}}; Return std :: Deposit (std :: start (list), std :: end (list), 0); } __ distributed __ ((noinline)) zero f (int x, int y, int z) {std :: printf ("sum =% d \ n", sum (x, y, z)); } Int main (int argc, char * argv []) {int x = std :: atoi (argv [1]); Int y = std :: atoi (argv [2]); Int z = std :: atoi (argv [3]); F (X, Y, Z); }  

I saw the assembly code I generated for clarity by the assembly code for me rewritten by c sum () Has been optimized.

  int sum int x, int y, int z) {int tmp = x; TMP + = Y; Tmp + = z; Return TMP; }  

I can say that the assembly code generated is optimal! If you got rid of it temporarily std :: arrays and unrolled, answer your question: Even if you make a temporary rotational container, then it should be optimized remotely if

compiler is smart enough and your numerical types are simple (underlying type or POD) to create temporary containers Or do not pay to copy the elements in a temporary container Areas if can be optimized away.


Sadly, GCC 4.7 2 He was not perfect:

  int sum (int x, int y, int z) {int a [3]; A [0] = x; A [1] = y; A [2] = z; Int tmp = x; TMP + = Y; Tmp + = z; Return TMP; }  

Unfortunately, it was not recognized that it could get rid of the temporary array. I'll check with the latest GCC from the trunk and if the problem still exists, then file a bug report; It looks like a bug in the adapter.


Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -