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

// 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转换效率要比 mbstowcswcstombs高。

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


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值