Question

I am creating a dll in c++ where I will have some functions to use in a Delphi 7 project.

The first function I exported reduces drastically the performance of my program, I already checked with AQtime and the slowest function is the one from my dll.

The function simply checks if a given char is in the array, before using OpenMP directives it worked slowly as well, so it shouldn't be that.

bool Validate(char character)
{

char char_store[] = {',' , '+' , '-' , '*' , '/',
                     '(' , ')' , '^' , '[' , ']' ,
                     '%' , '&' , '=' , '<' , '>' ,
                     ' ' , '.' , ';' , ':', char(13) , char(10)};

bool abort = false;

#pragma omp parallel for
for(int i = 0; i < 21; i++)
{
    #pragma omp flush (abort)
    if(!abort)
    {
        if (char_store[i] == character)
        {
            abort = true;
            #pragma omp flush (abort)
        }
    }
}
return abort;
}

I know in Delphi you can create a function to check the same thing in an easier way, but I find difficult to believe that this small array will cause my program to slow down from a matter of seconds to minutes.

I am using static loading for my dll.

function Validate(character: Char): Boolean; cdecl; external 'Library.dll';

Any reason why is this happening?

No correct solution

OTHER TIPS

Your function is trivial. It's a for loop with 21 iterations that performs a couple of simple equality tests. It can execute in ~100 cycles. The overhead of OpenMP is many orders of magnitude greater.

Simply put, you cannot expect threads to improve performance here. You need much larger tasks of work for threading to be effective. Single threaded code written in Delphi will be the most efficient way to implement the code in your question.

If removing all the OpenMP doesn't help (you say your code is slow without the OpenMP) then you need to look at your compiler options. Are you compiling with optimisation? If you have a question about performance, you do really need to supply a reproducible test case, with timings.

Your C++ code is rather strange too. It is needlessly complex. It could be written like this:

bool Validate(const char character)
{    
    const char char_store[] = {
        ',' , '+' , '-' , '*' , '/', '(' , ')' , '^' , '[' , ']' , '%' ,
        '&' , '=' , '<' , '>' , ' ' , '.' , ';' , ':',
        char(13) , char(10)};

    for(int i = 0; i < 21; i++)        
        if (char_store[i] == character)            
            return true;                    
    return false;
}

There's no point whatsoever in putting this code in a DLL, or trying to use a parallel for. You need to break your program into much bigger units of work for parallelisation to be effective.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top