• 关于string的一些测试


    #include 
    #include 
    #include  
    using namespace std;
    class spender
    {
    public:
    	spender(string strfun) :strfun(strfun)
    	{
    		t1 = std::chrono::steady_clock::now();
    	}
    	~spender()
    	{
    		auto t2 = std::chrono::steady_clock::now();
    		double dr_ms = std::chrono::duration<double, std::milli>(t2 - t1).count();
    		printf("%s耗时:%.3fms\n", strfun.c_str(), dr_ms);
    	}
    private:
    	decltype(std::chrono::steady_clock::now()) t1;
    	string strfun;
    };
    
    string Remove_Space1(string str)
    {
    	string result;
    	for (int i = 0; i < str.length(); i++)
    	{
    		if (!isspace(str[i]))
    		{
    			result = result + str[i];
    		}
    	}
    
    	return result;
    }
    
    string Remove_Space2(string str)
    {
    	string result;
    	for (int i = 0; i < str.length(); i++)
    	{
    		if (!isspace(str[i]))
    		{
    			result  += str[i];
    		}
    	}
    
    	return result;
    }
    
    string Remove_Space3(string str)
    {
    	string result;
    	result.reserve(str.length());
    	for (int i = 0; i < str.length(); i++)
    	{
    		if (!isspace(str[i]))
    		{
    			result += str[i];
    		}
    	}
    
    	return result;
    }
    
    string Remove_Space4(const string &str)
    {
    	string result;
    	result.reserve(str.length());
    	for (int i = 0; i < str.length(); i++)
    	{
    		if (!isspace(str[i]))
    		{
    			result += str[i];
    		}
    	}
    
    	return result;
    }
    
    void main()
    {
    
    	
    	string s = "hello world,hello world,hello world,hello world,hello world,\
    		hello world,hello world,hello world,hello world,hello world,hello world,hello world,hello world,hello world!";
    
    	{
    		spender t("Remove_Space1");
    		string strtmp = Remove_Space1(s); 
    		printf("%s\n",strtmp.c_str());
    	}
    
    	{
    		spender t("Remove_Space2");
    		string strtmp = Remove_Space2(s);
    		printf("%s\n", strtmp.c_str());
    	}
    
    	{
    		spender t("Remove_Space3");
    		string strtmp = Remove_Space3(s);
    		printf("%s\n", strtmp.c_str());
    	}
    
    	{
    		spender t("Remove_Space4");
    		string strtmp = Remove_Space4(s);
    		printf("%s\n", strtmp.c_str());
    	}
    
    	system("pause");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113

    结果:
    在这里插入图片描述
    从运行结果可以看出第一种方式最耗时,至于它的原因:result = result + str[i];是对象的拷贝而非移动,导致反复分配,释放内存和无畏地进行字符串复制。

  • 相关阅读:
    Java - NPE(NullPointerException);Optional
    QGC GuidedActionController.qml模式和toolBarIndicators 解锁和构型切 控制流程解析
    软件缺陷与软件测试定义
    使用自己的数据集测试Unbiased Mean Teacher for Cross-domain Object Detection
    图的结构模板及遍历
    Uniapp 文件选择插件 Ba-FilePicker
    centos安装mysql8.0.20、tar包安装方式
    HashMap源码阅读解惑
    一文初探 Go reflect 反射包
    Android --- 英文单引号用&apos;替换报错:does not contain a valid string resource
  • 原文地址:https://blog.csdn.net/FairLikeSnow/article/details/133053700