zoukankan      html  css  js  c++  java
  • c++ std string reserve 测试

    #include "windows.h"
    #include <tchar.h>
    #include <cstdio>
    #include <string>
    #include <limits>
    
    using namespace std;
    
    /*
    //z 2014-04-10 11:54:59 BG57IV3@XCL T1688293017.K.F1288753923[T13,L274,R5,V285]
    输出结果,观察数据可见其策略。
    
    可见一般情况下实际 reserve 的 capacity 比 申请的要大,当大于某个数值之后,变成逐个增加了,而且由于申请的内存巨大,非常消耗资源。
    
    15
    31
    47
    70
    105
    157
    235
    352
    528
    792
    1188
    1782
    2673
    4009
    6013
    9019
    13528
    20292
    30438
    45657
    68485
    102727
    154090
    231135
    346702
    520053
    780079
    1170118
    1755177
    2632765
    3949147
    5923720
    8885580
    13328370
    19992555
    29988832
    44983248
    67474872
    101212308
    151818462
    227727693
    341591539
    512387308
    512387309
    768580963
    768580964
    768580965
    768580966
    768580967
    768580968
    768580969
    768580970
    */
    void Test_String_Reserve()
    {
    	string str;
    	TCHAR buff[16] = {''};
    	size_t nCapacity = str.capacity();
    
    	int intMax = (std::numeric_limits<int>::max)();
    	for (int i = 1 ; i < intMax ;++i)
    	{
    		str.reserve(i);
    		if(nCapacity!=str.capacity())
    		{
    			_stprintf(buff,_T("%d"),nCapacity);
    			OutputDebugString(buff);
    			nCapacity = str.capacity();
    		}
    	}
    
    	_stprintf(buff,_T("%d"),nCapacity);
    	OutputDebugString(buff);
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	Test_String_Reserve();
    
    	return 0;
    }
    

    //z 2014-04-10 11:54:59 BG57IV3@XCL T1688293017.K.F1288753923[T13,L274,R5,V285]

    /*
    输出:
    从大往小reserve,可见一般一直保留不变,直到n小于等于15。由于采取了优化的措施(通常保留 capacity 不变),这部分运行较快。
    //z 2014-04-10 12:05:54 BG57IV3@XCL T3063169329.K.F1288753923[T14,L328,R5,V286]
    768580975
    15
    */
    void Test_String_Reserve_2()
    {
    	string str;
    	const int nReserve = 768580964;
    	str.reserve(nReserve);
    	TCHAR buff[16] = {''};
    	size_t nCapacity = str.capacity();
    
    	//int intMax = (std::numeric_limits<int>::max)();
    	for (int i = nReserve ; i > 0 ;i-=15)
    	{
    		str.reserve(i);
    		if(nCapacity!=str.capacity())
    		{
    			_stprintf(buff,_T("%d"),nCapacity);
    			OutputDebugString(buff);
    			nCapacity = str.capacity();
    		}
    	}
    
    	_stprintf(buff,_T("%d"),nCapacity);
    	OutputDebugString(buff);
    }

    //z 2014-04-10 13:47:04 BG57IV3@XCL T2734643987.K.F1288753923[T17,L426,R8,V350]
    /*
    输出:
    win7 x64 (visual studio 2005 | win32 build)
    4294967294 //z maxsize
    4294967295 //z unsigned int::max value
    */
    void GetStringMaxSize()
    {
    	TCHAR buff[16] = {''};
    	string str;
    	size_t nMaxSize = str.max_size();
    
    	_stprintf(buff,_T("%u"),nMaxSize);
    	OutputDebugString(buff);
    
    	unsigned int nMax = (std::numeric_limits<unsigned int>::max)();
    	_stprintf(buff,_T("%u"),nMax);
    	OutputDebugString(buff);
    }



    @IS2120#CNBLOGS.T2169364049[T1,L65,R1,V259]:备忘
    $ € ₤ ₭ ₪ ₩ ₮ ₦ ₱ ฿ ₡ ₫ ﷼ ¥ ﷼ ₫ ₡ ฿ ₱ ₦ ₮ ₩ ₪ ₭ ₤ € $
  • 相关阅读:
    大型电商业务架构 IT大咖说
    携程开源配置中心Apollo的设计与实现 IT大咖说
    李善友《认知升级之第一性原理》--507张PPT全解!_搜狐科技_搜狐网
    http://www.educity.cn/luntan/144478_5.html
    微服务架构下的分布式数据存储-技术之家
    http://blog.longjiazuo.com/archives/3080
    实施微服务架构的关键技术
    微服务架构的分布式事务解决方案
    k8s~helm镜像版本永远不要用latest
    nginx~对没有定义service_name的三级域名进行过滤
  • 原文地址:https://www.cnblogs.com/IS2120/p/6745673.html
Copyright © 2011-2022 走看看