我需要將數十億的DateTimes從UTC轉換爲EDT。什麼是TimeZoneInfo.ConvertTime的更快選擇?
TimeZoneInfo.ConvertTime非常方便,但非常非常慢。
我比較簡單的時間跨度減法。請參閱下面的SSCCE。
如果您註釋掉OPTION 1或OPTION 2(如代碼所示),您將看到VASTLY不同的運行時間。
我想要的功能,但需要更高的速度有沒有辦法做到這一點?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Globalization;
namespace BinTest2
{
public partial class Form1 : Form
{
static TimeZoneInfo edtZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
static TimeZoneInfo gmtZone = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
static TimeZoneInfo utcZone = TimeZoneInfo.FindSystemTimeZoneById("UTC");
public static CultureInfo ci = CultureInfo.InvariantCulture;
private void button5_Click(object sender, EventArgs e)
{
Stopwatch sw = new Stopwatch();
sw.Start();
DateTime DT = new DateTime(2013,01,01);
DateTime TDT;
TimeSpan TS = new TimeSpan(4,0,0);
for (int i = 0; i < 100000000; i++)
{
//TDT = DT - TS; //OPTION 1
TDT = TimeZoneInfo.ConvertTime(DT, utcZone, edtZone); //OPTION 2
}
sw.Stop();
label1.Text = "Time taken: " + sw.ElapsedMilliseconds ;
}
}
}
難道你不能做一個幫助你的方法嗎?這對我來說似乎是兩全其美。 –
問題在於不同日期的時區更改。 TimeZoneInfo處理所有這一切 – ManInMoon
由於這兩個選項做了不同的事情(如您所指出的,選項1沒有考慮DST),所以說選項1是「更快」並不公平。(您指出,選項1不考慮DST) –