替换文本时需要很长时间

时间:2015-03-31 08:56:40

标签: livecode

替换数组中的单词时需要大量时间。该数组由(y[1]y[2])组成,超过25000个单词。是否有可能缩短时间?我使用下面的代码替换。

on mouseUp
    put the field SRText into myArrayToBe
    split myArrayToBe by CR
    put the number of lines of (the keys of myArrayToBe) into myArraylength
    repeat with i = 1 to myArraylength 
    put  myArrayToBe[i] into y
    split y by colon
    put y[1] into searchStr
    put y[2] into replaceStr
    if searchStr is empty then
         put the  0 into m
      else
  put the htmlText of field "MytextField" into myHtml
  set the caseSensitive to true
  replace searchStr with  "<strike><font bgcolor=" & quote & "yellow" & quote & ">" & searchStr & "</font></strike><font bgcolor=" & quote & "green" & quote & ">" & replaceStr & "</font>" in myHtml
  set the htmlText of fld "MytextField" to myHtml
  end if
  end repeat
end mouseUp

此循环中是否有错误?

2 个答案:

答案 0 :(得分:1)

您也可以考虑在重复循环之外移动htmltext的放置和设置。

on mouseUp
 put the htmlText of field "MytextField" into myHtml
 set the caseSensitive to true
 put field "SRText" into myArrayToBe
 split myArrayToBe by CR
 repeat for each key myKey in myArrayToBe 
      put  myArrayToBe[myKey] into y
      split y by colon
      put y[1] into searchStr
      put y[2] into replaceStr
      if searchStr is empty then
           put 0 into m
      else   
           replace searchStr with  "<strike><font bgcolor=" & quote & \
                 "yellow" & quote & ">" & searchStr & \
                 "</font></strike><font bgcolor=" & quote & "green" & quote \
                 & ">" & replaceStr & "</font>" in myHtml               
      end if
 end repeat
 set the htmlText of fld "MytextField" to myHtml

结束mouseUp

答案 1 :(得分:0)

尝试使用repeat for each代替repeat with。通常,repeat for each要快得多。看起来阵列往往会减慢重复速度。一个简单的列表可能会更快。

on mouseUp
     put field SRText into myArrayToBe
     put the htmlText of field "MytextField" into myHtml
     set the caseSensitive to true
     repeat for each line myLine in myArrayToBe 
          set the itemDel to colon
          if item 1 of myLine is empty then
               put 0 into m
          else
               replace item 1 of myLine with "<strike><font bgcolor=" & quote & \
                     "yellow" & quote & ">" & item 1 of myLine & \
                     "</font></strike><font bgcolor=" & quote & "green" & \
                     quote & ">" & item 2 of myLine & "</font>" in myHtml
          end if
     end repeat
     set the htmlText of fld "MytextField" to myHtml
end mouseUp

如果最大的问题是阻止GUI,虽然您可以接受额外的减速,但您还可以添加一行wait for 0 millisecs with messages和一些代码以在重复循环结束时更新进度条。 / p>