Borland C ++ Builder 6字符串操作太慢了

时间:2017-01-14 20:17:24

标签: c++ string c++builder hang c++builder-6

我有一个学期通过的程序,但有问题。它将文本读入<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:strip-space elements="*"/> <xsl:template match="/root/entry"> <xsl:text>Entry: </xsl:text> <xsl:value-of select="."/> <xsl:text>&#xa;</xsl:text> <!-- Split the string --> <xsl:variable name="tbl" select="tokenize(., ';')"/> <!-- # of semicolons --> <xsl:variable name="cnt" select="count($tbl) - 1"/> <xsl:text># of ';': </xsl:text> <xsl:value-of select="$cnt"/> <xsl:text>&#xa;</xsl:text> <!-- Show each substring --> <xsl:for-each select="$tbl"> <xsl:value-of select="position()"/> <xsl:text>. </xsl:text> <xsl:value-of select="."/> <xsl:text>&#xa;</xsl:text> </xsl:for-each> </xsl:template> </xsl:stylesheet> ,然后在TStringList中对其进行操作。编译时一切正常。但是当我在没有安装Borland的情况下运行EXE文件时,它会在执行除此程序之外的任何操作时挂起,例如在其上运行浏览器。它只发生在3000行等大文件中。我该如何解决这个问题?

[由Spektre重建表格来源]

unit1.cpp

TMemo

unit1.h

