0

How to measure the a time-span in seconds using System.currentTimeMillis()?

I don't understand this code. I keep getting an error on the sceond line of code. Im having problems with the "counter". Is there a class or do I need an import for java.

long start = System.nanoTime();
counter.countPrimes(1000000);
final long end = System.nanoTime();

System.out.println("Took: " + ((end - start) / 1000000) + "ms");
System.out.println("Took: " + (end - start)/ 1000000000 + " seconds");
khelwood
  • 55,782
  • 14
  • 81
  • 108
slimKb
  • 11
  • You should check where `counter` is defined, as what type, where it is assigned a value, and what type it is. Wherever you took this code from should have the full definitions. – RealSkeptic Dec 01 '19 at 08:37
  • 1
    The `counter.countPrimes(100000)` is standing in for whatever code you want to time the execution of. It is not part of the logic of measuring the time. – khelwood Dec 01 '19 at 08:37
  • He's referring to this comment https://stackoverflow.com/a/6317765/7972621 and counter.CounPrimes() is just a function the guy is measuring the time of. It's his own implementation. For the code to run you need to create that class. – Ankit Sharma Dec 01 '19 at 08:39
  • 1
    Replace that line with whatever code whose execution time you want to measure. – Sweeper Dec 01 '19 at 08:59

0 Answers0