求助,为何这段代码性能如此低下

Milky (QAQ)
Posts: 1
Joined: Thu Apr 06, 2023 5:07 am

求助,为何这段代码性能如此低下

Postby Milky (QAQ) » Tue Apr 11, 2023 5:15 pm

- 运行环境:ESP-IDF 4.4
- 运行设备:ESP32S3,已经在编译选项中开启性能优化模式,并把 CPU 频率调到了 240MHz 。
- 目的:模拟 ESP32S3 处理图片,并测试性能
- 问题:测试时发现,以这个参数执行下来,每次计时循环平均耗时 150963 微秒,平均下来一次 float 计算要1微秒。请问是什么原因导致的呢?感谢您的帮助! :D

Code: Select all

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#define M2T(X) ((unsigned int)(X) / portTICK_PERIOD_MS) // ms to tick

#include "esp_random.h"

#include <sys/time.h>

static const char *TAG = "main";
#define dataLen 14400

void makeMatrixUint8(uint8_t *buf, int len)
{

    for (int i = 0; i < len; i++)
    {
        esp_fill_random(&buf[i], sizeof(uint8_t));
    }
}
void makeMatrixFloat(float *buf, int len)
{

    for (int i = 0; i < len; i++)
    {
        esp_fill_random(&buf[i], sizeof(float));
    }
}

static void testTask()
{
    uint8_t [i]testData1 = (uint8_t *)malloc(sizeof(uint8_t) [/i] dataLen);
    float [i]testData2 = (float *)malloc(sizeof(float) [/i] dataLen);
    struct timeval tv_d0;
    struct timeval tv_d1;

    while (1)
    {
        makeMatrixUint8(testData1, dataLen);
        makeMatrixFloat(testData2, dataLen);
        gettimeofday(&tv_d0, NULL);
        for (int t = 0; t < 10; t++)
        {

            for (int i = 0; i < dataLen; i++)
            {
                testData2[i] = testData1[i] * 0.3;
            }
        }
        gettimeofday(&tv_d1, NULL);
        ESP_LOGI(TAG, "%lu", 1000000 * (tv_d1.tv_sec - tv_d0.tv_sec) + (tv_d1.tv_usec - tv_d0.tv_usec));
        
        vTaskDelay(1);
    }
}

void app_main()
{
    xTaskCreate(testTask, "servoTask", 1024 * 4, NULL, tskIDLE_PRIORITY, NULL);
}

QQ26750452
Posts: 17
Joined: Thu May 13, 2021 1:48 pm

Re: 求助,为何这段代码性能如此低下

Postby QQ26750452 » Mon May 22, 2023 2:17 am

既然都是这么简短的代码,占不了多少空间,就把它们放在IRAM里运行看看有多快?
类似这个样子void IRAM_ATTR your_function(...);

ESP_Yake
Posts: 109
Joined: Mon Mar 06, 2017 12:23 pm

Re: 求助,为何这段代码性能如此低下

Postby ESP_Yake » Tue May 23, 2023 8:16 am

浮点的运算就是很慢,即使 S3 有了一个单精度硬件浮点单元,我们的建议你看看能否将里面一些浮点步骤优化成整形进行处理,参考: https://docs.espressif.com/projects/esp ... rall-speed
另外,以下是一个用户测试的我们 ESP32 系列芯片浮点测试结果,供你参考 https://esp32.com/viewtopic.php?p=82090#p82090

Who is online

Users browsing this forum: Bing [Bot] and 55 guests