#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 = 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));
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8Y3N0cmluZz4KCmNvbnN0IGludCBzaXplID0gMTAwMDAwMDAwOwoKc3RhdGljIGNoYXIgc19zdHJbc2l6ZV07CgpzdGQ6OnN0cmluZyB0ZXN0MSgpCnsKICAgIHJldHVybiBzdGQ6OnN0cmluZyhzX3N0cik7Cn0KCnZvaWQgdGVzdDIoc3RkOjpzdHJpbmcmIHMpCnsKICAgIHMgPSBzX3N0cjsKfQoKaW50IG1haW4oKQp7CiAgICBtZW1zZXQoc19zdHIsICd4Jywgc2l6ZSAtIDEpOwoKICAgIHN0ZDo6c3RyaW5nIHMoc19zdHIpOwogICAgY2xvY2tfdCBjMSwgYzIsIGMzLCBjNCwgYzUsIGM2OwoKICAgIC8vIOOCs+ODlOODvAogICAgYzEgPSBjbG9jaygpOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCAxMDsgaSsrKSB7CiAgICAgICAgcyA9IHN0YXRpY19jYXN0PGNvbnN0IHN0ZDo6c3RyaW5nJj4odGVzdDEoKSk7CiAgICB9CiAgICBjMiA9IGNsb2NrKCk7CgogICAgLy8g44Og44O844OWCiAgICBjMyA9IGNsb2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