简体   繁体   English

C ++代码优化

[英]C++ Code optimization

I have created my custom function to turn a wstring into lower case. 我创建了自定义函数,将wstring转换为小写。 However, it is pretty slow in DebugMode. 但是,它在DebugMode中非常慢。 Yes, I know ReleaseMode is what counts, but anyway it is pretty unnerving. 是的,我知道ReleaseMode很重要,但是无论如何它都令人不安。

wstring wstringToLower(wstring u)
{
    wstring s;

    for (int i=0;i<u.size();i++)
    {
        wstring sChar;
        sChar=u.substr(i,1);

        int iChar=static_cast<int>(sChar[0]);

        int iNewChar=charCodeToLower(iChar);

        wstring sNewChar=wstring(1,iNewChar);

        s.append(sNewChar);
    }

    return s;
}

Does anybody see anything obvious that I could improve to speed up the code, even in DebugMode? 是否有人看到即使在DebugMode下也可以改进以加快代码执行速度的明显方法?

Thank you! 谢谢!

There's no need to make temporary strings. 无需制作临时字符串。

So, for start, instead of: 因此,首先,而不是:

    wstring sNewChar=wstring(1,iNewChar);
    s.append(sNewChar);

This should do the trick: 这应该可以解决问题:

    s.push_back(iNewChar);

Then, instead of: 然后,代替:

    wstring sChar;
    sChar=u.substr(i,1);

    int iChar=static_cast<int>(sChar[0]);

This should work: 这应该工作:

    int iChar=static_cast<int>(u[i]);

And, of course, as noted by Marcel, you can do everything on the passed copy, avoiding the extra string allocation. 而且,当然,正如Marcel指出的那样,您可以对传递的副本执行所有操作,从而避免了额外的字符串分配。

Also, as noted in the comments: How to convert std::string to lower case? 另外,如注释中所述: 如何将std :: string转换为小写? . Also, read all answers (and comments) here: how to Make lower case letters for unicode characters : 另外,请在此处阅读所有答案(和评论): 如何为unicode字符设置小写字母

#include <algorithm>
#include <string>
#include <iostream>

using namespace std;


int main()
{

        ::setlocale(LC_ALL,"");
        std::wstring data = L"НЕМАЊА БОРИЋ"; // Wide chars
        std::transform(data.begin(), data.end(), data.begin(), ::towlower);

        // prints немања борић
        std::wcout << data << std::endl;

        return 0;
}

http://en.cppreference.com/w/cpp/string/wide/towlower http://en.cppreference.com/w/cpp/string/wide/towlower

First of all I would avoid to allocate memory for variables each run, since allocating is a heavy operation. 首先,我将避免为每次运行分配变量,因为分配是一项繁重的操作。

Then do not call u.size() in the for-loop declaration. 然后不要在for循环声明中调用u.size()。 It will be called every loop otherwise. 否则,将在每个循环中调用它。 Every function call less that you call in a loop is a good win for performance. 每个函数调用少于您在循环中调用的次数,对于性能而言是一个不错的选择。

Next everything Nemanja Boric said in the other answer. 接下来,Nemanja Boric在另一个答案中说的一切。

And since the variable u is passed as copy, you can use it as return value and operate directly on it. 由于变量u是作为副本传递的,因此您可以将其用作返回值并直接对其进行操作。

wstring wstringToLower(wstring u)
{
    int size = u.size();

    for (int i = 0; i < size; ++i)
    {
        u[i] = charCodeToLower(static_cast<int>(u[i]));
    }

    return u;
}

Conclusion: Basically avoid to allocate memory or calling functions in loops. 结论:基本上避免在循环中分配内存或调用函数。 Do just as much as you really have to. 尽力而为。

There is actually no need for the wstringToLower function at all. 实际上根本不需要wstringToLower函数。 You can use <algorithm> to do most of the work: 您可以使用<algorithm>来完成大部分工作:

std::wstring str = "Some String";
std::transform(str.begin(), str.end(), str.begin(), ::towlower);

If you are trying to localize it, you may want to modify it slightly: 如果您尝试对其进行本地化,则可能需要稍作修改:

std::wstring str = "Some String";
std::locale loc; // set your locale
std::transform(str.begin(), str.end(), str.begin(), [](wchar_t c)
{
    return use_facet<ctype<wchar_t>>(loc).tolower(c);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM