我只是试图确定每个"if"语句对我的C#应用程序在具有大量迭代的循环中使用时的性能的影响.我没有找到关于这个的主题所以我创建了这个.
对于测试,我做了2个循环:一个没有"if",一个有一个"if"语句.代码如下.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace IfPerformance
{
class Program
{
static void Main(string[] args)
{
int N = 500000000;
Stopwatch sw = new Stopwatch();
double a = 0, b = 0;
bool f;
sw.Restart();
for (int i = 0; i < N; i++)
{
a += 1.1;
f = a < N;
}
sw.Stop();
Console.WriteLine("Without if: " + sw.ElapsedMilliseconds + " ms");
a = 0;
sw.Restart();
for (int i = …Run Code Online (Sandbox Code Playgroud)