c#编译器如何处理这个DateTime.ToOADate()?

Man*_*oon 1 c# compiler-construction datetime

我需要在大型数据集上进行非常高性能的循环.我需要比较TimeStamps,但我的日期是OA格式:

if (md.DT_OA > new DateTime(2011, 3, 13).ToOADate()) 
Run Code Online (Sandbox Code Playgroud)

编译器会new DateTime(2011, 3, 13).ToOADate()在每个循环周期中进行评估吗?或者"优化者"会在开始时解决这个问题.

即我可以逃避懒惰并在代码中拥有这个吗?

你可以告诉我 - 我不太了解编译器的工作原理......

编辑1

评论启发我做了一个适当的测试:

下面的选项2大约3% faster是选项1.令人惊讶的是,速度不是很快 - 编译器似乎非常智能或创建日期很快.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Go();
        }

        public static void Go() 
        {
            int c = 0;
            DateTime st = DateTime.Now;
            DateTime dt = new DateTime(2011, 3, 13);
            for (int i = 0; i < 1000000; i++) 
            {
                if (DateTime.Now > new DateTime(2011, 3, 13)) // Option 1
                //if (DateTime.Now > dt)                          // Option 2
                {
                    c++;
                }
            }

            MessageBox.Show("Time taken: " + DateTime.Now.Subtract(st).TotalMilliseconds + " c: " + c);
        } 
    }
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ell 6

我认为它没有理由优化它......非常肯定它不会(我知道它不会在IL层优化它,但是这并没有说明JIT;但我真的不认为它将 - 它不知道该方法每次都会返回相同的东西).

相反,如果您关心它,请将其从环路中取出:

var oaDate = new DateTime(2011, 3, 13).ToOADate();
...loop...
    if (md.DT_OA > oaDate) {...}
Run Code Online (Sandbox Code Playgroud)

Re just new DateTime(int,int,int)(评论); 让我们创建一个测试程序:

    static void Main()
    {
        for (int i = 0; i < 1000; i++)
        {
            if (DateTime.Now > new DateTime(2011, 3, 13)) Console.WriteLine("abc");
        }
    }
Run Code Online (Sandbox Code Playgroud)

如果我们编译然后反汇编IL(reflector/ildasm/etc),我们得到:

L_0000: ldc.i4.0 
L_0001: stloc.0 
L_0002: br.s L_002b
L_0004: call valuetype [mscorlib]System.DateTime [mscorlib]System.DateTime::get_Now()
L_0009: ldc.i4 0x7db
L_000e: ldc.i4.3 
L_000f: ldc.i4.s 13
L_0011: newobj instance void [mscorlib]System.DateTime::.ctor(int32, int32, int32)
L_0016: call bool [mscorlib]System.DateTime::op_GreaterThan(valuetype [mscorlib]System.DateTime, valuetype [mscorlib]System.DateTime)
L_001b: brfalse.s L_0027
L_001d: ldstr "abc"
L_0022: call void [mscorlib]System.Console::WriteLine(string)
L_0027: ldloc.0 
L_0028: ldc.i4.1 
L_0029: add 
L_002a: stloc.0 
L_002b: ldloc.0 
L_002c: ldc.i4 0x3e8
L_0031: blt.s L_0004
L_0033: ret 
Run Code Online (Sandbox Code Playgroud)

通过L_0011查看L_0009 - 即创建new DateTime 每循环迭代(L_0031: blt.s L_0004循环重复).正如我所料,编译器对您的请求非常直接.