java - Calendar.getInstance() 性能通过 'warm up' 调用得到改进?

标签 java calendar

我注意到,如果我调用一次 Calendar.getInstance(),在我真正需要 Calendar 实例之前,第二次调用的性能会提高。

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());

    // this block of code performs faster if the above code is included
    Calendar calendar2 = Calendar.getInstance();
    calendar2.setTimeInMillis(System.currentTimeMillis());

我正在从我的服务器获取自纪元以来的时间并用它初始化日历。然后将日历解析为小时、分钟、秒和毫秒,并将其传递给设置时间的 native 函数。

我注意到,如果我首先实例化虚拟日历,节点之间的时钟差异为 2-5 毫秒,如果不实例化,则为 15-20 毫秒。

我正在测量从节点 A 到 B 的往返时间和数据包传输时间,反之亦然。我在请求-响应事务中的节点之间共享时间戳,并注意到当没有“预热”时,这些时间戳会更加关闭。

虽然我的测量结果可能有问题,但我始终通过“热身”电话获得更小的行程时间值。

我正在查看 Calendar 的源代码,但我没有看到任何对此行为的解释。

日历实现中是否有任何内容会影响后续调用的初始化性能?

Average from A to B: 21,4 

Average from A to B: 15,14 

Average from A to B: 14,84 

Average from A to B: 14,07 

有热身电话

Average from A to B: 11,8 

Average from A to B: 4,77 

Average from A to B: 4,54 

Average from A to B: 4,4

更新

随着时差变为 0.8 - 1 毫秒,提前单次调用 native 函数似乎也受益于性能改进。

最佳答案

Is there anything in calendar implementation that could affect performance of initialization with subsequent calls ?

这是 Calendar#getInstance() 的代码:

public static Calendar getInstance()
{
    Calendar cal = createCalendar(TimeZone.getDefaultRef(), Locale.getDefault(Locale.Category.FORMAT));
    cal.sharedZone = true;
    return cal;
}

TimeZone#getDefaultRef() 的代码是:

static TimeZone getDefaultRef() {
    TimeZone defaultZone = getDefaultInAppContext();
    if (defaultZone == null) {
        defaultZone = defaultTimeZone;
        if (defaultZone == null) {
            // Need to initialize the default time zone.
            defaultZone = setDefaultZone();
            assert defaultZone != null;
        }
    }
    // Don't clone here.
    return defaultZone;
}

Locale.getDefault(Locale.Category.FORMAT) 之一:

public static Locale getDefault(Locale.Category category) {
    // do not synchronize this method - see 4071298
    // it's OK if more than one default locale happens to be created
    switch (category) {
    case DISPLAY:
        if (defaultDisplayLocale == null) {
            initDefault(category);
        }
        return defaultDisplayLocale;
    case FORMAT:
        if (defaultFormatLocale == null) {
            initDefault(category);
        }
        return defaultFormatLocale;
    default:
        assert false: "Unknown Category";
    }
    return getDefault();
}

注意这两种方法如何检查已经初始化的变量,例如静态变量 defaultTimeZonedefaultFormatLocale。至少这有望提高第二次调用 Calendar#getInstance() 的性能。

关于java - Calendar.getInstance() 性能通过 'warm up' 调用得到改进?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33700082/

相关文章:

Java:Pacman 与墙壁的碰撞检测

java - 如何在 Netbeans 中设置 JList 的 ListModel?

java - 如何在java中获得上个月/一年?

python - 如何将月份名称映射到月份编号,反之亦然?

python - 在 Raspberry Pi 上通过 caldav 使用 python 读取 owncloud 日历

java: ArrayList() 方法返回结果:如果传递参数或不传递参数

java - int 数组的通用交换方法

java - 在 Actor 中停止 Akka Actor

android - Java/Android Calendar,周日增加1周

java - 删除 <af :inputDate> 的日历图标