当前位置:   article > 正文

STM32G4 Math Operation Benchmark Test

STM32G4 Math Operation Benchmark Test

Test Condition

MCU: STM32G431RB @170MHz
IDE: IAR V9.40
Optimization: -o3

Math Operation Benchmark test

OP times: 1M

Multiply

Float32Q31Q15
a = 1, b = 2588234705852941
a = 10000, b = 2588234705852941
a = 10000, b = 20000588234705852941
a = 1, b = 20000588234705852941
a = 32767, b = 2588234705852941
a = 2147483647, b = 25882347058-
a = -1, b = 2588234705852941
a = 1, b = -2588234705852941
a = -1, b = -2588234705852941

上述表格中Q15的测试数据为把相乘的结果赋值给int16_t,如果改为把结果赋值给int32_t,则所耗时间与Q31一致,更少。

Division

Float32Q31Q15
a = 1, b = 21294115882358823
a = 10000, b = 21294118823588235
a = 10000, b = 200001294115882358823
a = 32767, b = 21294118823588235
a = 2147483647, b = 2129411111764-
a = -1, b = 21294115882358823
a = 1, b = -21294117058870588
a = -1, b = -21294117058870588

似乎同样的代码重新编译以后测试结果又不一样。。。

几点观察结论:

  • 浮点数的乘除用时与操作数无关
  • 浮点数的乘法用时比定点多 20% 左右
  • 浮点数的除法所用时间在操作数较小时大约是定点数的倍多,当被除数值接近极限时32位定点除法与浮点除法接近
  • 盲猜在电机控制程序里,如果用全浮点数计算,所耗时间资源将会大约是定点数的150%左右

Test Code

volatile uint32_t sMulDurationUs;
volatile uint32_t iMulDurationUs;
volatile uint32_t fMulDurationUs;

volatile uint32_t sDivDurationUs;
volatile uint32_t iDivDurationUs;
volatile uint32_t fDivDurationUs;

void MathOpBenchmarkTest(void)
{

  volatile int16_t sa = 1;
  volatile int16_t sb = 2;
  volatile int16_t sc = 3;

  volatile int32_t ia = 1;
  volatile int32_t ib = 2;
  volatile int32_t ic = 3;

  volatile float32_t fa = 1;
  volatile float32_t fb = 2000;
  volatile float32_t fc = 3;

  volatile uint32_t startTick = DWT->CYCCNT;
  for (uint32_t i = 1000000; i > 0; i--) {
    sc = sa * sb;
  }
  volatile uint32_t stopTick = DWT->CYCCNT;
  sMulDurationUs = TimeMeasure(startTick, stopTick);

  startTick = DWT->CYCCNT;
  for (uint32_t i = 1000000; i > 0; i--) {
    ic = ia * ib;
  }
  stopTick = DWT->CYCCNT;
  iMulDurationUs = TimeMeasure(startTick, stopTick);

  startTick = DWT->CYCCNT;
  for (uint32_t i = 1000000; i > 0; i--) {
    fc = fa * fb;
  }
  stopTick = DWT->CYCCNT;
  fMulDurationUs = TimeMeasure(startTick, stopTick);

  startTick = DWT->CYCCNT;
  for (uint32_t i = 1000000; i > 0; i--) {
    sc = sa / sb;
  }
  stopTick = DWT->CYCCNT;
  sDivDurationUs = TimeMeasure(startTick, stopTick);

  startTick = DWT->CYCCNT;
  for (uint32_t i = 1000000; i > 0; i--) {
    ic = ia / ib;
  }
  stopTick = DWT->CYCCNT;
  iDivDurationUs = TimeMeasure(startTick, stopTick);

  startTick = DWT->CYCCNT;
  for (uint32_t i = 1000000; i > 0; i--) {
    fc = fa / fb;
  }
  stopTick = DWT->CYCCNT;
  fDivDurationUs = TimeMeasure(startTick, stopTick);

  sc++;
  ic++;
  fc++;
  
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/429156
推荐阅读
相关标签
  

闽ICP备14008679号