delphi - 需要使我的代码更快。将重复语句的结果存储在 bool 数组中

标签 delphi optimization

我的项目涉及历史和实时数据分析。最后阶段包含多种复杂算法(超过 800 个)。总体分析阶段大致如下:

  1. 基本公式计算->结果存储
  2. 使用上述公式的第二阶段公式->结果存储
  3. 第三阶段公式决定所需的操作。

这些第三阶段公式是大块的复杂条件。然而,这些复杂的条件使用有限数量的更简单的语句。结果,这些简单的语句在复杂的条件中重复了很多次。让我给你一个非常简化的例子。

if ((var1[0]<var2[0]*0.5)and(var3[0]=1))or(var15[1]<>var15[0]))
....lots of similar statements....
then take action.

现在,像这样的语句

"(var3[0]=1)" or "(var15[1]<>var15[0])"

在其他 if block 中反复使用。我的想法是解析所有这些独特的简单语句,并自动创建一些代码来计算它们的结果(真/假)并将它们存储在 bool 数组中,在第三阶段开始之前一次。就像这样:

arr[12]:=var1[0]<var2[0]*0.5;
arr[13]:=var3[0]=1;
...
arr[128]:=var15[1]<>var15[0];

然后(再次在编译之前解析我的代码)用相应的数组元素替换更简单的语句。 所以,而不是

if ((var1[0]<var2[0]*0.5)and(var3[0]=1))or(var15[1]<>var15[0]))

看起来像

if ((arr[12])and(arr[13]))or(arr[128])

代码中的这些更改会加快我的执行(计算)时间吗?或者编译器已经做了类似的事情而我只是在浪费时间?请记住,这些简单的语句在每个计算周期中都会重复数十或数百次。在实时数据开始之前,至少需要计算 300,000 个周期。因此,每一点都有帮助。本质上,我是在问变量之间的比较是否比 bool 数组元素值的检索慢;

更新

由于有人要求一些真实的代码,这里是真实代码的一部分。 98% 的代码都是变量(所有带有 [0] 的内容都是变量,允许访问以前的变量值)。 2%是函数。变量是在前一阶段计算的。几乎所有变量都是整数。总共有超过 800 个类似的 block 。至少 300,000 个计算周期。平均时间约为45秒。由于我不需要在这里解释的原因,我应该将其速度提高两倍。如果编写算法的人是程序员,代码可能会采用不同的形式。但他们不是。它们可以处理一些基本的东西,比如条件 block 。这是无法改变的事情。

我注意到有些人来这里是想表达讽刺。请远离。我不需要你的帮助。我们非常欢迎愿意提出建设性意见的人。事实上,我提前感谢他们付出努力阅读这么长的帖子。对于错误的代码格式,我深感抱歉,将其作为代码发布到此处是一次失败的努力。

if   ( ZEChT01Pc[0] < ZEChT02Pc[0])
and( ZEChP01Pc[0] < ZEChP02Pc[0])
and( ZEChT01Bn[0] > ZEChP01Bn[0])
and( BncUp_TL_P_1_2[0] > ZEChT01Bn[0])
and( higSncZEChT01[0] < HZigCh30[0])
and( ((ZEChT01Pc[0] < LZigCh30[0]) )
     or
       ( (ZEChT01Pc[0] < LZigCh3002[0] )    and( LZigCh30[0] < LZigCh3002[0] )    and( ZEChT01Pc[0] <= Bnc_Up_HZigCh_Pr[0]) )
       or
       ( (ZEChT01Pc[0] < LZigCh3002[0] )    and( LZigCh30[0] < LZigCh3002[0] )    and( ZEChT01Pc[0] > Bnc_Up_HZigCh_Pr[0] )    and( ZEChP02Pc[0] < TFZ11EndPc[0])) )
and( ((TL_Pks_1_2.tl_getvalue(0) < LZigCh30[0] + ((HZigCh30[0] - LZigCh30[0] )*0.80)) )
     or
       ( (ZEChT01Pc[0] < ULX2dly[0] )    and( C[0] > DLX2dly[0])) )
and (( (ZECoP01Bn[0] > ZEChP01Bn[0])    and (ZECoP01Bn[0] < ZEChT01Bn[0])
and (( (ZECoP01Pc[0] <= ZECoP02Pc[0])    and (C[0] > TL_ECo_Trs_1_2.tl_getvalue(0) )) or ( (ZECoP01Pc[0] > ZECoP02Pc[0]) )) )
     or
   ( (ZECoP01Bn[0] = ZEChP01Bn[0])    and (ZECoP02Bn[0] < ZEChT02Bn[0])  ))
