c - 我怎样才能最小化这个程序的代码大小?

标签 c memory optimization arduino

我的内存力有些问题。是否可以在这个函数中减少编译程序的内存?

它使用时间变量 {hh,mm,ss.0} 进行一些计算,并根据当前进度 (_SHOOT_COUNT) 返回时间(以 millis 为单位) )

unsigned long hour_koef=3600000L;
unsigned long min_koef=60000;

unsigned long timeToMillis(int* time)
{
  return (hour_koef*time[0]+min_koef*time[1]+1000*time[2]+100*time[3]);
}

float Func1(float x)
{
  return (x*x)/(x*x+(1-x)*(1-x));
}

float EaseFunction(byte percent,byte type)
{
  if(type==0)
    return Func1(float(percent)/100); 
}


unsigned long DelayEasyControl()
{

  long dd=timeToMillis(D1); 
  long dINfrom=timeToMillis(Din);
  long dOUTto=timeToMillis(Dout);
  if(easyINmode==0 && easyOUTmode==0) return dd;
  if(easyINmode==1 && easyOUTmode==0)
  {
    if(_SHOOT_COUNT<duration) return (dINfrom+(dd-dINfrom)*EaseFunction(_SHOOT_COUNT*100/duration,0));
    else return dd;
  } 
  if(easyOUTmode==1)
  {
    if(_SHOOT_COUNT>=_SHOOT_activation && _SHOOT_activation!=-1)
    {   
      if((_SHOOT_COUNT-_SHOOT_activation)<current_settings.delay_easyOUT_duration) return (dOUTto-(dOUTto-dd)*(1-EaseFunction((_SHOOT_COUNT-_SHOOT_activation)*100/duration,0)));
      else return dOUTto;
    } 
    else 
    {
      if(easyINmode==0) return dd;
      else if(_SHOOT_COUNT<duration) return (dINfrom+(dd-dINfrom)*EaseFunction(_SHOOT_COUNT*90/duration,0));
      else return dd;
    }
  }
}

最佳答案

您提到您要优化的是代码大小,并且您正在 Arduino 克隆(基于 ATmega32U4)上执行此操作。

那些 Controller 没有对 float 的硬件支持,所以都将在占用大量代码的软件中模拟。

尝试重写它来做定点运算,这样你会节省很多代码空间。

通过优化其他数据类型,您可能会看到一些小的 yield ,即 uint16_t 而不是 long 可能足以满足某些值,并将函数标记为 inline 可以保存跳转所需要的指令。当然,编译器可能已经内联了。

关于c - 我怎样才能最小化这个程序的代码大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23108950/

相关文章:

c - GCC - 如何停止链接 malloc?

C 解引用指针

C++/g++ : How does the compiler handle memory-allocation in this situation?

javascript - 如何在不使用 document.writes 的情况下在 document.getelementById 函数中编写 for 循环

java - 在Java中调用C程序

c++ - 指针数组和指向指针数组的指针

javascript - 确保在 r.js 优化的应用程序中调用 RequireJS 模块

python - Python中全局变量和局部变量的速度

c - 如何在 C 程序中运行 cmd 命令

C++对象,成员的内存位置偏移