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 }
复制代码

输出结果:

在测试代码中,函数返回值是容器的执行速度比容器作为参数传递要慢的多。

可以看到返回容器的函数里,容器频繁的创建销毁。

容器作为参数传递是项目中常见做法,很少看到函数返回容器。原因就在此。

posted @   逆向人  阅读(2939)  评论(0编辑  收藏  举报
编辑推荐:
· .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 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示