小编Kud*_*ral的帖子

如果c#中的语句性能

我只是试图确定每个"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)

.net c# performance

4
推荐指数
1
解决办法
5766
查看次数

标签 统计

.net ×1

c# ×1

performance ×1