Reputation: 19
std::string test = "small_string";
std::string test2_1 = test; // line 1
std::string test2_2 = std::move(test); // line 2
hi, i am curious about which version of string create is fast. test2_1
or test2_2
?
I was looking into the implement on VC++ version of std::string
, found out that test2_1
version will call memcpy(_First1, _First2, _Count))
and test2_2
will call memmove(_First1, _First2, _Count))
, and I just wonder which one is better if I want my code run faster.
==============================================================
hi, i just do some test, and there has something that i can't understand...here is my test code:
{
Timer t;
for (int i = 0; i < 1000000; i++){
char s[] = "small_string";
char t[10];
memcpy(&t, &s, 10);
//std::cout << t << std::endl;
}
std::cout << "test, memcpy: " << t.elapsed() << " second" << std::endl;
}
{
Timer t;
for (int i = 0; i < 1000000; i++){
char s[] = "small_string";
char t[10];
memmove(&t, &s, 10);
// std::cout << t << std::endl;
}
std::cout << "test, memmove: " << t.elapsed() << " second" << std::endl;
}
{
Timer t;
for (int i = 0; i < 1000000; i++){
std::string test = "small_string";
std::string test2_1 = test; // line 1
}
std::cout << "test, str copy: " << t.elapsed() << " second" << std::endl;
}
{
Timer t;
for (int i = 0; i < 1000000; i++){
std::string test = "small_string";
std::string test2_2 = std::move(test); // line 2
}
std::cout << "test, str move: " << t.elapsed() << " second" << std::endl;
}
and result is:(debug build)
test, memcpy: 0.0090005 second
test, memmove: 0.0110006 second
test, str copy: 4.92528 second
test, str move: 4.52926 second
I know memcpy should faster than memmove, and my first two test case prove that. But the result of std::string is different...move version is faster than copy version. I didn't know why even looking into implementation of std::string, i didn't find anything that can convince me that this should happen.
=======================================================
result:(release build)
test, memcpy: 0 second
test, memmove: 0.0080004 second
test, str copy: 0.0330019 second
test, str move: 0.0290017 second
Upvotes: 1
Views: 1133
Reputation: 171403
hi, i am curious about which version of string create is fast. test2_1 or test2_2?
This seems like a clear example of premature optimization. The two forms aren't even equivalent, so asking which is faster is ridiculous.
Write code that does the right thing, and only worry about silly micro-optimizations if there's a performance problem.
Line 1 leaves test
unchanged, so it keeps the same value. Line 2 modifies test
leaving it in an unspecified state.
If your code doesn't depend on the value of test
after creating the other string, then you should use std::move
because it expresses the desired semantics (and if the string doesn't fit in the small-string buffer it will definitely be faster).
If your code cares about the value of test
afterwards then do not use std::move
, just do a copy instead.
Benchmarking the difference between apples and oranges is a waste of time.
Benchmarking tiny differences in a debug build is a massive waste of time. If you care about performance you need to enable optimizations.
Upvotes: 1