分别以函数返回值方式和参数传引用方式测试了vector、map两种容器,代码如下:

 // testContainer.cpp : Defines the entry point for the console application.
// #include "stdafx.h"
#include <iostream>
#include <vector>
#include <map>
#include <chrono>
using namespace std;
vector<int> funcVec1(){
vector<int >vec;
for (int i = ; i < ; ++i){
vec.push_back(i);
}
return vec;
} void funcVec2(vector<int>&vec){
for (int i = ; i < ; ++i){
vec.push_back(i);
}
return;
} map<int, int>funcMap1(){
map<int, int>tmpMap;
for (int i = ; i < ; ++i){
tmpMap[i] = i;
}
return tmpMap;
} void funcMap2(map<int, int>&tmpMap){
for (int i = ; i < ; ++i){
tmpMap[i] = i;
}
} int _tmain(int argc, _TCHAR* argv[])
{
using namespace std::chrono;
steady_clock::time_point t1 = system_clock::now();
for (int i = ; i < ; ++i){
vector<int> vec1 = funcVec1();
}
auto t2 = std::chrono::system_clock::now();
//count输出时钟周期
cout << "return vec takes " << (t2 - t1).count()<<" tick count" << endl;
// duration_cast在chrono中,可以将时钟数转换为相应的时间间隔
cout << duration_cast<nanoseconds>(t2 - t1).count() << " nanoseconds" << endl;
cout << duration_cast<microseconds>(t2 - t1).count() << " microseconds" << endl;
cout << duration_cast<milliseconds>(t2 - t1).count() << " milliseconds" << endl;
cout << duration_cast<seconds>(t2 - t1).count() << " seconds" << endl;
cout << " --------------------------------" << endl;
vector<int> vec2;
for (int i = ; i < ; ++i){
funcVec2(vec2);
}
auto t3 = system_clock::now();
cout << "reference vec takes " << (t3 - t2).count() << " tick count" << endl;
cout << duration_cast<milliseconds>(t3 - t2).count() << " milliseconds" << endl;
cout << " --------------------------------" << endl;
for (int i = ; i < ; ++i){
map<int,int> tmpMap1 = funcMap1();
}
auto t4 = system_clock::now();
cout << "return map takes " << (t4 - t3).count() << " tick count" << endl;
cout << duration_cast<milliseconds>(t4 - t3).count() << " milliseconds" << endl;
cout << " --------------------------------" << endl;
map<int, int>tmpMap2;
for (int i = ; i < ; ++i){
funcMap2(tmpMap2);
}
auto t5 = system_clock::now();
cout << "reference map takes " << (t5 - t4).count() << " tick count" << endl;
cout << duration_cast<milliseconds>(t5 - t4).count() << " milliseconds" << endl;
return ;
}

输出结果:

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

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

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

STL中的容器作为返回值的更多相关文章

  1. STL中的容器

    STL中的容器 一. 种类: 标准STL序列容器:vector.string.deque和list. 标准STL关联容器:set.multiset.map和multimap. 非标准序列容器slist ...

  2. C++ STL 中 map 容器

    C++ STL 中 map 容器 Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处理能力,由于这个特性,它 ...

  3. STL中的容器介绍

    STL中的容器主要包括序列容器.关联容器.无序关联容器等. 一]序列容器 (1) vector vector 是数组的一种类表示,提供自动管理内存的功能,除非其他类型容器有更好满足程序的要求,否则,我 ...

  4. [C++]STL中的容器

    C++11 STL中的容器 一.顺序容器: vector:可变大小数组: deque:双端队列: list:双向链表: forward_list:单向链表: array:固定大小数组: string: ...

  5. 慕课网-Java入门第一季-7-3 Java 中无参带返回值方法的使用

    来源:http://www.imooc.com/code/1579 如果方法不包含参数,但有返回值,我们称为无参带返回值的方法. 例如:下面的代码,定义了一个方法名为 calSum ,无参数,但返回值 ...

  6. Java 中无参带返回值方法的使用

    如果方法不包含参数,但有返回值,我们称为无参带返回值的方法. 例如:下面的代码,定义了一个方法名为 calSum ,无参数,但返回值为 int 类型的方法,执行的操作为计算两数之和,并返回结果 在 c ...

  7. python中os.system()的返回值

    [python中os.system()的返回值] 如果第三方程序返回的是布尔型返回值,os.system会将true转为1,false转为0进行返回. 问题: /bin/xxx.py是一个返回码为1的 ...

  8. 算法求解中的变量、数组与数据结构(STL 中的容器)

    本质上算法都是对数据的操作,没有数据,没有存储数据的容器和组织方式,算法就是无源之水无本之木,就是巧妇也难为无米之炊.算法是演员,变量.数组.容器等就是舞台, 然后整个算法的处理流程,都是针对这些数据 ...

  9. C++ STL中常见容器的时间复杂度和比较和分析

    C++ STL中常见容器的时间复杂度 map, set, multimap, and multiset 上述四种容器采用红黑树实现,红黑树是平衡二叉树的一种.不同操作的时间复杂度近似为: 插入: O( ...

随机推荐

  1. cv2的安装

    第一种 ,直接尝试 pip install cv2 ,大可能报错. 第二种,pip install opencv-python ,大概率 直接成功. 第三种 ,去网上下包 放到 sit_package ...

  2. leetcode448

    public class Solution { public IList<int> FindDisappearedNumbers(int[] nums) { Dictionary<i ...

  3. 多进程模块 multiprocessing

    由于GIL的存在,python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程. multiprocessing包是Python中的多进程 ...

  4. Mybatis的mapper文件中#和$的区别 以及 resultType和resultMap的区别

    一般#{}用于传递查询的参数,一般用于从dao层传递一个string或者其他的参数过来,mybatis对这个参数会进行加引号的操作,将参数转变为一个字符串. SELECT * FROM employe ...

  5. Android中五大字符串总结(String、StringBuffer、StringBuilder、Spanna

    https://www.aliyun.com/jiaocheng/2861.html?spm=5176.100033.1.35.2ed56b03CbsYFK 摘要:String.StringBuffe ...

  6. 计算macd与ma技术指标

    MACD部分 转载自云金杞:https://blog.csdn.net/qq_26948675/article/details/72636590 数据获取于聚宽平台 import pandas as ...

  7. 求树的直径+并查集(bfs,dfs都可以)hdu4514

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 这题主要是叫我们求出树的直径,在求树的直径之前要先判断一下有没有环 树的直径指的就是一棵树上面距 ...

  8. fabric 在阿里云Ubuntu部署 注意

    部署时候报 段错误: signal SIGSEGV: segmentation violation code=0x1 addr=0x63 pc=0x7fcd47490259] 解决方案: 更新Hype ...

  9. [leetcode]170. Two Sum III - Data structure design两数之和III - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  10. Retrofit 2.0基于OKHttp更高效更快的网络框架 以及自定义转换器

    时间关系,本文就 Retrofit 2.0的简单使用 做讲解  至于原理以后有空再去分析 项目全面.简单.易懂  地址: 关于Retrofit 2.0的简单使用如下:  https://gitee.c ...