Created
March 11, 2016 21:23
-
-
Save karimnaaji/d58afb771de5edfc98a1 to your computer and use it in GitHub Desktop.
string move vs string copy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <ctime> | |
#include <string> | |
void function_a(const std::string& str) { | |
return; | |
} | |
void function_b(std::string str) { | |
return; | |
} | |
int main() { | |
std::string str = "a really not so long string"; | |
clock_t start, end; | |
start = clock(); | |
for (int i = 0; i < 1e6; ++i) { | |
function_a(str); | |
} | |
end = clock(); | |
std::cout << "function_a: " << float(end - start) / CLOCKS_PER_SEC * 1000.f << "ms" << std::endl; | |
start = clock(); | |
for (int i = 0; i < 1e6; ++i) { | |
function_b(str); | |
} | |
end = clock(); | |
std::cout << "function_b: " << float(end - start) / CLOCKS_PER_SEC * 1000.f << "ms" << std::endl; | |
start = clock(); | |
for (int i = 0; i < 1e6; ++i) { | |
function_b(std::move(str)); | |
} | |
end = clock(); | |
std::cout << "function_b-move: " << float(end - start) / CLOCKS_PER_SEC * 1000.f << "ms" << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment