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

问题描述 投票:0回答:1

我需要有非常高性能的循环来处理大型数据集。我需要比较时间戳,但我的日期是 OA 格式:

if (md.DT_OA > new DateTime(2011, 3, 13).ToOADate()) 

编译器会在每个循环周期中评估

new DateTime(2011, 3, 13).ToOADate()
吗?或者“优化器”会在一开始就解决这个问题吗?

也就是说,我可以摆脱懒惰并在代码中加入这个吗?

正如你所知 - 我不太了解编译器是如何工作的......

编辑1

评论激励我做一个适当的测试:

下面的选项 2 比选项 1 大约

3% faster
。令人惊讶的是速度并没有快很多 - 编译器似乎非常智能或者日期创建速度很快。

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);
        } 
    }
}
c# datetime
1个回答
6
投票

我看不出它有什么理由会优化它......很确定它不会(我知道它不会在IL层优化它,但这并没有说明JIT;但我真的我不认为它会 - 它不知道该方法每次都会返回相同的内容)。

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

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

只是

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");
        }
    }

如果我们编译它,然后反汇编 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 

查看 L_0009 到 L_0011 - 即创建

new DateTime
每个循环迭代
L_0031: blt.s L_0004
是循环重复)。正如我所期望的,编译器对您的请求非常直白。

© www.soinside.com 2019 - 2024. All rights reserved.