#Want to know Runtime of a program:

1 messages · Page 1 of 1 (latest)

fallow widgetBOT
#

<@&987246746478460948> please have a look, thanks.

fathom hearth
#

Well what is the method supposed to do?

crude estuary
#

And the first one... doesn't return anything, so how can you say that it gives a result ?

plain remnant
plain remnant
fathom hearth
#

Ok, have you considered creating a variable int numberOfMultiplications = n * Math.log(2 * n)?

crude estuary
fathom hearth
#

Have you noticed that you're multiplying by 1?

crude estuary
#

ah and that too ↑

plain remnant
fathom hearth
#
  1. Can you send us the actual code you're running please, because it sounds quite different from your first message
  2. 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
plain remnant
#

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();
    }
}                                                      ```
fathom hearth
#

And add this VM argument when running your program: -Djava.compiler=NONE

plain remnant
#

Where in the program do I add

fathom hearth
#

Which IDE are you using?

warm marsh
#

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

plain remnant
warm marsh
#

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

warm marsh
#

a framework to benchmark java code