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.