# Slowdown vs C++/Python for Eigen matrix multiplication via ccall

**URL:** https://discourse.julialang.org/t/slowdown-vs-c-python-for-eigen-matrix-multiplication-via-ccall/133783
**Category:** General Usage
**Tags:** performance
**Created:** [November 10, 2025, 11:09pm UTC](https://discourse.julialang.org/t/slowdown-vs-c-python-for-eigen-matrix-multiplication-via-ccall/133783 "2025-11-10T23:09:34Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![nrypkema](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nrypkema/32/219366_2.png) [@nrypkema](https://discourse.julialang.org/u/nrypkema)
#### Post date: [November 10, 2025, 11:09pm UTC](https://discourse.julialang.org/t/slowdown-vs-c-python-for-eigen-matrix-multiplication-via-ccall/133783/1 "2025-11-10T23:09:35Z")

</div>

I’m hoping someone can explain the performance degradation that I’m seeing with Eigen matrix multiplication (and other methods in the Eigen C++ library).

I have a simple example here:

> **[GitHub - nrypkema/ccall\_example: Demo for ccall+Eigen slowdown in Julia](https://github.com/nrypkema/ccall_example)**
>
> Demo for ccall+Eigen slowdown in Julia

The C++ file has a simple example of a function that multiplies two 3000x3000 Eigen matrices together (with the BLAS preprocessor directive EIGEN\_USE\_BLAS), which is compiled into a library libccall.so as well as an executable ccall\_ex. The library surfaces the function (matmulcpp) for use by Python and Julia.

```julia-auto
void matmulcpp() {
    auto t0 = std::chrono::high_resolution_clock::now();
    const int N = 3000;
    Eigen::MatrixXd A = Eigen::MatrixXd::Random(N, N);
    Eigen::MatrixXd B = Eigen::MatrixXd::Random(N, N);
    Eigen::MatrixXd C = A * B;
    auto t1 = std::chrono::high_resolution_clock::now();
    double elapsed_ms = duration_cast<std::chrono::microseconds>(t1 - t0).count() / 1000.0;
    std::cout << "Elapsed: " << elapsed_ms << " ms\n";
}

```

Running the C++ executable outputs the timings (e.g.):

```julia-auto
> ./build/ccall_ex
Elapsed: 604.718 ms
Elapsed: 573.624 ms
Elapsed: 585.415 ms
Elapsed: 559.289 ms
Elapsed: 576.903 ms

```

I get similar performance calling the function from Python using ctypes (e.g.):

```julia-auto
> python3 ccall.py
Elapsed: 588.029 ms
Elapsed: 600.39 ms
Elapsed: 580.682 ms
Elapsed: 615.011 ms
Elapsed: 585.849 ms

```

But in Julia, using ccall, I get significantly worse performance (e.g.):

```julia-auto
> julia ccall.jl
Elapsed: 1437.66 ms
Elapsed: 1405.58 ms
Elapsed: 1389.82 ms
Elapsed: 1394.7 ms
Elapsed: 1395.29 ms

```

Can anyone explain why I’m seeing this performance degradation? Thanks for the help!

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [November 10, 2025, 11:40pm UTC](https://discourse.julialang.org/t/slowdown-vs-c-python-for-eigen-matrix-multiplication-via-ccall/133783/2 "2025-11-10T23:40:56Z")

</div>

Are you using the same BLAS library? The same number of BLAS threads?

That’s all that really matters for large matrix multiplication; the language impact should be tiny because all the work is done in the BLAS library. We see these kinds of posts over and over, and the differences invariably come down to the BLAS configuration.

---

<div class="post-metadata">

### Author: ![nrypkema](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nrypkema/32/219366_2.png) [@nrypkema](https://discourse.julialang.org/u/nrypkema)
#### Post date: [November 10, 2025, 11:54pm UTC](https://discourse.julialang.org/t/slowdown-vs-c-python-for-eigen-matrix-multiplication-via-ccall/133783/3 "2025-11-10T23:54:16Z")

</div>

Ah yes, if I set OPENBLAS\_NUM\_THREADS explicitly, then I get similar performance across all three. I suppose without setting this environmental variable then Julia defaults to 1.

Thanks!
