Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 字符串的快速多重替换_C++_String_Boost_Replace - Fatal编程技术网

C++ 字符串的快速多重替换

C++ 字符串的快速多重替换,c++,string,boost,replace,C++,String,Boost,Replace,我有一个字符串,如下所示: {A}jahshs{b}jwuw{c}wuqjwhaha{d}{e}{f}jsj{g} 我需要用不同的字符串替换每个{x}。这个问题的出现是因为这个过程将以1000次/秒的速度重复,所以我需要一个优化/快速的方法来完成 有什么想法吗?推进替换?Boost格式?等等 预先分配所有缓冲区 利润 哦,不要发垃圾邮件。示例代码在5到10分钟内完成 好的,下面是:还有 #include <string> #include <sstream> #inc

我有一个字符串,如下所示:

{A}jahshs{b}jwuw{c}wuqjwhaha{d}{e}{f}jsj{g}
我需要用不同的字符串替换每个
{x}
。这个问题的出现是因为这个过程将以1000次/秒的速度重复,所以我需要一个优化/快速的方法来完成

有什么想法吗?推进替换?Boost格式?等等

  • 预先分配所有缓冲区

  • 利润

  • 哦,不要发垃圾邮件。示例代码在5到10分钟内完成

    好的,下面是:还有

    #include <string>
    #include <sstream>
    #include <boost/utility/string_ref.hpp>
    
    template <typename Range>
    int expand(Range const& /*key*/)
    {
        return rand()%42; // todo lookup value with key (be sure to stay lean here)
    }
    
    #include <iostream>
    int main()
    {
        static const std::string msg_template = "{A}jahshs{b}jwuw{c}wuqjwhaha{d}{e}{f}jsj{g}\n";
    
        std::ostringstream builder;
        builder.str().reserve(1024); // reserve ample room, not crucial since we reuse it anyways
    
        for (size_t iterations = 1ul << 14; iterations; --iterations)
        {
            builder.str("");
            std::ostreambuf_iterator<char> out(builder);
    
            for(auto f(msg_template.begin()), l(msg_template.end()); f != l;)
            {
                switch(*f)
                {
                    case '{' : 
                        {
                            auto s = ++f;
                            size_t n = 0;
    
                            while (f!=l && *f != '}')
                                ++f, ++n;
    
                            // key is [s,f] now
                            builder << expand(boost::string_ref(&*s, n));
    
                            if (f!=l)
                                ++f; // skip '}'
                        }
                        break;
                    default:
                        *out++ = *f++;
                }
            }
            // to make it slow, uncomment:
            // std::cout << builder.str();
        }
    }
    
    #包括
    #包括
    #包括
    模板
    整数扩展(范围常数&/*键*/)
    {
    return rand()%42;//带键的todo查找值(请确保此处保持精简)
    }
    #包括
    int main()
    {
    static const std::string msg_template=“{A}jahshs{b}jwuw{c}wuqjwhaha{d}{e}{f}jsj{g}\n”;
    标准::ostringstream builder;
    builder.str().reserve(1024);//保留足够的空间,这并不重要,因为我们无论如何都会重用它
    
    对于(size_t iterations=1ul
    std::string::replace
    ,测量并证明它不够快?但是我应该为每个{x}调用replace在字符串中,大约10。所以10x1000每秒替换。没有任何东西可以替代在您端进行测试和测量。变量太多。如果您编写一些代码,但速度仍然比您预期的慢,我们至少可以查看您的代码并讨论。每个{x}是一个宏名称,要替换的内容可能会有所不同,除了主字符串也可能会有所不同。因此,是一种宏扩展器。是吗?您运行了代码吗?有什么您不明白的吗?是否使用了
    expand
    函数?正如您所注意的,它“是一种宏扩展器”。我加倍确保您可以返回任何可以进行IO流处理的内容。可能是这样吗?这可以提高效率。一个模仿mailmerge的示例:哦,和(“除了主字符串也可以变化”)。最后但并非最不重要的一点:我用Boost Spirit实现了一个更通用的宏扩展系统(即嵌套宏、转义宏字符、递归求值)Cheers固定了基准。现在在我的盒子上进行4 Mio/s扩展,