and (( (C[0] > ULX30[0])    and (C[1] <= ULX30[0])    and (ULX30[0] > TL_Pks_1_2.tl_getValue(0))
and (( (BrUpULX30[1] < ZEchT01Bn[0]) ) or ( (chgZigLCh[0] > BrUpULX30[1]) )) )
     or
     ( (C[0] > TL_Pks_1_2.tl_getvalue(0))    and (C[1] <= TL_Pks_1_2.tl_getvalue(0))    and ( TL_Pks_1_2.tl_getValue(0) > ULX30[0])    and (BncUp_TL_P_1_2[1] < ZEchT01Bn[0]) ))
and( ((uniBrUpULX3002_bn[0] > ZEChT01Bn[0] )    and( uniBrUpULX30[0] > TL_Pks_1_2.tl_getvalue(0) )    and( uniBrUpULX3002[0] < TL_Pks_1_2.tl_getvalue(0)) )= false)
and( ((uniBrUpULX3002_bn[0] > ZEChT01Bn[0] )    and( uniBrUpULX30[0] > TL_Pks_1_2.tl_getvalue(0) )    and( uniBrUpULX3002[0] > TL_Pks_1_2.tl_getvalue(0)) )= false)
and( NoLong[0] = 0)
and( ((TL_Pks_1_2.tl_getvalue(0) < LZigCh30[0] )    and( chgZigLCh[0] < ZEChT01Bn[0] )    and( ULX30[0] > LZigCh30[0]) )= false)
and( ((((C[0] < DLXdly[0] )    and( HZigCh30[0] < DLXdly[0] - Tk(0.0050)) = false)))
      or
      ( (ZEChT01Bn[0] = TFZ10EndBnum[0] )    and( ZEChT01Pc[0] < TFZ20EndPc[0] )    and( higSncZEChT01[0] > TFZ40EndPc[0])) )
and( ((ZEChP01Pc[0] > DLXdly[0] )    and( ZEChT01Pc[0] < DLXdly[0] )    and( HZigCh30[0] < DLXdly[0] )) = false)
and( ((higSncZEChT01[0] > HZigCh30[0] -Tk(0.0010) )    and( Bnc_Dn_HZigCh[0]  > higbarSncZEChT01[0]) )= false)
and( ((TFZ10EndBnum[0] > TFZ11EndBnum[0] )    and( TFZ10Type[0]= 5 )    and( TFZ10Extension[0] = 0 )    and( ULX30[0] < LZigCh30[0]) )= false)
and( ((Bnc_Dn_LZigCh[0] > ZEChT01Bn[0] )    and( C[0] < LZigCh30[0]) )= false)
and( ((ZEChP01Pc[0] > DLXdly[0] )    and( ZEChT01Pc[0] < DLXdly[0] )    and( C[0] < DLXdly[0] )    and( First[0] = -1) )= false)
and( ((LZigCh3002[0] >  DLXdly[0] )    and( LZigCh30[0] <  DLXdly[0] )    and( C[0] < DLXdly[0])    and( HZigCh3002[0] >  DLXdly[0] )    and( HZigCh30[0] <  DLXdly[0] )) = false)
and( ((LZigCh3003[0] >  DLXdly[0] )    and( LZigCh3002[0] <  DLXdly[0] )    and( C[0] < DLXdly[0])
and( HZigCh3002[0] >  DLXdly[0] )    and( HZigCh30[0] <  DLXdly[0] )    and( LZigCh30[0] < DLXdly[0] )    and( currentbar - chgZigLCh[0] <= 3 )) = false)
and( ((((TFZ10EndBnum[0] > TFZ11EndBnum[0] )    and( C[0] > higSncShFrm[0]  - ((higSncShFrm[0] - TFZ10EndPc[0])*0.5) )    and( higBarSncShFrm[0] <= ZEChP02Bn[0]) = false)))
     or
     ( (ZEChT01Pc[0] < DLXdly[0] )    and( C[0] > DLXdly[0])) )
and( ((C[0] <= LZigCh30[0] )    and( H[0] > LZigCh30[0]) = false))
and( ((ZEChT01Pc[0] < ULXdly[0] )    and( ZEChT02Pc[0] < ULXdly[0] )    and( ZEChP01Pc[0] > ULXdly[0] )
and( ZEChP02Pc[0] > ULXdly[0] )    and( ZEChT01Pc[0] < ZEChT02Pc[0])    and  (BncUpDLXdly[0] < ZEchT01Bn[0]) )= false)
and( ((((TFZ11EndBnum[0] > TFZ10EndBnum[0] )    and( ZEChT01Pc[0] > TFZ11EndPc[0] - ((TFZ11EndPc[0] - TFZ10EndPc[0])*0.382)) = false)))
      or
      ( (C[0] > ULXdly[0])) )
