Created
November 1, 2019 18:11
-
-
Save terngkub/7af346483498730ceba3bc3da4833c35 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <vector> | |
| #include <chrono> | |
| void copy_arg(std::vector<int> v) | |
| { | |
| (void)v; | |
| } | |
| void ref_arg(std::vector<int> const & v) | |
| { | |
| (void)v; | |
| } | |
| void move_arg(std::vector<int> && v) | |
| { | |
| (void)v; | |
| } | |
| void test_func_arg() | |
| { | |
| std::cout << "test function argument.\n"; | |
| std::vector<int> v(1000000); | |
| { | |
| auto start = std::chrono::system_clock::now(); | |
| copy_arg(v); | |
| auto end = std::chrono::system_clock::now(); | |
| auto duration = end - start; | |
| std::cout << "copy: " << duration.count() << '\n'; | |
| } | |
| { | |
| auto start = std::chrono::system_clock::now(); | |
| ref_arg(v); | |
| auto end = std::chrono::system_clock::now(); | |
| auto duration = end - start; | |
| std::cout << "ref: " << duration.count() << '\n'; | |
| } | |
| { | |
| auto start = std::chrono::system_clock::now(); | |
| move_arg(std::move(v)); | |
| auto end = std::chrono::system_clock::now(); | |
| auto duration = end - start; | |
| std::cout << "move: " << duration.count() << '\n'; | |
| } | |
| } | |
| std::vector<int> copy_return(int n) | |
| { | |
| std::vector<int> v(1000000, n); | |
| return v; | |
| } | |
| // shouldn't return reference if the object is created in the function because it will be out of scope when the function end | |
| std::vector<int> move_return(int n) | |
| { | |
| return std::vector<int>(1000000, n); | |
| } | |
| void test_func_return() | |
| { | |
| std::cout << "Test function return.\n"; | |
| { | |
| auto start = std::chrono::system_clock::now(); | |
| std::vector<int> v = move_return(rand()); | |
| auto end = std::chrono::system_clock::now(); | |
| auto duration = end - start; | |
| std::cout << "move: " << duration.count() << '\n'; | |
| } | |
| { | |
| auto start = std::chrono::system_clock::now(); | |
| std::vector<int> v = copy_return(rand()); | |
| auto end = std::chrono::system_clock::now(); | |
| auto duration = end - start; | |
| std::cout << "copy: " << duration.count() << '\n'; | |
| } | |
| { | |
| auto start = std::chrono::system_clock::now(); | |
| std::vector<int> v = move_return(rand()); | |
| auto end = std::chrono::system_clock::now(); | |
| auto duration = end - start; | |
| std::cout << "move: " << duration.count() << '\n'; | |
| } | |
| { | |
| auto start = std::chrono::system_clock::now(); | |
| std::vector<int> v = copy_return(rand()); | |
| auto end = std::chrono::system_clock::now(); | |
| auto duration = end - start; | |
| std::cout << "copy: " << duration.count() << '\n'; | |
| } | |
| } | |
| void test_container() | |
| { | |
| std::cout << "Test container copy vs move\n"; | |
| { | |
| std::vector<int> v1(1000000, rand()); | |
| auto start = std::chrono::system_clock::now(); | |
| std::vector<int> v2(v1); | |
| auto end = std::chrono::system_clock::now(); | |
| auto duration = end - start; | |
| std::cout << "copy: " << duration.count() << '\n'; | |
| std::cout << "check: " << v1[0] << "," << v2[0] << '\n'; | |
| } | |
| { | |
| std::vector<int> v1(1000000, rand()); | |
| auto start = std::chrono::system_clock::now(); | |
| std::vector<int> v2(std::move(v1)); | |
| auto end = std::chrono::system_clock::now(); | |
| auto duration = end - start; | |
| std::cout << "move: " << duration.count() << '\n'; | |
| // data in v1 are moved to v2 so when we print v1 it will be segfault | |
| std::cout << "check: " << v1[0] << "," << v2[0] << '\n'; | |
| } | |
| } | |
| int main() | |
| { | |
| // only need to read the data, use const ref | |
| // write data and also want the outside to change, use ref | |
| // write data and don't need that data outside, use move | |
| // need a separated copy, use copy | |
| test_func_arg(); | |
| // Don't worry about the return | |
| // value created and returned from the function will have Return Value Optimization (RVO), which mean the result will be returned with move and not copy. | |
| test_func_return(); | |
| // move is a lot faster than copy but you can't access the source (because it's moved). | |
| test_container(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment