English 中文(简体)
经常支流,从长期到隐形
原标题:Current milliseconds, from long to int
  • 时间:2011-11-23 15:01:31
  •  标签:
  • java

I currently have the following code:

public static int currentTimeMillis()
{
    long millisLong = System.currentTimeMillis();
    while ( millisLong > Integer.MAX_VALUE )
    {
        millisLong -= Integer.MAX_VALUE;
    }
    return (int)millisLong;
}

which returns the current time in an int format (not exactly, but it can be used for time differences). For very good reasons, I can t use long.

Yes, I am just interested in the difference between two calls, and this approach works well. But it just looks wrong. I know that. And inefficient. I know. So my questions is, how can I improve it?

我需要一个接回<代码>int的职能。 因此:

int x1 = currentTimeMillis(); //my function
long y1 = System.currentTimeMillis();

.....

int x2 = currentTimeMillis();
long y2 = System.currentTimeMillis();

// here, x2 - x1 must be equal to y2 - y1

EDIT:

FYI, I want to do this for benchmarking. I m running tests on multiple threads in parallel, the stop event is triggered by an external component. I m also serializing the data in a way that only supports int, and the object I m serializing can not have long members.

最佳回答
问题回答

较快的解决办法不是找到一个模块,而是简单双tw掩盖行动:

public static int currentTimeMillis() {
    return (int) (System.currentTimeMillis() & 0x00000000FFFFFFFFL);
}

了解到如果<代码>现时Millis <代码>上的数值滚动 在<代码>x1和x2之间,请在结尾处添加<代码>x2 - x1作为负值。 解决这一问题有两个途径。

  1. Use BalusC s approach, and then when you subtract x2 - x1 check to see if it s negative. If it is, add Integer.MAX_VALUE to it again. (This assumes that no two time values will be checked that are actually more than Integer.MAX_VALUE distance apart.)
  2. Store the current time as a a private static value when the class is first initialized. From then on, make your function return the difference between the current time and that first startup time. This assumes that these time values will not need to persist when the system is restarted, and that the system will not be alive long enough to pass the Integer.MAX_VALUE mark.

作为附带说明,我相信我们会问,你为什么不使用<条码>长期<<<>>数值?

你赢得了一定时间,因为你在执行方案期间可以缩短时间,把事情稍微 off倒。 但是,得来的人数很多。 在不了解你的问题的情况下,我不得不假设你对实际时间不感兴趣。 也许你只是对过去时间重新感兴趣? 你们是否拥有一些基准? 如果时间已经过去,“巴普卢克”提出的组合解决办法就会被罚款。

你可以做些什么。

public static int currentTimeMillis() {
    return (int) System.currentTimeMillis();
}

如果其负数,它就取得了一定结果,那么你会有所作为。 e.g.

int start = currentTimeMillis();

// will be positive for differences less than 24 days.
int time = currentTimeMillis() - start; 

Using this approach avoid the issue of overflows as they will cancel out e.g.

int start = Integer.MAX_VALUE;
// 1 ms later
int time = Integer.MIN_VALUE /*due to overflow*/ - start; 
// time equals 1 due to underflow.




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签