今天很郁闷,折腾了一天,也没想出来如何改进,性能还是那么低下#include "time.h"
#include "iostream"
using namespace std;int main()
{
string s= "";
for(int i = 0; i < 1000; i ++)
{
s+="00011001110000010";
}
char ch = '1';
clock_t start,finish;
int size = s.size(); start = clock();
    for (int i=0;i < size;i++)
{
        for (int n = i;(char)s[i] == ch; n++)
            if ((char)s[n] != ch)
            {
if(n < size)
{
if(n == i+1)
cout<<i+1<<endl;
if(n != i +1)
cout<<i+1<<'-'<<n<<endl;
i=n;
}
}
} finish = clock();
cout<<"字符串长度: "<<size<<endl;
cout<<"耗时: "<<finish - start<<"毫秒"<<endl;
return 0;

}现在,处理17000长度的字符串,耗时在2000毫秒左右,如何能提高性能呢?希望各路高手不吝赐教
 
不知道为什么,只能悬赏100分,可怜啊
 
 
 

解决方案 »

  1.   

    using namespace System;
    using namespace System::Text;int main()
    {
        // Create a StringBuilder that expects to hold 50 characters.
        // Initialize the StringBuilder with "ABC".
        StringBuilder^ sb = gcnew StringBuilder("ABC", 50);
    用vc.net的话 不如用stringbuilder优化一下效率
      

  2.   

    你的程序是想做什么?
    两个for循环遍历字符串数组目的是啥?
      

  3.   


    #include "time.h"
    #include "iostream"
    using namespace std;int main()
    {
    string s= "";
    for(int i = 0; i < 1000; i ++)
        {
            s+="00011001110000010";
        }
    int n;
        int ch = '1';
        clock_t start,finish;
        int size = s.size();    start = clock();
        for(i = 0; i < size; ++i)
        {
            for(n = i; s[i] == ch; ++n)
    {
                if (n < size && s[n] != ch)
                {
                    if(n == ++i)
    {
    printf("%d\n", i);
    }
    else
    {
    printf("%d-%d\n", i, n);
    }
                    i=n;
                }
    }
        }    finish = clock();
        cout<<"字符串长度: "<<size<<endl;
        cout<<"耗时: "<<finish - start<<"毫秒"<<endl;    
        return 0;
    }
      

  4.   

    你的90%时间基本都花在了屏幕输出上了,console的滚屏幕效率是很低的。
      

  5.   

    for(n = i; s[i] == ch; ++n)
    这段代码好像意义不大吧
      

  6.   


    #include "time.h"
    #include "iostream"
    using namespace std;int main()
    {
    string s= "";
        for(int i = 0; i < 1000; i ++)
        {
            s+="00011001110000010";
        }
        int n;
        int ch = '1';
        clock_t start,finish;
        int size = s.size();
    string buf;
    char sTmp[32];    start = clock();
        for(i = 0; i < size; ++i)
        {
            for(n = i; s[i] == ch; ++n)
            {
                if (n < size && s[n] != ch)
                {
                    if(n == ++i)
                    {
    sprintf(sTmp, "%d\n", i);
                    }
                    else
                    {
                        sprintf(sTmp, "%d-%d\n", i, n);
                    }
    buf += sTmp;
                    i=n;
                }
            }
        }    finish = clock(); puts(buf.c_str());    cout<<"字符串长度: "<<size<<endl;
        cout<<"耗时: "<<finish - start<<"毫秒"<<endl;    return 0;
    }
      

  7.   

    耗时不是算法的问题,是你输出占用了时间。你可以把cout去掉看看计算的时间,另外,5楼的用printf代替了cout节约了时间,换成cout也是一样的耗时!!#include "time.h"
    #include "iostream"
    using namespace std;int main()
    {
        string s= "";
        for(int i = 0; i < 1000; i ++)
        {
            s+="00011001110000010";
        }
        char ch = '1';
        clock_t start,finish;
        int size = s.size();    start = clock();
        int n = -1;
        bool bRecord = false;
        for ( i=0;i < size;i++)
        {
    if((char)s[i] != ch)
    {
    if(!bRecord)
    {
    if(i == n+1)
    {
         cout<<n+1<<endl;
    }
    else
    {
         cout<<n+1<<'-'<<i<<endl;
    }
    bRecord = true;
    }
    continue;
    }
    if(bRecord)
    {
    n = i;
    bRecord = false;
    }    }    finish = clock();
        cout<<"字符串长度: "<<size<<endl;
        cout<<"耗时: "<<finish - start<<"毫秒"<<endl;    
        return 0;
        
    }
      

  8.   


    #include <stdio.h>#define SIZE 17000
    #define N (SIZE-1)int main(void)
    {
    register v0 = 16, v1 = 10; puts("4-5\n8-10\n16"); while( v0 < N )
    {
    printf("%d-%d\n", (v0 += 5), (v1 += 12));
    printf("%d-%d\n", (v0 += 4), (v1 += 5));
    printf("%d\n", (v0 += 8));
    } return 0;
    }