Thread: Opitimizing my Loop...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    35

    Angry Opitimizing my Loop...

    hello again,
    can any at all help my speed up this loop ive been at it for weeks on end trying different methods , functions ... but i just cant get it to work properly and faster because the problem is that as the buffer gets bigger its takes longer to go through the loop and it pauses the program until its done :'(

    Code:
    void OutputToDisplay(char *p)
    {
        char R[3],G[3],B[3];
        
         szBuffer2.append(p);
         SetWindowText((HWND)hEditDisplay, szBuffer2.c_str());
         
         //set applicable character formats.
         CHARFORMAT cf;
         ZeroMemory(&cf,sizeof(cf));
         cf.cbSize=sizeof(cf);
         lstrcpy(cf.szFaceName,TEXT("Verdana"));
         cf.bCharSet=ANSI_CHARSET;
         cf.bPitchAndFamily=FF_ROMAN;
         cf.dwMask=CFM_COLOR|CFM_SIZE|CFM_FACE|CFM_EFFECTS;
         cf.dwEffects=0;
         cf.crTextColor=RGB(0,180,255);
         cf.yHeight=150;
         //select all the current text for formatting
         CHARRANGE cr={0,-1};  
         SendMessage(hEditDisplay,EM_EXSETSEL,0,(LPARAM)&cr);
         //let the cntrl know about it
         SendMessage(hEditDisplay,EM_SETCHARFORMAT,SCF_WORD|SCF_SELECTION,(LPARAM)&cf);
         
         int len = strlen(szBuffer2.c_str());
         char data[len];
         
         strcpy(data,szBuffer2.c_str());
         
         int count = 0;
         int countc = 0,countcc = 0;
         int iOCcount = 0,iCCcount = 0;    
         int iTotalClength = 0;
         int clength = 0;
         
         for(int i = 0;i<strlen(data);i++)
         {
          if(data[i] == '\n'){
           count++;
          }
    
          if(data[i] == C_COLOR){ 
          
          int counter = 1;
          int l = 0,m = 0,n = 0;
           
          if(data[i+1] == '('){
          for(int j=i;j<i+14;j++){
           if(data[j] == ')'){
            clength = j-i;
           }
          }
          
           SendMessage((HWND)hEditDisplay, EM_SETSEL, (i-count)-(iCCcount+iTotalClength), ((i-count)-(iCCcount+iTotalClength))+(clength+1));
           SendMessage((HWND)hEditDisplay, EM_REPLACESEL, 0, (LPARAM)"");
    
           for(int k=i+1;k<(clength+i);k++){
           
           if(data[k] != '('){ 
            if((data[k] != ')') & (counter <= 3)){
             if(data[k] != ','){
              switch(counter){
               case 1:
               {
                R[l] = data[k];
                l++;
               }
               break;
               case 2:
               {
                G[m] = data[k];
                m++;
               }
               break;
               case 3:
               {
                B[n] = data[k];
                n++;
               }
               break;                      
              }
             }else{
              counter++;
             }
            }else{
             k = clength;
            }
           }
           }
                  
           if((R != "") & (G != "") & (B != "")){
                       //set applicable character formats.
                ZeroMemory(&cf,sizeof(cf));
                cf.cbSize=sizeof(cf);
                lstrcpy(cf.szFaceName,TEXT("Verdana"));
                cf.bCharSet=ANSI_CHARSET;
                cf.bPitchAndFamily=FF_ROMAN;
                cf.dwMask=CFM_COLOR|CFM_SIZE|CFM_FACE|CFM_EFFECTS;
                cf.dwEffects=0;
                cf.crTextColor=RGB(atoi(R),atoi(G),atoi(B));
                cf.yHeight=150;
    
                CHARRANGE cr1={(i-count)-(iCCcount+iTotalClength),strlen(data)};  
                SendMessage(hEditDisplay,EM_EXSETSEL,0,(LPARAM)&cr1);
    
                SendMessage(hEditDisplay,EM_SETCHARFORMAT,SCF_WORD|SCF_SELECTION,(LPARAM)&cf);
    
           }     
          }
          iTotalClength += clength;
          iCCcount++;
          }      
         }  
                          
         SetFocus(hEditDisplay);
         SendMessage((HWND)hEditDisplay, EM_SETSEL, (WPARAM)-1, (LPARAM)-1);
         SendMessage((HWND)hEditDisplay, EM_SCROLLCARET, 0, 0);
         SetFocus(hEditMessage);
    }
    the sring that is sent to it is :-

    char szIntro[150] = "(0,0,225)# Welcome to Hyper Chat v.1 (127,127,127)-\r\n(255,0,0)# Finished Initializing HC GUI. (127,127,127)-\r\n\r\n";

    basically it prints out to a RICHEDIT the formatted text its like mIRC scripting but mine just sucks

    if there is any headers or any functions that already do this then please tell me

    thx alot in advance
    Theres a sucker born every minute, but a swallower is harder to find

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    You appear to be sending too many messages, try to see if you can fold many operations into one larger one. I really can't tell what you are trying to do here, but that would be the first cause of slowness.

    You are converting between char[]'s and std::strings too much, and for no apparent reason. You are also regularly calling strlen(). strlen is not free, it requres looping over every single character. std::string's .size() is as close to free as such things get, seing as how it always knows it's size.

    string also has find_first_of, a good fast way to search for characters.

    R,G,B are of char[] type, != does not work with them, again making them strings will let it work or in your case R[0] = '\0' will test for an empty string.

  3. #3
    Wen Resu
    Join Date
    May 2003
    Posts
    219
    you could have it do this in its own thread, that way the program can continue while this is running, not excatly a great solution but might help

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    35
    have you ever seen mirc, becoz what im trying to do is similar to that, im trying to format certain parts of a richdit with a custom color
    Theres a sucker born every minute, but a swallower is harder to find

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM