Is it possible to get timestamps with fractional millisecond precision?

8

I have to calculate the runtime of a javascript function, I have my code ready and working, my only problem is the getMilliseconds() limit that only goes to 999 and the method execution is extremely fast, so it would need more homes.

Code of getTime() and tempoExecucao() : link

Would anyone know a solution to this?

    
asked by anonymous 26.03.2014 / 18:46

3 answers

0

Oops, thanks for the help guys, with the idea that you can formulate the following:

I got the with var tempoInicio = getTime(); the start of execution and with var tempoFim = getTime(); the end of it.

// Pega tempo
function getTime(){
    var horaInicio      = new Date();
    var minutos         = horaInicio.getMinutes();
    var segundos        = horaInicio.getSeconds();
    var milisegundos    = horaInicio.getMilliseconds();
    var arrTempo = [minutos, segundos, milisegundos];
    var tempo = arrTempo.join();
    return tempo;
}

With the 2 start and end data, send by parameter for the time treatment in the following function:

// Calcula Tempo de Execução
function tempoExecucao(tempoInicio, tempoFim){
    var tempoInicioCalc = tempoInicio.split(",");
    var tempoFinalCalc = tempoFim.split(",");
    // calculo do tempo
    var calcMinuto          = tempoFinalCalc[0]-tempoInicioCalc[0];
    var calcSegundo         = tempoFinalCalc[1]-tempoInicioCalc[1];
    var calcMilisegundos    = tempoFinalCalc[2]-tempoInicioCalc[2];
    // une em uma array
    var tempoCalc = [calcMinuto, calcSegundo, calcMilisegundos];
    // une em uma string
    tempoCalc.join();
    return tempoCalc;
}

But it will have a small problem in ms because the sum may give negative values, then implement this function to complement.

function trataTempoNegativo(trataTempo){
    if(trataTempo[2] < 0){
        var tratadoTempo= [];
        tratadoTempo[0] = trataTempo[0];
        tratadoTempo[1] = trataTempo[1];
        tratadoTempo[2] = trataTempo[2]+1000;
        return tratadoTempo;
    }
    return trataTempo;
}

Thank you once again!

    
10.04.2014 / 07:09
7

Performance timer

Yes, you can get timestamps with fractional milliseconds precision. Modern browsers let you use performance timers:

window.performance.now()

When measuring the start and end time with this method, you will have much more precision. The value returned is a float, which measures milliseconds, but has the fractional part, so it can accurately measure below milliseconds.

var inicio = window.performance.now();
// medindo o tempo do for, iterando 1 milhão de vezes
for (var i = 0; i < 1000000; i++){}
document.write(window.performance.now() - inicio);

example jsfiddle

Learn more about this feature

Previous answer

Why not use a full date / time. Take the start / end date / time and then subtract it:

var inicio = new Date().getTime();
//executar código
console.log(new Date().getTime() - inicio);

Now, if you want more precision, then the way you will execute the code being tested several times and then make a division:

var inicio = new Date().getTime();
for (var i = 0; i < 1000; i++)
{
    //executar código
}
console.log((new Date().getTime() - inicio) / 1000);

    
26.03.2014 / 18:51
1

Dude, I found this function that is an attempt to implement PHP's microtime in Javascript, try to take a look:

function microtime(get_as_float) {
  //  discuss at: http://phpjs.org/functions/microtime/
  // original by: Paulo Freitas
  //   example 1: timeStamp = microtime(true);
  //   example 1: timeStamp > 1000000000 && timeStamp < 2000000000
  //   returns 1: true

  var now = new Date()
    .getTime() / 1000;
  var s = parseInt(now, 10);

  return (get_as_float) ? now : (Math.round((now - s) * 1000) / 1000) + ' ' + s;
}

source: link

    
26.03.2014 / 18:52