Why C++ code runs slowly?
Posted: 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?
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
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);
Thanks