迷雾少年 发表于 2015-10-3 17:40:34

【原创】去除字符串空白的算法及效率比较

本帖最后由 迷雾少年 于 2015-10-3 17:51 编辑

第一种:
#include <iostream>
#include <string.h>
#include <WinSock2.h>
#include <tchar.h>
#include <windows.h>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
int main(void) {

        char sz[] = "w w w b b s f is h c c o m";
        char *p = sz;
        int len = strlen(p);
        cout << p << endl;

        LARGE_INTEGER t1, t2, tc;
        QueryPerformanceFrequency(&tc);

        QueryPerformanceCounter(&t1);
        /*Clear Space*/
        while (*p) {
                if ((' ' == *p))//||( '1'==*p)
                        lstrcpy(p, p + 1);

                if ((' ' != *p)) // || ('1' == *p)
                        p++;
        }
        /*Clear Space*/
        QueryPerformanceCounter(&t2);


        cout << sz << "Time:"<<t2.QuadPart-t1.QuadPart<<"纳秒"<<endl;

       

        system("Pause");

        return EXIT_SUCCESS;
}




第二种:#include <iostream>
#include <string.h>
#include <WinSock2.h>
#include <tchar.h>
#include <windows.h>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
int main(void) {

        char sz[] = "w w w b b s f is h c c o m";
        char *p = sz;
        int len = strlen(p);
        cout << p << endl;

        LARGE_INTEGER t1, t2, tc;
        QueryPerformanceFrequency(&tc);

        QueryPerformanceCounter(&t1);

        /*Clear Space*/

        char *space; //指向 空白字符
        char *pstrc = sz;

        while (*pstrc) {
                if (' ' == *pstrc) {                //如果发现空白
                        space = pstrc;                        //指向空白处

                        while (' ' == *++space);   //找到非空白字符

                        if (0 == *space) break;

                        /* 交换空白处和非空白处 */
                        *space += *pstrc;
                        *pstrc = *space - *pstrc;
                        *space = *space - *pstrc;
                }

                pstrc++;

        }
        /* 最后一步!!!结束0 */
//        pstrc = sz;
//        while (' ' != *pstrc++);
        *pstrc = 0;

        /*Clear Space*/
        QueryPerformanceCounter(&t2);


        cout << sz << "Time:"<<t2.QuadPart-t1.QuadPart<<"纳秒"<<endl;

       

        system("Pause");

        return EXIT_SUCCESS;
}

测来测去都平均 2纳秒。。。


第三种不是我原创的一好友写的{:9_240:}
#include <iostream>
#include <string.h>
#include <WinSock2.h>
#include <tchar.h>
#include <windows.h>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
int sync(char *sz);
int main(void) {

        char sz[] = "w w w b b s f is h c c o m";
        char *p = sz;
        int len = strlen(p);
        cout << p << endl;

        LARGE_INTEGER t1, t2, tc;
        QueryPerformanceFrequency(&tc);

        QueryPerformanceCounter(&t1);

        /*Clear Space*/

        sync(sz);

        /*Clear Space*/
        QueryPerformanceCounter(&t2);


        cout << sz << "Time:"<<t2.QuadPart-t1.QuadPart<<"纳秒"<<endl;

       

        system("Pause");

        return EXIT_SUCCESS;
}
int sync(char *sz)
{


        //tp为目的指针,fp为源指针 必须保证 tp指向从左往右第一个空白 fp指向从左往右第一个非空白
        char *tp, *sp;
        tp = sp = sz;
        bool isok = true;//是否同步模式
                                       //初始化tp
        while (*tp&&*tp != ' ') tp++;
        sp = tp + 1;//初始化sp
        if (*tp == 0 || *sp == '\0') return 1;//字符串太短

        while (*sp) //由于 sp在tp右边或者指向第一个位置 所以以此判断是否结束
        {

                if (isok)
                {
                        //如果同步模式下且指向的是空白则代表一块移动完成,退出同步模式,标志归位
                        if (*sp == ' ') isok = false;
                        else
                        {
                                //否则则移动递增
                                *tp = *sp;
                                tp++; sp++;
                        }
                }
                else if (*sp != ' ')
                {
                        //如果不是同步模式 ,sp指向非空白则sp就绪,进入同步模式
                        isok = true;
                }
                else sp++;//如果不是同步模式且sp指向空白代表没有找到块,继续寻找
        }
        *tp = 0;//添加末尾标识
        return 0;
}




以上代码 VS2015 IDE 测试通过   

ps:如果大家有更效率的算法 可以发来交流交流。。。

迷雾少年 发表于 2015-10-3 17:45:19

忘记说了    上面是在Release模式测试 的

迷雾少年 发表于 2015-10-3 17:56:55

我去   第二种打多了多余的东西了。。
直接赋值不交换就可以了。。。
                        /* 交换空白处和非空白处 */
                        //*space += *pstrc;
                        //*pstrc = *space - *pstrc;
                        //*space = *space - *pstrc;
                        *pstrc = *space;
                        *space = ' ';

迷雾少年 发表于 2015-10-3 18:07:27

上面是微秒 不是纳秒忘记改了{:9_240:}

康小泡 发表于 2015-10-3 19:24:47

不错啊。
页: [1]
查看完整版本: 【原创】去除字符串空白的算法及效率比较