逝水流年

This is a blog to record my life, my work, my feeling …

Std::string 与 Std::wstring 转换方法的效率比较

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Calls the provided work function and returns the number of milliseconds
// that it takes to call that function.
template <class Function>;
__int64 time_call(Function&& f)
{
    __int64 begin = GetTickCount64();
    f();
    return GetTickCount64() - begin;
}
// Use WideCharToMultiByte and MultiByteToWideChar
string WChar2Ansi(const wchar_t* pwszSrc)
{
    int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);
    if (nLen<= 0) return string("");
    char* pszDst = new char[nLen];
    if (NULL == pszDst) return string("");
    WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);
    pszDst[nLen -1] = 0;
    string strTemp(pszDst);
    delete [] pszDst;
    return strTemp;
}
wstring Ansi2WChar(const char* pszSrc, int nLen)
{
    int nSize = MultiByteToWideChar(CP_ACP, 0, pszSrc, nLen, 0, 0);
    if(nSize <= 0) return NULL;
    wchar_t *pwszDst = new wchar_t[nSize+1];
    if( NULL == pwszDst) return NULL;
    MultiByteToWideChar(CP_ACP, 0, pszSrc, nLen, pwszDst, nSize);
    pwszDst[nSize] = 0;
    if( pwszDst[0] == 0xFEFF)                    // skip Oxfeff
        for(int i = 0; i < nSize; i ++)
            pwszDst[i] = pwszDst[i+1];
    wstring wcharString(pwszDst);
    delete pwszDst;
    return wcharString;
}
// Use wcstombs_s and mbstowcs_s
string ws2s(wstring ws)
{
    const wchar_t* Source = ws.c_str();
    size_t size = 2 * ws.size() + 1;
    char* Dest = new char[size];
    memset(Dest,0, size);
    size_t len = 0;
    wcstombs(Dest, Source, size);
    //wcstombs_s(&len, Dest, size, Source, size);
    string result = Dest;
    delete [] Dest;
    return result;
}
wstring s2ws(string s)
{
    const char* Source = s.c_str();
    size_t size = s.size() + 1;
    wchar_t* Dest = new wchar_t[size];
    wmemset(Dest, 0, size);
    size_t len = 0;
    mbstowcs(Dest, Source, size);
    //mbstowcs_s(&len, Dest, size, Source, size);
    wstring result = Dest;
    delete [] Dest;
    return result;
}
// Testing
    __int64 elapsed;
    string input = "Hello, World.\\XXX\\XXX";
    wstring winput = L"Hello, World.\\XXX\\XXX";
    wchar_t dest[1000];
    int count = 10000;
    elapsed = time_call([input, &count]
    {
        while (count)
        {
            Ansi2WChar(input.c_str(), input.length());
            count--;
        }
    });
    memset(dest, 0, 1000);
    _itow_s(elapsed, dest, 10);
    OutputDebugStringW(L"Time:");
    OutputDebugStringW(dest);
    OutputDebugStringW(L"ms\n");
    count = 10000;
    elapsed = 0;
    elapsed = time_call([input, &count]
    {
        while (count)
        {
            s2ws(input);
            count--;
        }
    });
    memset(dest, 0, 1000);
    _itow_s(elapsed, dest, 10);
    OutputDebugStringW(L"Time:");
    OutputDebugStringW(dest);
    OutputDebugStringW(L"ms\n");
    count = 10000;
    elapsed = 0;
    elapsed = time_call([winput, &count]
    {
        while (count)
        {
            WChar2Ansi(winput.c_str());
            count--;
        }
    });
    memset(dest, 0, 1000);
    _itow_s(elapsed, dest, 10);
    OutputDebugStringW(L"Time:");
    OutputDebugStringW(dest);
    OutputDebugStringW(L"ms\n");
    count = 10000;
    elapsed = 0;
    elapsed = time_call([winput, &count]
    {
        while (count)
        {
            ws2s(winput);
            count--;
        }
    });
    memset(dest, 0, 1000);
    _itow_s(elapsed, dest, 10);
    OutputDebugStringW(L"Time:");
    OutputDebugStringW(dest);
    OutputDebugStringW(L"ms\n");

输出:

Time:78ms

Time:94ms

Time:62ms

Time:109ms

从输出结果可以看出

WideCharToMultiByte和MultiByteToWideChar转换效率要比mbstowcs和wcstombs高。

注意:如果使用mbstowcs则需要Disable Specific Warnings:4996

Comments