#include <iostream>
#include <string>
#include <cstring>
const int size = 100000000;
static char s_str[size];
std::string test1()
{
return std::string(s_str);
}
void test2(std::string& s)
{
s = std::string(s_str);
}
int main()
{
memset(s_str, 'x', size - 1);
std::string s(s_str);
clock_t c1, c2, c3, c4, c5, c6;
// コピー
c1 = clock();
for (int i = 0; i < 10; i++) {
s = static_cast<const std::string&>(test1());
}
c2 = clock();
// ムーブ
c3 = clock();
for (int i = 0; i < 10; i++) {
s = test1();
}
c4 = clock();
// バッファの再利用
c5 = clock();
for (int i = 0; i < 10; i++) {
test2(s);
}
c6 = clock();
printf("コピー : %d\n", static_cast<int>(c2 - c1));
printf("ムーブ : %d\n", static_cast<int>(c4 - c3));
printf("再利用 : %d\n", static_cast<int>(c6 - c5));
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8Y3N0cmluZz4KIApjb25zdCBpbnQgc2l6ZSA9IDEwMDAwMDAwMDsKIApzdGF0aWMgY2hhciBzX3N0cltzaXplXTsKIApzdGQ6OnN0cmluZyB0ZXN0MSgpCnsKICAgIHJldHVybiBzdGQ6OnN0cmluZyhzX3N0cik7Cn0KIAp2b2lkIHRlc3QyKHN0ZDo6c3RyaW5nJiBzKQp7CiAgICBzID0gc3RkOjpzdHJpbmcoc19zdHIpOwp9CiAKaW50IG1haW4oKQp7CiAgICBtZW1zZXQoc19zdHIsICd4Jywgc2l6ZSAtIDEpOwogCiAgICBzdGQ6OnN0cmluZyBzKHNfc3RyKTsKICAgIGNsb2NrX3QgYzEsIGMyLCBjMywgYzQsIGM1LCBjNjsKIAogICAgLy8g44Kz44OU44O8CiAgICBjMSA9IGNsb2NrKCk7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IDEwOyBpKyspIHsKICAgICAgICBzID0gc3RhdGljX2Nhc3Q8Y29uc3Qgc3RkOjpzdHJpbmcmPih0ZXN0MSgpKTsKICAgIH0KICAgIGMyID0gY2xvY2soKTsKIAogICAgLy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