Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将DateTime中的天、小时、分钟、秒作为十进制?_C#_Performance_Datetime_Decimal - Fatal编程技术网

C# 如何将DateTime中的天、小时、分钟、秒作为十进制?

C# 如何将DateTime中的天、小时、分钟、秒作为十进制?,c#,performance,datetime,decimal,C#,Performance,Datetime,Decimal,我有一个DateTime,需要以十进制格式获取日期和小时数。例如,对于newdatetime(2009,6,19,18,0,0)我需要的天数为19.75Day+(TimeOfDay.TotalHours/24)似乎有效,但是否有直接或更好的转换?考虑到我也在使用高分辨率的日期,速度对于这个计算很重要。给定 DateTime time = new DateTime(2009, 6, 19, 18, 0, 0); …你所拥有的 decimal totalDays1 = (decimal) (tim

我有一个
DateTime
,需要以十进制格式获取日期和小时数。例如,对于
newdatetime(2009,6,19,18,0,0)我需要的天数为
19.75
Day+(TimeOfDay.TotalHours/24)
似乎有效,但是否有直接或更好的转换?考虑到我也在使用高分辨率的日期,速度对于这个计算很重要。

给定

DateTime time = new DateTime(2009, 6, 19, 18, 0, 0);
…你所拥有的

decimal totalDays1 = (decimal) (time.Day + time.TimeOfDay.TotalHours / 24);// 19.75M
…已经相当简洁了,所以我不知道您希望或期望改进多少。您可以使用,但需要更多的工作来设置

DateTime lastDayOfPreviousMonth = new DateTime(time.Year, time.Month, 1).AddDays(-1);
decimal totalDays2 = (decimal) (time - lastDayOfPreviousMonth).TotalDays;// 19.75M
我曾经测试过四种不同的方法

using System;
using BenchmarkDotNet.Attributes;

public static class Program
{
    static void Main(string[] args)
    {
        BenchmarkDotNet.Running.BenchmarkRunner.Run<CalculateTotalDaysBenchmarks>();
    }
}

[ClrJob()]
[CoreJob()]
public class CalculateTotalDaysBenchmarks
{
    private static readonly DateTime TestTime = new DateTime(2009, 6, 19, 18, 0, 0);

    [Benchmark(Baseline = true)]
    public decimal Method1_DayPlusTotalHoursDivided_CastResult()
    {
        return (decimal) (TestTime.Day + TestTime.TimeOfDay.TotalHours / 24);
    }

    [Benchmark()]
    public decimal Method1_DayPlusTotalHoursDivided_CastTotalHours()
    {
        return TestTime.Day + (decimal) TestTime.TimeOfDay.TotalHours / 24;
    }

    [Benchmark()]
    public decimal Method2_DayPlusTicksDivided()
    {
        return TestTime.Day + (decimal) TestTime.TimeOfDay.Ticks / TimeSpan.TicksPerDay;
    }

    [Benchmark()]
    public decimal Method3_SubtractLastDayOfPreviousMonth()
    {
        DateTime lastDayOfPreviousMonth = new DateTime(TestTime.Year, TestTime.Month, 1).AddDays(-1);

        return (decimal) (TestTime - lastDayOfPreviousMonth).TotalDays;
    }

    [Benchmark()]
    public decimal Method4_NewTimeSpan()
    {
        return (decimal) new TimeSpan(TestTime.Day, TestTime.Hour, TestTime.Minute, TestTime.Second, TestTime.Millisecond).TotalDays;
    }
}

您开始使用的方法要比其他方法快得多。

您尝试过使用吗?我认为新的timespan()。totaldays可以工作。在速度方面,不确定其中一种方法是否优于另一种方法。。。。新的时间跨度(cd.Day、cd.Hour、cd.Minute、cd.Second、cd.millis秒);啊,这也行,尽管它会截断任何亚毫秒的时间,如果这对你很重要的话。你追求的是速度吗?清晰更少的字符?速度对于这个计算是重要的,考虑我也使用HRE-RES日期。我已经添加了基准代码和结果到我的答案。
// * Summary *

BenchmarkDotNet=v0.11.4, OS=Windows 10.0.17763.379 (1809/October2018Update/Redstone5)
Intel Core i7 CPU 860 2.80GHz (Nehalem), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=2.1.505
  [Host] : .NET Core 2.1.9 (CoreCLR 4.6.27414.06, CoreFX 4.6.27415.01), 64bit RyuJIT
  Clr    : .NET Framework 4.7.2 (CLR 4.0.30319.42000), 64bit RyuJIT-v4.7.3362.0
  Core   : .NET Core 2.1.9 (CoreCLR 4.6.27414.06, CoreFX 4.6.27415.01), 64bit RyuJIT


|                                          Method | Runtime |     Mean |     Error |    StdDev | Ratio | RatioSD |
|------------------------------------------------ |-------- |---------:|----------:|----------:|------:|--------:|
|     Method1_DayPlusTotalHoursDivided_CastResult |     Clr | 118.2 ns | 1.2644 ns | 1.1827 ns |  1.00 |    0.00 |
| Method1_DayPlusTotalHoursDivided_CastTotalHours |     Clr | 263.9 ns | 0.7289 ns | 0.6462 ns |  2.23 |    0.02 |
|                     Method2_DayPlusTicksDivided |     Clr | 194.1 ns | 0.8827 ns | 0.8256 ns |  1.64 |    0.02 |
|          Method3_SubtractLastDayOfPreviousMonth |     Clr | 138.9 ns | 0.4757 ns | 0.3714 ns |  1.17 |    0.01 |
|                             Method4_NewTimeSpan |     Clr | 134.7 ns | 0.8376 ns | 0.7835 ns |  1.14 |    0.01 |
|                                                 |         |          |           |           |       |         |
|     Method1_DayPlusTotalHoursDivided_CastResult |    Core | 113.3 ns | 0.1982 ns | 0.1655 ns |  1.00 |    0.00 |
| Method1_DayPlusTotalHoursDivided_CastTotalHours |    Core | 261.3 ns | 2.9683 ns | 2.6313 ns |  2.31 |    0.02 |
|                     Method2_DayPlusTicksDivided |    Core | 197.9 ns | 4.4254 ns | 5.2681 ns |  1.74 |    0.04 |
|          Method3_SubtractLastDayOfPreviousMonth |    Core | 131.1 ns | 0.8406 ns | 0.7863 ns |  1.16 |    0.01 |
|                             Method4_NewTimeSpan |    Core | 132.1 ns | 1.1211 ns | 1.0486 ns |  1.16 |    0.01 |