2
    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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
George Irimiciuc
  • 4,573
  • 8
  • 44
  • 88
  • one second = 1,000 milliseconds = 1,000,000 microseconds = 1,000,000,000 nanoseconds – assylias Apr 01 '14 at 10:25
  • Refer to Wikipedia or elsewhere for the definition of the `milli-` and `nano-` prefix. Java did not invent them. – Marko Topolnik Apr 01 '14 at 10:25
  • Ah, so I should just add 3 and 9 decimals. I see. Thought they had a different meaning than the realtime ones. – George Irimiciuc Apr 01 '14 at 10:26
  • And keep in mind that the name "nanoTime" is deceptive. Depending on your operating system it will give different precision between several tens to several hundreds of nanoseconds. – jwenting Apr 01 '14 at 10:33
  • Actually, the *precision* of the value returned by nanoTime is always the same -- billionths of seconds. The *accuracy* can vary, i.e., not all systems have a clock that counts individual nanoseconds, and so the clock might only tick on 100 or 1000 nanoseconds and you will only ever get multiples of that. – arcy Apr 01 '14 at 10:59

2 Answers2

5

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.

Marco13
  • 53,703
  • 9
  • 80
  • 159
  • So I should just use `System.time.millis` instead of nano? – George Irimiciuc Apr 01 '14 at 10:58
  • It depends on your system. I would be surprised if most general-purpose computers had clocks that measured nanoseconds accurately, and in fact most PC-style machines used to only tick at 10 millisecond intervals. Keep in mind, too, that nanoseconds will overflow a long int much faster. – arcy Apr 01 '14 at 11:00
  • @rcook A time difference measured in nanoseconds will overflow after appx. 290 years, so this should not be an issue. Possibly interesting: http://stackoverflow.com/questions/351565/system-currenttimemillis-vs-system-nanotime – Marco13 Apr 01 '14 at 14:51
0

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));
pardeep131085
  • 5,468
  • 2
  • 21
  • 9