第一次使用时,Jodatime的LocalDateTime很慢 [英] Jodatime's LocalDateTime is slow when used the first time

查看:363
本文介绍了第一次使用时,Jodatime的LocalDateTime很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在java项目中测试一些webapp技术,并且想知道为什么页面有时会加载很快,有时需要花费近5秒来加载。

I'm currently testing out some webapp technologies in a java project and was wondering why the pages sometimes load fast and sometimes take almost 5s to load.

我终于发现它就是这一行

I finally found out that it is this line

LocalDateTime now = new LocalDateTime();

当它第一次被调用时,需要永远获得当前时间。
在此之后调用时,即使在完全不同的地方,它也会非常快。

When it's called the first time, it takes forever to get the current time. When called after that, even somewhere completely different, it's pretty fast however.

我目前正在使用

    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>1.6.2</version>
    </dependency>

有没有人有类似的经历?真的卡在这里..我可以在我的应用程序中提前一段时间使用LocalDateTime来加强后续调用 - 但这似乎相当沉闷。

Has anyone had any similar experience? Really stuck here.. I could use LocalDateTime some time early in my application to fasten up subsequent calls - but this seems pretty dull tho.

编辑

EDIT

我现在误用了Spring:

I misuse Spring for that now:

@Service
public class JodaTimeLoader {

  public JodaTimeLoader() {
    LocalDateTime loadMe = new LocalDateTime();
  }

}


推荐答案

第一次这样做时,Joda Time加载了许多静态资源(例如,年表描述符)这是你所看到的成本。这是一次性费用;你按程序支付一次。如果真的困扰你,请在启动期间尽早加载它,也许是这样:

The first time you do that, Joda Time loads a number of static resources (e.g., its chronology descriptors) which is the cost that you're seeing. This is a one-off cost; you pay it once per process. Load it early during startup if it really bothers you, perhaps like this:

static {
    // Build the local caches inside Joda Time immediately instead of lazily
    new LocalDateTime();
}

这篇关于第一次使用时,Jodatime的LocalDateTime很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