哪个效率更高?
字段stateA与stateB是相关联的,当stateA="1"时,stateB="0"或stateB="1";stateB="2"时,stateA="3"或stateA="4"。
现在我有两个思路,方法一:比较stateB与这四个值;方法二:把stateB转换成int类型然后做加减操作。请问哪个效率更高点?有什么办法可以查看比较这两中方法的效率?
方法一的代码:
protected void UpdateState(string stateA, string stateB)
{
if (stateA == "0")
{
stateA = "1";
SetLoBcodeState(stateB);
}
else
{
stateA = "0";
SetLoBcodeState(stateB);
}
}
protected string SetStateB(string state)
{
switch (state)
{
case "0":
return "2";
case "1":
return "3";
case "2":
return "0";
case "3":
return "1";
default:
return "Wrong State Value";
}
}
方法二的代码:
protected void UpdateState2(string stateA, string stateB)
{
if (stateA == "0")
{
stateA = "1";//此时stateB为"2"或"3"
stateB = (Convert.ToInt32(stateB) + 2).ToString();
}
else
{
stateA = "0";//此时stateB为"0"或"1"
stateB = (Convert.ToInt32(stateB) - 2).ToString();
}
}
相关推荐
C#判断字符串中是否包含指定字符串及contains与indexof方法效率问题
主要介绍了C#判断字符串中是否包含指定字符串及contains与indexof方法效率问题 ,文中给大家列举通过两种方法来判断,需要的朋友可以参考下
LABVIEW最实用的技巧合集.pdf
LABVIEW常用的开发技巧,由拥有多年开发经验的工程师几,记录,内容丰富实用,是初学者必看之书。
php使用mysqli和pdo扩展,测试对比连接mysql数据库的效率完整示例
主要介绍了php使用mysqli和pdo扩展,测试对比连接mysql数据库的效率,结合完整实例形式对比分析了php分别使用mysqli和pdo扩展连接mysql数据库的执行时间,需要的朋友可以参考下
探讨++i与i++哪个效率更高
答案: 在内建数据类型的情况下,效率没有区别; 在自定义数据类型的情况下,++i效率更高! 分析: (自定义数据类型的情况下) ++i返回对象的引用; i++总是要创建一个临时对象,在退出函数时还要销毁它,而且返回临时对象的值时还会调用其拷贝构造函数。 (重载这两个运算符如下) 代码如下:#include <iostream>using namespace std; class MyInterger{public: long m_data;public: MyInterger(long data):m_data(data){} MyInterger & operator++(
StringBuider 在什么条件下、如何使用效率更高
都说 StringBuilder 在处理字符串拼接上效率要强于 String,但有时候我们的理解可能会存在一定的偏差。今天小编给大家带来一篇教程关于StringBuider 在什么条件下、如何使用效率更高?感兴趣的朋友一起看看吧



