我的编译器会忽略无用的代码吗? [英] Will my compiler ignore useless code?

查看:41
本文介绍了我的编译器会忽略无用的代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在网络上回答了有关此主题的几个问题,但没有找到我的问题的答案,或者是使用另一种语言,或者不是完全完全回答(无效代码不是 >无用的代码),这是我的问题:

I've been through a few questions over the network about this subject but I didn't find any answer for my question, or it's for another language or it doesn't answer totally (dead code is not useless code) so here's my question:

编译器会忽略无用的代码吗?

Is (explicit or not) useless code ignored by the compiler?

例如,在此代码中:

double[] TestRunTime = SomeFunctionThatReturnDoubles;
// A bit of code skipped
int i = 0;
for (int j = 0; j < TestRunTime.Length; j++)
{

}
double prevSpec_OilCons = 0;

for循环会被删除吗?

will the for loop be removed?

我使用

背景是我维护了很多代码(我没有写过),我想知道是否应该将无用的代码作为目标,还是让编译器来处理这个问题?

The background is that I maintain a lot of code (that I didn't write) and I was wondering if useless code should be a target or if I could let the compiler take care of that.

推荐答案

我根据一些回答者关于使用 long.MaxValue 的想法,做了一些表格来对其进行测试,这是我的参考代码:

I've made a little form to test it according to a few answerers ideas about using long.MaxValue, here's my reference code:

public Form1()
{
    InitializeComponent();
    Stopwatch test = new Stopwatch();
    test.Start();
    myTextBox.Text = test.Elapsed.ToString();
}

这是带有 kinda 无用代码的代码:

and here's the code with kinda useless code:

public Form1()
{
    InitializeComponent();
    Stopwatch test = new Stopwatch();
    test.Start();
    for (int i = 0; i < int.MaxValue; i++)
    {
    }
    myTextBox.Text = test.Elapsed.ToString();
}

您会说我使用 int.MaxValue 而不是 long.MaxValue ,我不想度过 year 天在这个.

You'll remark that I used int.MaxValue instead of long.MaxValue, I didn't want to spend the year day on this one.

如您所见:

---------------------------------------------------------------------
|                   |   Debug               |   Release             |
---------------------------------------------------------------------
|Ref                |   00:00:00.0000019    |   00:00:00.0000019    |
|Useless code       |   00:00:05.3837568    |   00:00:05.2728447    |
---------------------------------------------------------------------

代码未优化.稍等一下,我将尝试使用一些 int [] 来测试 int [].长度:

The code isn't optimized. Hang on a bit, I'll try with some int[] to test int[].Lenght:

public Form1()
{
    InitializeComponent();
    int[] myTab = functionThatReturnInts(1);

    Stopwatch test = new Stopwatch();
    test.Start();
    for (int i = 0; i < myTab.Length; i++)
    {

    }
    myTextBox.Text = test.Elapsed.ToString();
}
public int[] functionThatReturnInts(int desiredSize)
{
    return Enumerable.Repeat(42, desiredSize).ToArray();
}

结果如下:

---------------------------------------------
|   Size            |   Release             |
---------------------------------------------
|             1     |   00:00:00.0000015    |
|           100     |   00:00:00            |
|        10 000     |   00:00:00.0000035    |
|     1 000 000     |   00:00:00.0003236    |
|   100 000 000     |   00:00:00.0312673    |
---------------------------------------------

因此,即使使用数组,也根本不会对其进行优化.

So even with arrays, it doesn't get optimized at all.

这篇关于我的编译器会忽略无用的代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