Skip to content

Instantly share code, notes, and snippets.

@karimnaaji
Created March 11, 2016 21:23
Show Gist options
  • Save karimnaaji/d58afb771de5edfc98a1 to your computer and use it in GitHub Desktop.
Save karimnaaji/d58afb771de5edfc98a1 to your computer and use it in GitHub Desktop.
string move vs string copy
#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