Search code examples
pythonc++pybind11

pybind11 somehow slows c++ function


All stackoverflow/github issues I've seen were about speeding up functions calls from Python in case of marshalling objects. But my problem is about the working time of pure c++ function inside pybind11 C++ module function.

I have the training function that loads dataset and calls train method from the native C++ library class:

void runSvm() 
    // read smvlight file into required sparse representation
    auto problem = read_problem( "random4000x20.train.svml" );

    CSvmBinaryClassifierBuilder::CParams params( CSvmKernel::KT_Linear );
    params.Degree = 3;
    params.Gamma = 1/20;
    params.Coeff0 = 0;

    // measure time here
    using namespace std::chrono;
    system_clock::time_point startTime = high_resolution_clock::now();

    CSvmBinaryClassifierBuilder( params ).Train( *problem ); // measure time only for this line

    nanoseconds delay = duration_cast<nanoseconds>( high_resolution_clock::now() - startTime );
    std::cout << setprecision(3) << delay / 1e6 << std::endl;
}

I bound this function to Python via pybind11:

PYBIND11_MODULE(PythonWrapper, m) {
    m.def( "runSvm", &runSvm );
}

Then compiled pybind11 module library and called it from Python. The timer value was over 3000ms. But when I call this function from pure C++, the timer value is around 800ms. Of course, I was expecting some overhead but not in this place and not so much.

I ran it in one thread and both cases it 100% loaded one core.

Where the issue can be? Who faced the same and how did you handle this?


Solution

  • When I was working on a reproducible example, I found out that I compare different svm kernels in C++ example (it was based on libsvm params proved 'rbf') and in pybind11 lib (it was hardcoded 'linear'). After fixing it and comparing the same algorithms there was no difference in time.