• 关于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];是对象的拷贝而非移动,导致反复分配,释放内存和无畏地进行字符串复制。

  • 相关阅读:
    DDD(领域驱动设计)
    SpringBoot的测试
    Node简介
    Python自学教程1-安装pycharm和执行环境
    (七)Java算法:希尔插入排序(详细图解)
    js实现购物车加减操作
    期末作业C#实现学生宿舍管理系统
    在 4GB 物理内存的机器上,申请 8G 内存会怎么样?
    语言学本科论文有什么好的选题推荐吗?
    阿里为何禁止在对象中使用基本数据类型
  • 原文地址:https://blog.csdn.net/FairLikeSnow/article/details/133053700