Question

    long startTime = System.nanoTime();
    long startTimer = System.currentTimeMillis();

    M = app.decriptare_simpla(C);

    long endTime = System.nanoTime();
    long stopTimer = System.currentTimeMillis();

    //mesajul initial dupa decriptare
    System.out.println("M : " + M.toString());
    System.out.println("Decriptarea a durat: " + (endTime - startTime));
    System.out.println("Decriptarea a durat: " + (stopTimer - startTimer));

This gave me:

Decriptarea a durat: 14811776
Decriptarea a durat: 15

What I want to ask is how much of a second are those 2 numbers? I mean are they, 0.15, 0.015, 0.0015...? I'd like to print them in that manner, not as an long but don't know how many decimals to add. Same question for the other number.

Was it helpful?

Solution

The conversions follow the usual rules for Standard SI Units:

long nanoSeconds = ...
double microSeconds = nanoSeconds / 1e3;
double milliSeconds = microSeconds / 1e3;
double seconds = milliSeconds / 1e3;

// Shortcuts:
double milliSeconds = nanoSeconds / 1e6;
double sconds = nanoSeconds / 1e9;

For some conversions, you can also have a look at the TimeUnit class: It allows conversions between values in different time units, for example

long microSeconds = NANOSECONDS.toMicros(nanoSeconds);

However, it unfortunately does not allow time spans given in double precision, but only as long values.


An aside, also mentioned in the comments: Measuring time spans in the order of 10-15ms usually makes no sense due to the limited resolution of the internal timer.

OTHER TIPS

Have you tried like this

System.out.println(TimeUnit.SECONDS.convert((endTime - startTime), TimeUnit.NANOSECONDS));
System.out.println(TimeUnit.SECONDS.convert((stopTimer - startTimer), TimeUnit.MILLISECONDS));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top