3
\$\begingroup\$

Due to software constraints, I cannot use the standard libraries, math.h, algorithm, templates, inline, or boost. I am also using standard C (ISO C99) such that array is not a reserved keyword like it is in Visual Studio.

I need to duplicate the Matlab any function in C / C++ for an array. I currently have written two any functions with different inputs depending on the array size.

  • Is it possible to write just one any function without using templates?
  • How can I improve performance?

I am striving for const correctness, performance/efficiency, and avoiding implicit type casting.

bool any(bool* array, const int N){
    // mimics the behavior of Matlab's any() function. Returns true if any element of the array is true
    bool val;
    val = array[0] == true;
    int i = 0;
    while (val == false && i < N){
        val = array[i++] == true;
    }
    return val;
}

bool any(bool** array, const int nRow, const int nCol){
    // mimics the behavior of Matlab's any() function. Returns true if any element of the array is true
    bool val;
    val = array[0][0] == true;
    int i = 0;
    while (i < nRow && val == false){
        int j = 0;
        while (j < nCol && val == false){
            val = array[i][j++] == true;
        }
        i++;
    }
    return val;
}
\$\endgroup\$
3
  • \$\begingroup\$ So I already recommend that const int N should be a size_t \$\endgroup\$ Commented Oct 4, 2011 at 19:06
  • 1
    \$\begingroup\$ Passing N as a const does not add to const correctness as it is passed by value. Just pass size_t \$\endgroup\$ Commented Oct 4, 2011 at 19:51
  • 1
    \$\begingroup\$ If you must use == true to convert a bool to a bool, then remember that the result is still a bool. Therefore, you should write val = (array[0] == true) == true;. Or maybe just val = array[0]. \$\endgroup\$ Commented Oct 5, 2011 at 13:24

2 Answers 2

2
\$\begingroup\$

I would suggest having the 2D implementation call the 1D implementation, rather than duplicating the "any" logic in both implementations, e.g.

bool any(bool** array, const int nRow, const int nCol){
    // mimics the behavior of Matlab's any() function. Returns true if any element of the array is true
    bool val = false;

    for (int i = 0; i < nRow && val == false; ++i)
    {
        val = any(array[i], nCol); // call 1D version of any
    )
    return val;
}
\$\endgroup\$
1
\$\begingroup\$

Depending on how you compiler implements it.
It may potentially be faster to allcoate a block of zero memory and compare it (This of course will be platform dependent). Some instruction sets allow all the following to be done in one op code (each line). But of course any speed improvements you get have to be weighed against the readability.

bool any(bool* array, size_t N)
{
    void* comp   = alloca(sizeof(bool) * N);
    bzero(comp, sizeof(bool) * N);
    int   result = bcmp(array, comp, sizeof(bool) * N);

    return result != 0;
}

Personally I think you are way to concerned about micro optimizations.
These should be irrelevant to you. The compiler is very good at this kind of macro optimization.

\$\endgroup\$
7
  • \$\begingroup\$ where do you see macro optimization? I don't have any templates, inlines, or #define. \$\endgroup\$ Commented Oct 4, 2011 at 20:15
  • \$\begingroup\$ You are worrying about macro optimizations. These are optimizations that the compiler does like peephole, loop-unrolling, etc etc. i.e. Worrying about minor changes in the source to try and make it more efficient. \$\endgroup\$ Commented Oct 4, 2011 at 20:19
  • \$\begingroup\$ I generally agree with you. Unfortunately due to certain software certification issues, I have to worry about minor changes, etc. Please being aware of good software practices techniques is never bad thing. \$\endgroup\$ Commented Oct 4, 2011 at 20:24
  • \$\begingroup\$ Speaking of good software practices from SO: "Why is alloca not considered good practice?" : stackoverflow.com/questions/1018853/… \$\endgroup\$ Commented Oct 4, 2011 at 20:25
  • 3
    \$\begingroup\$ @Tux-D: the usual term for a small, localised optimisation is "micro-optimisation" (not "macro"). \$\endgroup\$ Commented Oct 4, 2011 at 23:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.