¿Cómo ejecutar la velocidad de cálculo en una clase usando currentTimeMillis()?

I'm trying to run this test - for computation speed - on this object class named Point with some methods in it. It takes three inputs/variables in its constructor Point(String, double, double).

I have this idea but I can't seem to put it in code: I want to create a random number between A and C using Math.random, put the number in a variable (for each of those three inputs) and use it to create an array of objects, then loop over the array creating new instances each time.

My code below is quite gibberish, but any help would be appreciated.

public class Test{
    long startTime = System.currentTimeMillis();
    long total = 0;
    for (int i = 0; i < 10000000; i++) {
        total += i;
        randomValue1 = (int)(Math.random()*10); 
        randomValue2 = (int)(Math.random()*10);
        Point[] instances = new Point[numberOfElements];
        for (int i=0; i<instances.length; i++) {
            instances[i] = new Point( ,randomValue1, randomValue2);
        }
    }
    long stopTime = System.currentTimeMillis();
    long elapsedTime = stopTime - startTime;
    System.out.println(elapsedTime);
}

preguntado el 22 de septiembre de 13 a las 22:09

Use nanoTime of currentTimeMillis, you'll also want to warm up the JIT by running the method(s) around 10000 times (as I understand it) to get better results -

Much of this code doesn't appear to be doing what you think. e.g. total isn't used for anything and wouldn't be useful if it was. The random values will be the same for every point. The Points are 2D so it not clear what you would use three values for. Most of the time will be spent creating objects and generating random values. -

1 Respuestas

No solo currentTimeMillis does not have enough resolution but also writing microbenchmarks by hand is tricky and error-prone due to the JIT's optimizations. You should consider using a microbenchmark framework such as Calibrar preferiblemente.

Respondido el 22 de Septiembre de 13 a las 22:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.