and( ((((TFZ10EndBnum[0] > TFZ11EndBnum[0] )    and( TFZ20EndBnum[0] < TFZ11EndBnum[0] )    and( ZEChT01Pc[0] > TFZ11EndPc[0] - ((TFZ11EndPc[0] - TFZ20EndPc[0])*0.382)) )= false))
      or
      ( (C[0] > ULXdly[0])) )
and( ((((TFZ20EndBnum[0] > TFZ11EndBnum[0] )    and( TFZ30EndBnum[0] < TFZ11EndBnum[0] )    and( ZEChT01Pc[0] > TFZ11EndPc[0] - ((TFZ11EndPc[0] - TFZ30EndPc[0])*0.382)) )= false))
      or
      ( (C[0] > ULXdly[0])))
and( ((((TFZ30EndBnum[0] > TFZ11EndBnum[0] )    and( TFZ40EndBnum[0] < TFZ11EndBnum[0] )    and( ZEChT01Pc[0] > TFZ11EndPc[0] - ((TFZ11EndPc[0] - TFZ40EndPc[0])*0.382)) )= false))
      or
      ( (C[0] > ULXdly[0])))
and( ((ZEChP01Pc[0] > ZEChP03Pc[0] )    and( ZEChP01Pc[0] > ZEChP04Pc[0]  )    and( C[0] < DLXdly[0]) = false) )
and (( (( (LZigCh30[0] < DLXdly[0] )    and( LZigCh3002[0] > DLXdly[0] )    and( ZEChP01Pc[0] < DLXdly[0] )    and( C[0] < DLXdly[0]) ) = false))
      or
      ( (ZEchT01Pc[0] = TFZ10EndPc[0]) or (ZEchT02Pc[0] = TFZ20EndPc[0]) or (ZEchT03Pc[0] = TFZ30EndPc[0]) ))
and( NoLong2[0] = 0 )
and( ((ZEChP02Pc[0] > ULX2dly[0] )    and( ZEChP01Pc[0] < ULXdly[0] )    and( C[0] < ULX2dly[0] )    and( BrDnDLXdly[0] < BrUpULXdly[0])
and( Min(ZEChT01Pc[0],ZEChT02Pc[0]) > ULXdly[0] - ((ULXdly[0] - DLXdly[0]) * 0.618)) )= false)
and( ((BrDnDLXdly[0] < BrUpULXdly[0] )    and( Min(ZEChT01Pc[0],ZEChT02Pc[0]) > ULXdly[0] - ((ULXdly[0] - DLXdly[0]) * 0.4))
and( TFZ10EndBnum[0] > TFZ11EndBnum[0] )    and( C[0] < ULX2dly[0] )    and( ULXdly[0] > ULX2dly[0] ) )= false)
and( ((BrDnDLXdly[0] < BrUpULXdly[0] )    and( TFZ10EndPc[0]  > ULXdly[0] - ((ULXdly[0] - DLXdly[0]) * 0.4))
and( TFZ10EndBnum[0] < TFZ11EndBnum[0] )    and( C[0] < ULX2dly[0] )    and( ULXdly[0] > ULX2dly[0] ) )= false)
and( ((ZEChP02Pc[0] > ULX2dly[0] )    and( ZEChP01Pc[0] < ULXdly[0] )    and( C[0] < ULX2dly[0] )    and( BrDnDLXdly[0] < BrUpULXdly[0])
and( C[0] > LZigCh30[0] + ((HZigCh30[0] - LZigCh30[0]) * 0.768) )    and( C[0] < ULX2dly[0]) )= false)
and( ((LZigCh30[0] < DLXdly[0] )    and( DLXdly[0] < HZigCh30[0] )    and( C[0] > LZigCh30[0] + ((DLXdly[0] - LZigCh30[0])*0.618))
and( DLXdly[0] - C[0]  < Tk(0.0040) )    and( C[0] < DLXdly[0] ) )= false)
and( ((((ZEChT01Bn[0] <> TFZ10EndBnum[0] )    and( ZEChT01Pc[0] >= LZigCh30[0])) = false))
      or
      ( (ZEChT01Pc[0] < LZigCh30[0] )    and( C[0] > LZigCh30[0] )    and( LZigCh30[0] > ULXdly[0]))
      or
      ( (LZigCh30[0] < LZigCh3002[0] )    and( chgZigLCh[0] > ZEChT01Bn[0] )    and( ZEChP01Pc[0] < LZigCh3002[0] )    and( C[0] > ZEChT02Pc[0] ) )
      or
      ( (ZEChT01Pc[0] <= DLXdly[0] )    and( C[0] > DLXdly[0])))
