.net core 的时间日期类为啥这么慢, 跟 Java 至少几十倍的差距
source link: https://www.v2ex.com/t/877417
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
.net core 的时间日期类为啥这么慢, 跟 Java 至少几十倍的差距
thinkershare 9 小时 41 分钟前 4 不知道你在说啥, 你这样提问, 不提供代码, 没有任何意义. .NET 日志库一大堆, 我完全不相信你自己写的日志组件能达到第三方日志组件的水平, 因为你一看对.NET 常规 API 都不熟练.
|
Leviathann 9 小时 22 分钟前 via iPhone 为什么要用一个被官方标记 deprecated 的类我说的是 date
|
thinkershare 9 小时 3 分钟前 1 https://imgur.com/QUQOi6q.jpg ,Java 提供的 Date 类型早就被废弃, 和 DateTime 提供的 API 都没法公平比较. 根本就不应该被使用. .NET 提供的 DateTime 其实也应该被废弃, 使用 DateTimeOffset 替代.
|
Bingchunmoli 8 小时 54 分钟前 via Android @thinkershare 实际是项目依旧 1.8 ,有些序列化支持 D'ateTime 还需要额外操作,date 实际代码用的还是更多。 还是早点更替把,框架支持跟上
|
Maboroshii 8 小时 17 分钟前 via Android .net 性能确实不咋地… 但是最近也需要用.net 开发。搭车请教一下.net6 怎么用 github 上别人写的库?我拉下来发现.net 版本不一致都不能跑… 是一个一个文件复制到我自己的项目里面用吗
|
a33291 8 小时 15 分钟前
cpu i5-9600K
内存 32G
以下代码均不开优化,否则可能会被优化掉
场景 1 格式化为字符串
C# netcore 6.0.400
```
using System;
using System.Diagnostics;
var sw = Stopwatch.StartNew();
for (var i = 0; i < 1_000_000; i++)
{
var now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
//var now = DateTime.Now;
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
```
Java 1.8.0.333
```
package com.company;
import java.util.Date;
import java.text.SimpleDateFormat;
class Playground {
public static void main(String[] args) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long stime = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
Date now = new Date();
String nowString = format.format(now);
}
long etime = System.currentTimeMillis();
System.out.printf("%d ", (etime - stime));
}
}
```
以上代码分别执行 5 次
C# 243 232 237 233 236
Java 617 629 619 632 616
场景 1 只读取当前时间,不做格式化
C# netcore 6.0.400
```
using System;
using System.Diagnostics;
var sw = Stopwatch.StartNew();
for (var i = 0; i < 1_000_000; i++)
{
//var now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
var now = DateTime.Now;
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
```
Java 1.8.0.333
```
package com.company;
import java.util.Date;
import java.text.SimpleDateFormat;
class Playground {
public static void main(String[] args) {
//SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long stime = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
Date now = new Date();
//String nowString = format.format(now);
}
long etime = System.currentTimeMillis();
System.out.printf("%d ", (etime - stime));
}
}
```
以上代码分别执行 5 次
C# 39 38 39 39 39
Java 9 10 10 9 9
结论
1. 均执行时间字符串格式化时,C#比 Java 快
2. 仅执行获取当前时间的代码时,java 更快
关于结论 2 的分析
通过查看源代码,java 的 new Date() 仅执行一个成员赋值
```
public Date() {
this(System.currentTimeMillis());
}
public Date(long date) {
fastTime = date;
}
private transient long fastTime;
```
而 C#的 Date.Now 源码
```
public static DateTime Now
{
get
{
DateTime utc = UtcNow;
long offset = TimeZoneInfo.GetDateTimeNowUtcOffsetFromUtc(utc, out bool isAmbiguousLocalDst).Ticks;
long tick = utc.Ticks + offset;
if ((ulong)tick <= MaxTicks)
{
if (!isAmbiguousLocalDst)
{
return new DateTime((ulong)tick | KindLocal);
}
return new DateTime((ulong)tick | KindLocalAmbiguousDst);
}
return new DateTime(tick < 0 ? KindLocal : MaxTicks | KindLocal);
}
}
public static unsafe DateTime UtcNow
{
get
{
ulong fileTimeTmp; // mark only the temp local as address-taken
s_pfnGetSystemTimeAsFileTime(&fileTimeTmp);
ulong fileTime = fileTimeTmp;
if (s_systemSupportsLeapSeconds)
{
// Query the leap second cache first, which avoids expensive calls to GetFileTimeAsSystemTime.
LeapSecondCache cacheValue = s_leapSecondCache;
ulong ticksSinceStartOfCacheValidityWindow = fileTime - cacheValue.OSFileTimeTicksAtStartOfValidityWindow;
if (ticksSinceStartOfCacheValidityWindow < LeapSecondCache.ValidityPeriodInTicks)
{
return new DateTime(dateData: cacheValue.DotnetDateDataAtStartOfValidityWindow + ticksSinceStartOfCacheValidityWindow);
}
return UpdateLeapSecondCacheAndReturnUtcNow(); // couldn't use the cache, go down the slow path
}
else
{
return new DateTime(dateData: fileTime + (FileTimeOffset | KindUtc));
}
}
}
```
可以看出 java 的实现在仅 new Date()的情况下,执行的操作比 C#少很多,诸如 getTime 等都是调用时才计算,而 C#这边则是已经需要计算很多成员所以更慢.
Recommend
-
90
前言相关术语的解释时间的表现形式time模块datetime模块时间格式码总结前言在开发工作中,我们经常需要用到日期与时间,如:作为日志信息的内容输出计算某个功能的执行时间用日期命名一个日志文件的名称记录或展示某文章的发布或修改时间其他Python中提供了多个用...
-
62
点击上方“程序员大咖”,选择“置顶公众号” 关键时刻,第一时间送达! 写过Py...
-
44
golang 的日期时间包: time 的使用方式。 time package 包含了 time.Time 时间对象 及 构建此时间对象的一些方法( time.Unix()
-
8
编者按:本文来自微信公众号 “腾讯研究院”(ID:cyberlawrc) ,36氪经授权发布。 作者 闫德利腾讯研究院资深专家 王潇咏腾讯研究院特约研究...
-
7
同样都是使用接口,JAVA和Go差距咋就这么大呢? 13161658867 · 大约16小时之前 · 383 次点击 ·...
-
7
Tim Cook证实苹果员工重返办公室的时间至少推迟到10月 2021年07月29日07:41 新浪科技综合 我有话说(2人参与) 收藏本...
-
3
毕业十年,差距为何这么大?张一鸣认为:人才是一种基因发布时间:2022-11-29 01:06:42...
-
5
为什么台式机RTX 4080和笔记本RTX 4080性能差距这么大?|显卡|rtx|gtx|cuda_网易订阅 台式机和笔记本RTX 4080性能差距巨大,其中4K游戏性能差距高达35%,究竟...
-
7
新造车观察6min read至少 30 万起?雷军:为什么小米 SU7 敢卖这么贵?2023/12/29
-
2
Gurman:Apple Vision Pro 2 至少还有 18 个月的时间 admin 2024年02月19日 22:52 苹果首次通过Vision Pro头显进军混合现实领域,总...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK