-
05-07-2019 - |
题
我可以实现
if (a == "b" || "c")
而不是
if (a == "b" || a== "c")
解决方案
不,你可以这样做:
if (new[] { "b", "c" }.Contains(a))
如果您有 LINQ 扩展程序,但这几乎没有改进。
在回应关于性能的评论时,这里有一些基本的时序代码。请注意,必须以批判的眼光来看待代码,我可能在这里做了一些使时间偏差的事情。
结果首先:
||, not found: 26 ms
||, found: 8 ms
array.Contains, not found: 1407 ms
array.Contains, found: 1388 ms
array.Contains, inline array, not found: 1456 ms
array.Contains, inline array, found: 1427 ms
switch-statement, not interned, not found: 26 ms
switch-statement, not interned, found: 14 ms
switch-statement, interned, not found: 25 ms
switch-statement, interned, found: 8 ms
所有代码都执行了两次,并且只传递了nr。报告了2,从等式中删除JITting开销。两个传递执行每种类型的检查一百万次,并执行它,其中要查找的元素是找到它的元素之一(即,if语句将执行其块),并且一旦元素不在(该块不会执行)。报告每个的时间。我测试了一个预构建的数组和每次构建的数组,这部分我不确定编译器推断和优化了多少,这里可能存在缺陷。
在任何情况下,看起来使用switch语句,无论是否首先实现字符串,都会给出与简单或语句大致相同的结果,这是预期的,而数组查找则更多昂贵的,对我来说也是预期的。
请修改代码,并在出现问题时更正(或评论)。
这是源代码,相当长:
using System;
using System.Linq;
using System.Diagnostics;
namespace StackOverflow826081
{
class Program
{
private const Int32 ITERATIONS = 1000000;
static void Main()
{
String a;
String[] ops = CreateArray();
Int32 count;
Stopwatch sw = new Stopwatch();
Int32 pass = 0;
Action<String, Int32> report = delegate(String title, Int32 i)
{
if (pass == 2)
Console.Out.WriteLine(title + ": " + sw.ElapsedMilliseconds + " ms");
};
for (pass = 1; pass <= 2; pass++)
{
#region || operator
a = "a";
sw.Start();
count = 0;
for (Int32 index = 0; index < ITERATIONS; index++)
{
if (a == "b" || a == "c")
{
count++;
}
}
sw.Stop();
report("||, not found", count);
sw.Reset();
a = "b";
sw.Start();
count = 0;
for (Int32 index = 0; index < ITERATIONS; index++)
{
if (a == "b" || a == "c")
{
count++;
}
}
sw.Stop();
report("||, found", count);
sw.Reset();
#endregion
#region array.Contains
a = "a";
sw.Start();
count = 0;
for (Int32 index = 0; index < ITERATIONS; index++)
{
if (ops.Contains(a))
{
count++;
}
}
sw.Stop();
report("array.Contains, not found", count);
sw.Reset();
a = "b";
sw.Start();
count = 0;
for (Int32 index = 0; index < ITERATIONS; index++)
{
if (ops.Contains(a))
{
count++;
}
}
sw.Stop();
report("array.Contains, found", count);
sw.Reset();
#endregion
#region array.Contains
a = "a";
sw.Start();
count = 0;
for (Int32 index = 0; index < ITERATIONS; index++)
{
if (CreateArray().Contains(a))
{
count++;
}
}
sw.Stop();
report("array.Contains, inline array, not found", count);
sw.Reset();
a = "b";
sw.Start();
count = 0;
for (Int32 index = 0; index < ITERATIONS; index++)
{
if (CreateArray().Contains(a))
{
count++;
}
}
sw.Stop();
report("array.Contains, inline array, found", count);
sw.Reset();
#endregion
#region switch-statement
a = GetString().Substring(0, 1); // avoid interned string
sw.Start();
count = 0;
for (Int32 index = 0; index < ITERATIONS; index++)
{
switch (a)
{
case "b":
case "c":
count++;
break;
}
}
sw.Stop();
report("switch-statement, not interned, not found", count);
sw.Reset();
a = GetString().Substring(1, 1); // avoid interned string
sw.Start();
count = 0;
for (Int32 index = 0; index < ITERATIONS; index++)
{
switch (a)
{
case "b":
case "c":
count++;
break;
}
}
sw.Stop();
report("switch-statement, not interned, found", count);
sw.Reset();
#endregion
#region switch-statement
a = "a";
sw.Start();
count = 0;
for (Int32 index = 0; index < ITERATIONS; index++)
{
switch (a)
{
case "b":
case "c":
count++;
break;
}
}
sw.Stop();
report("switch-statement, interned, not found", count);
sw.Reset();
a = "b";
sw.Start();
count = 0;
for (Int32 index = 0; index < ITERATIONS; index++)
{
switch (a)
{
case "b":
case "c":
count++;
break;
}
}
sw.Stop();
report("switch-statement, interned, found", count);
sw.Reset();
#endregion
}
}
private static String GetString()
{
return "ab";
}
private static String[] CreateArray()
{
return new String[] { "b", "c" };
}
}
}
其他提示
嗯,最接近你可以得到的是:
switch (a) {
case "b":
case "c":
// variable a is either "b" or "c"
break;
}
据我所知,这不是一种选择。
您可以使用正则表达式:
if(Regex.IsMatch(a, "b|c"))
如果“a”的内容是可以超过一个字符使用它:
if(Regex.IsMatch(a, "^(b|c)<*>quot;))
不,没有那种语法。但是有很多选项来编码。
if ("bc".Contains(a)) { } // Maybe check a.Length == 1, too.
if ((a[0] & 0x62) == 0x62) { } // Maybe check a.Length == 1, too.
if (new String[] { "b", "c" }.Contains(a)) { }
也许你可以做一些运算符重载并让你的语法有效,但这实际上取决于你想要实现的目标,并且很难从你的简单例子中看出来。
在某些情况下你可以。即标记的枚举:
[Flags]
enum MyEnum {
None = 0,
A = 1,
B = 2,
C = 4,
D = 8
}
//...
MyEnum a = MyEnum.B
if((a & (MyEnum.B | MyEnum.C)) > 0)
// do something
相当于:
if((a & MyEnum.B) > 0 || (a & MyEnum.C) > 0)
// do something
其原因与位掩码有关。在二进制文件中,
None = 00000
A = 00001
B = 00010
C = 00100
D = 01000
所以当我们使用|时运算符,我们进行逐位比较,查找列中的任何1并将它们复制到结果中。如果列中没有1,则复制0。
B 00010
& C 00100
---------
00110
然后当我们应用&amp;运算符,我们在复制1之前在每列的所有行中查找1。
(B & C) 00110
& (a = B) 00010
---------------
00010
哪个是&gt; 0,因此返回true。
奇怪的是,这是最有效的方法,因为它可以节省数字比较(&gt;)和逻辑运算符(||),它可以完成所有那些奇特的短路和诸如此类的事情。
不,这不是或者运算符(||)在C#中的工作方式。
另一种解决方案,虽然它使代码的可读性降低,但是创建一个检查所需值的函数,类似于:
public static bool Any(object a, params object[] b)
{
foreach(object item in b)
{
if(a == b)
{
return true;
}
}
return false;
}