STL中的容器作为返回值
分别以函数返回值方式和参数传引用方式测试了vector、map两种容器,代码如下:
1 // testContainer.cpp : Defines the entry point for the console application. 2 // 3 4 #include "stdafx.h" 5 #include <iostream> 6 #include <vector> 7 #include <map> 8 #include <chrono> 9 using namespace std; 10 vector<int> funcVec1(){ 11 vector<int >vec; 12 for (int i = 0; i < 10; ++i){ 13 vec.push_back(i); 14 } 15 return vec; 16 } 17 18 void funcVec2(vector<int>&vec){ 19 for (int i = 0; i < 10; ++i){ 20 vec.push_back(i); 21 } 22 return; 23 } 24 25 map<int, int>funcMap1(){ 26 map<int, int>tmpMap; 27 for (int i = 0; i < 10; ++i){ 28 tmpMap[i] = i; 29 } 30 return tmpMap; 31 } 32 33 void funcMap2(map<int, int>&tmpMap){ 34 for (int i = 0; i < 10; ++i){ 35 tmpMap[i] = i; 36 } 37 } 38 39 int _tmain(int argc, _TCHAR* argv[]) 40 { 41 using namespace std::chrono; 42 steady_clock::time_point t1 = system_clock::now(); 43 for (int i = 0; i < 100000; ++i){ 44 vector<int> vec1 = funcVec1(); 45 } 46 auto t2 = std::chrono::system_clock::now(); 47 //count输出时钟周期 48 cout << "return vec takes " << (t2 - t1).count()<<" tick count" << endl; 49 // duration_cast在chrono中,可以将时钟数转换为相应的时间间隔 50 cout << duration_cast<nanoseconds>(t2 - t1).count() << " nanoseconds" << endl; 51 cout << duration_cast<microseconds>(t2 - t1).count() << " microseconds" << endl; 52 cout << duration_cast<milliseconds>(t2 - t1).count() << " milliseconds" << endl; 53 cout << duration_cast<seconds>(t2 - t1).count() << " seconds" << endl; 54 cout << " --------------------------------" << endl; 55 vector<int> vec2; 56 for (int i = 0; i < 100000; ++i){ 57 funcVec2(vec2); 58 } 59 auto t3 = system_clock::now(); 60 cout << "reference vec takes " << (t3 - t2).count() << " tick count" << endl; 61 cout << duration_cast<milliseconds>(t3 - t2).count() << " milliseconds" << endl; 62 cout << " --------------------------------" << endl; 63 for (int i = 0; i < 100000; ++i){ 64 map<int,int> tmpMap1 = funcMap1(); 65 } 66 auto t4 = system_clock::now(); 67 cout << "return map takes " << (t4 - t3).count() << " tick count" << endl; 68 cout << duration_cast<milliseconds>(t4 - t3).count() << " milliseconds" << endl; 69 cout << " --------------------------------" << endl; 70 map<int, int>tmpMap2; 71 for (int i = 0; i < 100000; ++i){ 72 funcMap2(tmpMap2); 73 } 74 auto t5 = system_clock::now(); 75 cout << "reference map takes " << (t5 - t4).count() << " tick count" << endl; 76 cout << duration_cast<milliseconds>(t5 - t4).count() << " milliseconds" << endl; 77 return 0; 78 }
输出结果:
在测试代码中,函数返回值是容器的执行速度比容器作为参数传递要慢的多。
可以看到返回容器的函数里,容器频繁的创建销毁。
容器作为参数传递是项目中常见做法,很少看到函数返回容器。原因就在此。
作者:逆向人 公众号:逆向人 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 如果文中有什么错误,欢迎指出。以免更多的人被误导。 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?