Why C++ code runs slowly?

User avatar
nvtby_espf
Posts: 15
Joined: Wed Mar 03, 2021 1:11 pm
Location: Belarus
Contact:

Why C++ code runs slowly?

Postby nvtby_espf » Sun May 16, 2021 12:48 am

Hi! Shown below is a code from C++ method, each statement with elapsed time. I feel it should run 10x times faster on ESP32-S2. Given CPU clock as 4.2 ns per tick (240 MHz), simple sum takes ~60 ticks. I think it's unusually high. I searched for Xtensa docs but did not find reference numbers for how much math instructions take.
Compiler makes code under performance option. Memory is SRAM. Arrays were allocated by "new" operator. Measurements were made by system timer and factored to nanoseconds.
Do you have any idea what's wrong? Did you conduct similar tests but got faster results?

Code: Select all

    float* x = matrixTest+1;
    float* y = matrixTest+1+10000;
    int i;
    for( i = 0; i < 10000; i++ )
    {
        if(    ! x[i]      ) break; // 130 ns
        if( y[0] == x[i]   ) break; // 129 ns
        if( y[i] == x[i]   ) break; // 133 ns
        if( x[i]+y[i] < 0. ) break; // 370 ns
        if( y[i]-x[i] < 0. ) break; // 445 ns
        if( y[i]*x[i] < 0. ) break; // 409 ns
        if( y[i]/x[i] < 0. ) break; // 1381 ns
    }
    printf("i=%d\n",i);
Measurements were made when only one line was uncommented. A "matrixTest" array is a class member. It has 40000 elements, resides in heap. It was allocated and initialized before the test.
Thanks

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Why C++ code runs slowly?

Postby ESP_Angus » Tue May 18, 2021 12:16 am

Hi nvbty,

The ESP32-S2 doesn't have a hardware floating point unit, so all floating point calculations have to be emulated in software. Unfortunately, this is pretty slow.

If you can find a way to use integer or fixed-point math (I believe there are some good C++ fixed point arithmetic libraries out there), then it will be noticeably faster.

It will also be faster on ESP32 or the upcoming ESP32-S3 (both have hardware single-precision floating point units), although floating point calculations are always slower than integer ones even with a hardware FPU.

Who is online

Users browsing this forum: No registered users and 89 guests