//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdio.h>
#include <strutils.hpp>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
AnsiString NazwaPliku;
AnsiString szerstop;
AnsiString dlustop;
AnsiString final;
AnsiString Linia;
AnsiString Linia1;
TStringList *Lista=NULL;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner):TForm(Owner)
    {
    Lista = new TStringList;
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::Wczytaj1Click(TObject *Sender)
    {
    if(OpenDialog1->Execute())
        {
        Lista->LoadFromFile(OpenDialog1->FileName);
        NazwaPliku=OpenDialog1->FileName;
        Memo1->Lines->LoadFromFile(NazwaPliku);
        }
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::Zamknij1Click(TObject *Sender)
    {
    if (Application->MessageBox("Czy na pewno zakończyć?","Tak",MB_YESNO|MB_ICONQUESTION)==IDYES)
        {
        Application->Terminate();
        }
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::Konwertuje1Click(TObject *Sender)
    {
    AnsiString Poczatek="<?xml version=\"1.0\" encoding=\"UTF-8\"?><kml xmlns=\"http://www.opengis.net/kml/2.2\"><Document><name>Ścieżka</name><description>.</description><Style id=\"yellowLineGreenPoly\"><LineStyle><color>7f00ffff</color><width>75</width></LineStyle><PolyStyle><color>7f00ff00</color></PolyStyle></Style><Placemark><name>Absolute Extruded</name><description>.</description><styleUrl>#yellowLineGreenPoly</styleUrl><LineString><extrude>1</extrude><tessellate>1</tessellate><altitudeMode>absolute</altitudeMode><coordinates>";
    AnsiString Koniec="</coordinates></LineString></Placemark></Document></kml>";
    Memo1->Clear();
    Memo1->Lines->Add(Poczatek);
    for(int i=0; i<Lista->Count; i++)
        {
        AnsiString str_linia=Lista->Strings[i];
        char* linia=str_linia.c_str();
        if(strstr(linia,"GPGGA")!=NULL)
            {
            int stopnies, stopnied, lpz;
            double czas, minutys, minutyd, szer_stop, dlu_stop;
            char ms, md;
            lpz=sscanf(linia, "$GPGGA,%lf,%2d%lf,%c,%3d%lf,%c", &czas, &stopnies, &minutys, &ms, &stopnied, &minutyd, &md);
            Sleep(0);
            if(lpz==7)
                {
                if(ms=='N') szer_stop=stopnies+(minutys/60.0);
                 else       szer_stop=-(stopnies+(minutys/60.0));
                if(md=='E') dlu_stop=stopnied+(minutyd/60.0);
                else        dlu_stop=-(stopnied+(minutyd/60.0));
                }
            Edit1->Text=FloatToStr(szer_stop);
            Edit1->Refresh();
            Edit2->Text=FloatToStr(dlu_stop);
            Edit2->Refresh();
            }
        AnsiString szerstop=Edit1->Text;
        szerstop=StringReplace(szerstop,",",".",TReplaceFlags());
        AnsiString dlustop=Edit2->Text;
        dlustop=StringReplace(dlustop,",",".",TReplaceFlags());
        AnsiString final=dlustop+","+szerstop;
        Memo1->Lines->Add(final);
        }
    Memo1->Lines->Add(Koniec);
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::Wyczysc1Click(TObject *Sender)
    {
    Memo1->Clear();
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::Zapiszjako1Click(TObject *Sender)
    {
    SaveDialog1->Execute();
    Memo1->Lines->SaveToFile(SaveDialog1->FileName);
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
    {
    if(Application->MessageBox("Czy na pewno zakończyć?","Tak",MB_YESNO | MB_ICONQUESTION)==IDNO)
        {
        Action=caNone;
        }
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::Czcionka1Click(TObject *Sender)
    {
    if (FontDialog1->Execute());
        {
        Memo1->Font->Name=FontDialog1->Font->Name;
        Memo1->Font->Color=FontDialog1->Font->Color;
        Memo1->Font->Size=FontDialog1->Font->Size;
        Memo1->Font->Style=FontDialog1->Font->Style;
        }
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::Info1Click(TObject *Sender)
    {
    ShowMessage("");
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::Filtruj1Click(TObject *Sender)
    {
    Memo1->Clear();
    AnsiString Linia1;
        {
        for(int j=0; j<Lista->Count;j++)
            {
            AnsiString str_linia1=Lista->Strings[j];
            char* linia1=str_linia1.c_str();
            if(strstr(linia1,"GPGGA")!=NULL)
            Memo1->Lines->Add(linia1);
            Sleep(0);
            }
        }
    }
//---------------------------------------------------------------------------
void __fastcall TForm1::PrzywrcTXT1Click(TObject *Sender)
    {
    Memo1->Clear();
    Memo1->Lines->LoadFromFile(NazwaPliku);
    }
//---------------------------------------------------------------------------

unit1.dfm

//$$---- Form HDR ----
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Dialogs.hpp>
#include <Buttons.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TMemo *Memo1;
    TOpenDialog *OpenDialog1;
    TPanel *Panel1;
    TSpeedButton *Wczytaj1;
    TSpeedButton *Zamknij1;
    TSpeedButton *Konwertuje1;
    TEdit *Edit1;
    TSaveDialog *SaveDialog1;
    TEdit *Edit2;
    TSpeedButton *Wyczysc1;
    TSpeedButton *Zapiszjako1;
    TFontDialog *FontDialog1;
    TSpeedButton *Czcionka1;
    TSpeedButton *Info1;
    TSpeedButton *Filtruj1;
    TSpeedButton *PrzywrcTXT1;
    void __fastcall Wczytaj1Click(TObject *Sender);
    void __fastcall Zamknij1Click(TObject *Sender);
    void __fastcall Konwertuje1Click(TObject *Sender);
    void __fastcall Wyczysc1Click(TObject *Sender);
    void __fastcall Zapiszjako1Click(TObject *Sender);
    void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
    void __fastcall Czcionka1Click(TObject *Sender);
    void __fastcall Info1Click(TObject *Sender);
    void __fastcall Filtruj1Click(TObject *Sender);
    void __fastcall PrzywrcTXT1Click(TObject *Sender);
private:    // User declarations
public:     // User declarations
    __fastcall TForm1(TComponent* Owner);
void __fastcall TForm1::Idleloop(TObject *Sender, bool &Done);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

1 个答案:

答案 0 :(得分:1)

最简单的将是:

  1. 删除所有Sleep()
  2. 添加Application->ProcessMessages()

    进入工作循环(每次n-th迭代一次)

  3. 在您还在做任何事情时,将lock添加到任何操作中

    避免一次运行多个事物。您可以在执行前设置Enabled=false;,然后在Enabled=true;之后设置任何与您的操作冲突的按钮。您可以将所有按钮放在单个面板上,然后启用/禁用面板(甚至使其不可见)。

  4. 线程是理想的,但是如果你没有使用它们,那么很难实现和调试,所以我不建议这样做。还有OnIdleEvent VCL 中未执行任何操作时运行。您可以在那里移动您的计算,但是您需要按时间限制它,因此将操作分开以使其工作一段时间然后返回 VCL 然后再次继续...

    您可以优化代码,例如,将文本累积到AnsiString然后将其添加为整体而不是逐行添加到TMemo通常会更快,例如:

    AnsiString txt="";
    txt+="123\r\n";
    txt+="456\r\n";
    txt+="789\r\n";
    Memo1->Lines->Add(txt);
    

    另外如果我看对了你就按照我的口味做了许多字符串复制......

    为什么使用Edit1,Edit2作为临时变量?这是SLOOW,因为Edit1->Text中的任何更改都会调用重绘和填充...为什么不使用一些AnsiString而在循环后填充Edit1->Text ...