Java calendar.getInstance() 或 calendar.clone()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2595630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
calendar.getInstance() or calendar.clone()
提问by Aravind Yarram
I need to make a copy of a given date 100s of times (I cannot pass-by-reference). I am wondering which of the below two are better options
我需要复制给定日期 100 次(我不能通过引用传递)。我想知道以下哪两个是更好的选择
newTime=Calendar.getInstance().setTime(originalDate);
OR
或者
newTime=originalDate.clone();
Performance is of main conern here.
性能是这里的主要关注点。
thx.
谢谢。
采纳答案by armandino
I would use
我会用
newTime= (Calendar) originalDate.clone();
回答by nahojkap
My approach would be to go for option 1) and then make sure the application is thoroughly profiled to check for bottlenecks. It may be that the above code is not an issue at all in the overall performance of the application at the end of the day.
我的方法是选择选项 1),然后确保对应用程序进行彻底分析以检查瓶颈。可能是上面的代码在一天结束时对应用程序的整体性能来说根本不是问题。
回答by Chris Lercher
- My gut tells me, that clone() will be faster.
- Why not try it with a quick benchmark [*]?
- Consider using just the long value of
date.getTime()
, if you don't have to do calendar calculations.
- 我的直觉告诉我, clone() 会更快。
- 为什么不使用快速基准测试 [*] 来尝试?
date.getTime()
如果您不必进行日历计算,请考虑仅使用 的 long 值。
[*]
[*]
private static final int N = 100000;
public static void main(final String[] args) throws Exception {
final Date date = new Date();
{
final long start = System.currentTimeMillis();
for (int i = 0; i < N; i ++) {
final Date date2 = (Date) date.clone();
}
final long end = System.currentTimeMillis();
System.out.println("Clone: " + (end - start) + " ms");
}
{
final long start = System.currentTimeMillis();
for (int i = 0; i < N; i ++) {
final Calendar cal = Calendar.getInstance();
cal.setTime(date);
final Date date2 = cal.getTime();
}
final long end = System.currentTimeMillis();
System.out.println("Caldendar.setTime: " + (end - start) + " ms");
}
}
Results:
结果:
Clone: 13 ms
Caldendar.setTime: 317 ms
PS I'm not sure, if you really need a Calendar
, or a Date
, so feel free to modify the test...
PS我不确定,如果你真的需要一个Calendar
,或者一个Date
,那么请随意修改测试......
(In response to the comment: To improve test accuracy, you can also run the tests individually, increase the value of N, ...)
(回应评论:为了提高测试准确性,您也可以单独运行测试,增加N的值,...)
回答by user207421
I cannot pass-by-reference
我不能通过引用
You sure can't. There is no such thing in Java. But I would review the requirement. What is the actualrisk that someone is going to modify the Date if you pass around the same one all the time? and can you control that? e.g. by checking getTime() before and after each call, and throwing an RTE if it changes?
你肯定不能。Java 中没有这样的东西。但我会要求。如果您一直传递相同的日期,那么某人修改日期的实际风险是多少?你能控制吗?例如,通过在每次调用之前和之后检查 getTime(),并在它发生变化时抛出 RTE?
回答by johannesemden
In Scala I would do a clone()and a cast to Calendar with .asInstanceOf[Calendar]like:
在 Scala 中,我会使用.asInstanceOf[Calendar]执行clone()和转换为 Calendar ,例如:
val now = Calendar.getInstance()
val newDate = now.clone().asInstanceOf[Calendar]