“0==myString.length”的效率比“""==myString”高

本文通过实验对比了两种字符串判空方法:“”==myString与0==myString.Length的执行效率。测试代码使用C#编写,并利用Stopwatch进行计时,结果显示在大量数据下两者的性能差异。

测试代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace cs_6
{
    /// <summary>
    /// 测试""==myString与0==myString.length的效率问题
    /// </summary>
    class Program
    {
        /// <summary>
        /// 必须从命令行传入一个数字串参数(表示测试的数据量)
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            int count;
            int m=0,n=0;
            double Milliseconds1, Millisenconds2;
            Stopwatch stopwatch1 = new Stopwatch();
            if (0 == args.Length)
            {
                Console.WriteLine("请传入一个参数!");
                Console.ReadKey();
                return;
            }
            if (!int.TryParse(args[0],out count))
            {
                Console.WriteLine("传入的参数必须为数字串!");
                Console.ReadKey();
                return;
            }
            String myString = "";


            //“"" == myString”判断的效率
            stopwatch1.Start();
            for (int i = 0; i < count; i++)
            {
                if ("" == myString)
                {
                    m++;
                }
            }
            stopwatch1.Stop();
            Milliseconds1 = stopwatch1.Elapsed.Milliseconds;


            //“0 == myString.Length”判断的效率
            Stopwatch stopwatch2 = new Stopwatch();
            stopwatch2.Start();
            for(int i=0;i<count;i++)
            {
                if (0 == myString.Length)
                {
                    n++;
                }
            }
            stopwatch2.Stop();
            Millisenconds2 = stopwatch2.Elapsed.Milliseconds;
            string fomat=@"“"" == str1”判断的效率为:{0},“0 == str1.Length”判断的效率为:{1}";
            Console.WriteLine(fomat,Milliseconds1,Millisenconds2);
            Console.ReadKey();

        }    
    }
}


 

测试结果如下:

#include <iostream> #include <cstring> using namespace std; class MyString { private: char* data; // 存储字符串数据 int length; // 字符串长度 public: // 默认构造函数 MyString() : length(0) { data = new char[1]; data[0] = '\0'; } // 从C风格字符串构造 MyString(const char* str) { if (str == nullptr) { length = 0; data = new char[1]; data[0] = '\0'; } else { length = strlen(str); data = new char[length + 1]; strcpy(data, str); } } // 拷贝构造函数 MyString(const MyString& other) { length = other.length; data = new char[length + 1]; strcpy(data, other.data); } // 填充构造函数:n个字符c MyString(int n, char c) { if (n <= 0) { length = 0; data = new char[1]; data[0] = '\0'; } else { length = n; data = new char[length + 1]; for (int i = 0; i < length; ++i) { data[i] = c; } data[length] = '\0'; } } // 析构函数 ~MyString() { delete[] data; } // 赋值运算符重载 MyString& operator=(const MyString& other) { if (this != &other) { delete[] data; length = other.length; data = new char[length + 1]; strcpy(data, other.data); } return *this; } // 下标运算符重载(可修改) char& operator[](int index) { // 简单的越界检查 if (index < 0 || index >= length) { cerr << "索引越界!" << endl; exit(1); } return data[index]; } // 下标运算符重载(只读) const char& operator[](int index) const { if (index < 0 || index >= length) { cerr << "索引越界!" << endl; exit(1); } return data[index]; } // 相等比较运算符 bool operator==(const MyString& other) const { if (length != other.length) return false; return strcmp(data, other.data) == 0; } // 字符串连接运算符 MyString operator+(const MyString& other) const { MyString result; result.length = length + other.length; delete[] result.data; // 释放默认分配的内存 result.data = new char[result.length + 1]; strcpy(result.data, data); strcat(result.data, other.data); return result; } // 友元:输入运算符 friend istream& operator>>(istream& is, MyString& str) { char buffer[1024]; // 假设输入不超过1023个字符 is >> buffer; // 释放原有内存并分配新内存 delete[] str.data; str.length = strlen(buffer); str.data = new char[str.length + 1]; strcpy(str.data, buffer); return is; } // 友元:输出运算符 friend ostream& operator<<(ostream& os, const MyString& str) { os << str.data; return os; } }; int main() { MyString s1; MyString s2("hello"); MyString s3(s2); MyString s4(5, 'x'); cin >> s1; cout << s4; s1 = s4; cout << s4[0]; s4[0] = 'x'; if (s1 == s4) { cout << "s1==s4" << endl; } s1 = s4 + "hello"; return 0; } 不使用strlenstrcmp函数
09-03
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值