I need to perform a rather complicated check on a vector, and I have to repeat it thousands and millions of times. To make it more efficient, I translated this formula into C ++ source code and compiled it in a highly optimized binary format, which I call in my code. The formula is always pure Boolean: only & &, || as well as! used. Typical source code is as follows:
#include <assert.h> #include <vector> using DataType = std::vector<bool>; static const char T = 1; static const char F = 0; const std::size_t maxidx = 300; extern "C" bool check (const DataType& l); bool check (const DataType& l) { assert (l.size() == maxidx); return (l[0] && l[1] && l[2]) || (l[3] && l[4] && l[5]); //etc, very large line with && and || everywhere }
I will compile it as follows:
g++ -std=c++11 -Ofast -march=native -fpic -c check.cpp
The effectiveness of the resulting binary is critical.
It worked great with a recent test case with lots of variables (300, as you can see above). In this case, g ++ consumes more than 100 GB of memory and freezes forever.
My question is pretty simple: how can I simplify this code for the compiler? Should I use some additional variables, get rid of the vector or something else?
EDIT1: Ok, here is a screenshot from the top utility.

cc1plus is busy with my code. The test function depends on 584 variables (sorry for the inaccurate number in the above example) and contains 450,000 expressions.
I agree with @akakatak's comment below. G ++ seems to be doing something O (N ^ 2).
source share