and( ((C[0] < TFZ20EndPc[0] )    and( C[0] < LZigCh3002[0] )    and( TFZ20Type[0] > 3 )    and( ((TFZ20EndBnum[0] = ZEChT02Bn[0]) )or( (TFZ20EndBnum[0] = ZEChT03Bn[0]))) )= false)
and( ((((ZEChT01Bn[0] <> TFZ10EndBnum[0] )    and( ZEChT01Pc[0] < LZigCh30[0] )    and( LZigCh30[0] > ULXdly[0]) )= false))
      or
      ( (C[0] < LZigCh30[0] + ((HZigCh30[0] - LZigCh30[0] )*0.618) )    and( HZigCh30[0] - C[0] >= Tk(0.0040)) ))
and( ((LZigCh30[0] < DLXdly[0] )    and( LZigCh3002[0] > DLXdly[0] )    and( ZEChP02Pc[0] < DLXdly[0] + Tk(0.0050) )
and( ZEChP02Pc[0] < LZigCh3002[0] )    and( C[0] < LZigCh3002[0] ) = false))
and ( (( (HZigCh30[0] < DLXdly[0])    and (HZigCh3002[0] > DLXdly[0])    and (ZEchT01Bn[0] = TFZ10EndBnum[0])
and (TFZ10Type[0] = 5 )    and (TFZ10Extension[0] = 0)    and (DLXdly[0] < DLX2dly[0]) ) = false) )
and (( (( (chgULXdly[0] > ZEchP01Bn[0])    and (ULXdly[0] > ULX2dly[0])    and (C[0] < ULXdly[0])    and (ZEchT01Pc[0] > ULX2dly[0]) )=false))
      or
      ( (TFZ10EndBnum[0] = ZEchT01Bn[0])    and (TFZ10Type[0] > 3) ))
and (( (( (chgULXdly[0] > ZEchP01Bn[0])    and (ULXdly[0] > ULX2dly[0])    and (C[0] < ULXdly[0])    and (C[0] < ULX2dly[0]))= false))
    or
    ( ( ZechT01Pc[0] < ULXdly[0] - ((ULXdly[0] - DLXdly[0])*0.75))    and (C[0] > LZigCh30[0])    and (DLX30[0] > LZigCh30[0]) ))
and (( (( (chgULXdly[0] > ZEchP01Bn[0])    and (ULXdly[0] < ULX2dly[0])    and (C[0] < ULXdly[0]) ) = false))
       or
       ( ( ZechT01Pc[0] < ULXdly[0] - ((ULXdly[0] - DLXdly[0])*0.75))    and (C[0] > LZigCh30[0])    and (DLX30[0] > LZigCh30[0]) ))
and (( (TFZ11EndBnum[0] = ZEchP02Bn[0])    and (TFZ11Type[0] > 3)    and (min(ZEchT01Pc[0],ZechT02Pc[0]) = TFZ10EndPc[0])    and (C[0] < ULXdly[0])
and (TFZ10Type[0] = 3)    and (TFZ10Extension[0] = 2)    and (TFZ10EndPc[0] > ULXdly[0] - ((ULXdly[0] - DLXdly[0])*0.5)) ) = false)
and (( (TFZ11EndBnum[0] = ZEchP02Bn[0])    and (TFZ11Type[0] > 3)    and (min(ZEchT01Pc[0],ZechT02Pc[0]) = TFZ10EndPc[0])    and (C[0] < ULXdly[0])
and (( (TFZ10Type[0] = 3)    and (TFZ10Extension[0] = 2) )= false)    and (TFZ10EndPc[0] > ULXdly[0] - ((ULXdly[0] - DLXdly[0])*0.8)) ) = false)
and (( (( (TFZ11EndBnum[0] = ZEchP02Bn[0])    and (TFZ11Type[0] > 3)    and (min(ZEchT01Pc[0],ZechT02Pc[0]) <> TFZ10EndPc[0])) = false))
     or
     ( (min(ZEchT01Pc[0],ZechT02Pc[0]) < ULXdly[0] - ((ULXdly[0] - DLXdly[0])*0.8)) )
     or
     ( (Bnc_Up_LZigCh[0] > ZEchT01Bn[0])    and (ZEchT01Pc[0] > ZechP03Pc[0]) ))
