#Want to know Runtime of a program:
1 messages · Page 1 of 1 (latest)
Well what is the method supposed to do?
And the first one... doesn't return anything, so how can you say that it gives a result ?
The method supposed to perform number of multiplications equal to n*log2n.
I have other program I call in it, so it returns
Ok, have you considered creating a variable int numberOfMultiplications = n * Math.log(2 * n)?
it doesn't return anything, it cannot give any result
Have you noticed that you're multiplying by 1?
ah and that too ↑
yes, I am using "System.currentTimeMillis()" for testing the runtime of the superlinear method only
- Can you send us the actual code you're running please, because it sounds quite different from your first message
- Be aware that the compiler will make your code more efficient - if it detects that your computation boils down to multiplying by 1, it is likely to optimise that by removing the loop
class Oppgave1 {
public static int logarithms(long n) {
int tmp = 1, iterasjoner = 0;
for (long i = n; i > 0; i /= 2, iterasjoner++)
tmp *= 1;
return iterasjoner;
}
public static int superlinear(long n) {
int tmp = 1;
for (long i = 0; i < n; i++)
for (long j = 0; j < Math.log(n) / Math.log(2); j++) {
tmp *= 1;
}
return tmp;
}
public static int superlinear2(long n) {
int tmp = 1;
int iterations = 0;
for (long i = 0; i < n; i++){
tmp *= 1;
for (long j = n; j > 0; j /=2, iterations++) {
tmp *= 1;
}
}
return tmp;
}
public static void main(String[] args)
{
Scanner S = new Scanner(System.in);
long n, T, T1, T2;
int choice, iterations = 0;
System.out.print("1:O(log_n) 2:(nlog2n)? ");
choice = S.nextInt();
System.out.print("n? ");
n = S.nextLong();
T1 = System.currentTimeMillis();
if (choice == 1)
iterations = logarithms(n);
else if (choice == 2)
superlinear(n);
T2 = System.currentTimeMillis();
T = T2 - T1;
System.out.print("T = " + T+ " ms");
if (choice == 1)
System.out.print(" (" + iterations + ")");
System.out.println();
}
} ```
Ok, do this
And add this VM argument when running your program: -Djava.compiler=NONE
Where in the program do I add
Which IDE are you using?
even with those flags added, ur timing will be super off
u simply have to accept the fact that any sort of timing is impossible in a VM language
use JMH which was made for that purpose
Intelij
what ur doing here is literally exactly what jmh was made for
no matter what u do, unless u use jmh, ur results will be completely incorrect
like, it might tell u that technique 1 needs 400ms and technique 2 needs 5ms. so u conclude technique 2 must be better.
but in reality, u measured 99% side effects like garbage collection and a trillion other things that go on in a VM in the background
so technique 1 was better after all
u cant know
any timing measurements are literally garbage
unless u make them with JMH
What is JMH?
a framework to benchmark java code