Is it possible to obtain timestamps with millisecond fractions accuracy?

Asked

Viewed 1,414 times

9

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

Code of getTime() and of tempoExecucao(): http://pastebin.com/P9Cv6wzt

Someone would know a solution to this?

3 answers

9

Performance timer

It is possible to obtain timestamps with millisecond fractions accuracy. Modern browsers allow you to use performance timers:

window.performance.now()

By measuring the initial and final time with this method, you will have much more precision. The returned value is a float, which measures milliseconds, but has the fractional part, so it allows measuring with precision 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);

jsfiddle of example

Learn more about this resource

1

Dude, I found this function which is an attempt to implement PHP 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: https://github.com/kvz/phpjs/blob/master/functions/datetime/microtime.js

0


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

I caught the with var tempoInicio = getTime(); the start of the 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 per parameter to the time processing 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 can give negative values, then implementing 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;
}

Thanks again!

  • But that doesn’t give you more precision than milliseconds! That’s not what you wanted?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.