algorithm - Variable Number of Operations to Perform on Data -
please correct me if not possible c++ here idea: have set of data to, @ runtime, perform addition (+), subtraction(-), and/or multiplication(*). have 3 loops achieve this, means can slow. i'd put these operations single loop.
pseudocode:
data applyoperations(const data &a, ... const data &n, operatora(), ..., operatorn()) { (size_t = 0; < a.size(); ++i) result[i] = a[i] operatora() ... n[i] operatorn(); return result; }
this way, can apply n operations in whatever order want in single loop. can point me right direction achieve in c++11?
thanks!
basically have 2 nested loops, 1 on "array" of n sets , operators, , 1 on m elements in each set.
from complexity analysis point of view makes no difference loop outer one; complexity o(n*m). however, if data sets passed argument functions in fact same set, on modern architectures better performance having iteration on data items outer one. reason effect of caches. if iterate on data on , over, you're going have more cache misses, have heavy effect on performance. of course, if you're rally passing different data set each operator, there no difference.
Comments
Post a Comment