user2065929
user2065929

Reputation: 1095

How to convert a long to milliseconds

I have a program that times the amount of time a message takes to be sent from one machine to another, however the result in long format, how can I convert this to milliseconds

MPI.Init(args);
int myrank = MPI.COMM_WORLD.Rank();
long startTime = System.nanoTime();
if (myrank == 0) {
     ...
} else {
      ...
}

long endTime = System.nanoTime();
long duration = endTime - startTime;

as the results are :

MPJ Express (0.35) is started in the cluster configuration
Starting process <0> on <Raptor>
Starting process <1> on <Predator>
338198093
received: test               :
449582529
Stopping process <0> on <Raptor>
Stopping process <1> on <Predator>

and I would like to convert these to something a bit more readable

Upvotes: 0

Views: 774

Answers (2)

JB Nizet
JB Nizet

Reputation: 691785

One millisecond is 1,000,000 nanoseconds, so you just need a division.

A more readable and safe way to do it is to use the TimeUnit class:

long millis = TimeUnit.NANOSECONDS.toMillis(nanos)

Upvotes: 4

Reimeus
Reimeus

Reputation: 159784

One easy alternative is to use milliseconds to begin with:

long startTime = System.currentTimeMillis();
...
long endTime = System.currentTimeMillis();

Upvotes: 2

Related Questions