and (( (ZechT01Pc[0] < ZechT02Pc[0])    and (ZechP01Pc[0] < ZechP02Pc[0])    and (ZEchT01Pc[0] > ULXdly[0])    and (C[0] < ZEchT02Pc[0])    and (C[0] > ULXdly[0]) ) = false)
and (( (ZechT01Pc[0] < ZechT02Pc[0])    and (ZechP01Pc[0] < ZechP02Pc[0])    and (ZEchT01Pc[0] > ULXdly[0])    and (ZEchP02Bn[0] = TFZ11EndBnum[0])
and (ZEchT02Bn[0]= TFZ20EndBnum[0])    and (ZEchT01Bn[0]= TFZ10EndBnum[0])    and (TFZ10Type[0] = 3)    and (C[0] > ULXdly[0]) )= false)
and (( (ZechT01Pc[0] < ZechT02Pc[0])    and (ZechP01Pc[0] < ZechP02Pc[0])    and (ZEchT01Pc[0] > ULXdly[0])    and (ZEchP03Bn[0] = TFZ11EndBnum[0])
and (ZEchT02Bn[0]= TFZ20EndBnum[0])    and (ZEchT01Bn[0]= TFZ10EndBnum[0])    and (TFZ10Type[0] = 3)    and (C[0] > ULXdly[0]) )= false)
and (((( (TFZ10Type[0] = 7)    and (TFZ20type[0] = 5)    and (TFZ10Extension[0] = 0)    and (TFZ20Extension[0] = 0)    and (TFZ10EndPc[0] > ULXdly[0] - ((ULXdly[0] - DLXdly[0])*0.5))) = false))
      or
      ( (C[0] > ULXdly[0]) ) or ( (ZechT01Pc[0] < ULX2dly[0] )    and (C[0] > ULX2dly[0])    and (ULX2dly[0] < ULXdly[0]) ))
and (( (max(HZigCh30[0],HZigCh3002[0]) - LZigCh30[0] < Tk(0.0100))    and (C[0] > LZigCh30[0] + ((max(HZigCh30[0],HZigCh3002[0]) - LZigCh30[0])*0.618)) ) = false)
and (( (ZEchP02Pc[0] > ULXdly[0] - ((ULXdly[0] - DLXdly[0])*0.236))    and (ZEchT01Bn[0] <> TFZ10EndBnum[0])    and (BncUpDLXdly[0] < ZEchT01Bn[0])) = false)
and (( (Bnc_Dn_LZigCh[0] > ZEChT01Bn[0] )    and (C[0] < LZigCh30[0]) ) = false)
and (( (TFZ41EndBnum[0] > TFZ10EndBnum[0])    and (C[0] < ULXdly[0])    and  (BncUpDLXdly[0] < ZEchT01Bn[0]) ) = false)
and ( GenL01[0] = false)
and ( GenL02[0] = false)
and ( GenL03[0] = false)
and ( GenL04[0] = True)
then    

感谢您的阅读。

最佳答案

从软件开发的角度来看,您的方法听起来存在严重缺陷。像 if ((arr[12])and(arr[13]))or(arr[128]) 这样的代码本质上是不可读且不可维护的。

您应该为常用的 bool 值创建属性并为它们指定有意义的名称。如果您发现很难命名属性,请不要害怕写

if height>CriticalHeight then

根据我的经验,缓存这些测试结果的性能优势是无法衡量的。您当前的方法肯定会导致缺陷和不正确的代码。

关于delphi - 需要使我的代码更快。将重复语句的结果存储在 bool 数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7620006/

相关文章:

delphi - Delphi中使用VID和PID直接与USB设备进行数据交换

c++ - 在 C++ 中缓慢写入数组

php - 遍历 PHP 数组是否比遍历 MySQLi 关联数组更好?

algorithm - 在固定容量中找到所有可能的段平铺

delphi - 为什么 Delphi 没有在正确的文件夹中输出 DCU 文件?

rest - 为什么 indy 返回不可读的数据但 chrome 加载它正常?

delphi - 计时器自行停止

javascript - 使用纯 JavaScript 进行点分页

mysql - 此简单查询的正确 MySQL 索引

delphi - TListView.Clear 卡住了我的应用程序,我该如何修复它?