Stavros_S
Stavros_S

Reputation: 2235

Optimizing a functions performance using a loops wall-time

I use the following function below to update and call a drawAccel();function that builds out an animated strip chart.

function messagecb(header, message) {
    if(header.type == 6) {
       // processEchoReply(message);

   } else if(header.type == 4) {
        // accel
        var accels = message.b64UnpackAccelMsg();

        for(var index = 0; index < accels.length; ++index) {
            var accel = accels[index];
            var totalClock = accelEpochAdjust(accel.clock);

            addAccelDatum(totalClock, accel.x,  accel.y, accel.z);

        }

    if ( typeof messagecb.counter == 'undefined' ) {
      messagecb.counter = 0;
    }

    ++messagecb.counter;
    if (messagecb.counter % 10 === 0) {
      drawAccel();
    }

    } else if(header.type == 3) {
       // info
       var info2 = message.b64UnpackInfo2Msg();

       displayCurrentPosition(info2.fixtime, info2.lat, info2.lon, info2.alt);
       displayMobileStatus(info2.rssi, info2.bandClass, info2.batt);

    } else if(header.type == 11) {
        btReceive(header, message);
}

}

I come across some intermittent performance issues in IE8 though. So I would like to collect the elapsed wall time running inside the update for loop, and not call the drawAccel() renderer unless I'm using less than 50% of the wall time.

Pseudo code example:

  if ((lastEnteredTime - lastExitedTime)/(currentTime - lastEnteredTime) < .5){
       drawAccel();
  } else {
   //do nothing 
  }

My problem is I'm not sure how I can go about getting the last entered time and the last exited time of the loop so that I can run this condition. Any ideas? Thanks!

Upvotes: 1

Views: 88

Answers (1)

Adam Rackis
Adam Rackis

Reputation: 83376

It's not clear to me exactly what you're trying to do, but something like this should get you close. +new Date() will give you the number of milliseconds since 1/1/1970, so making that same call at various places should be able to get you what you want

var start = +new Date();

for(var index = 0; index < accels.length; ++index) {
    var accel = accels[index];
    var totalClock = accelEpochAdjust(accel.clock);

    var current = +new Date();
    var timeElapsedInMs = current - start;
    //not sure the exact test you want to run here

    addAccelDatum(totalClock, accel.x,  accel.y, accel.z);
}

Edit based on your comment. So if you always want to have a lastEntered and lastExited values, something like this might be what you want

var lastEntered, lastExisted = +new Date();

for(var index = 0; index < accels.length; ++index) {
    lastEntered = +new Date();

    var accel = accels[index];
    var totalClock = accelEpochAdjust(accel.clock);

    var timeElapsedInMs = current - start;
    //not sure the exact test you want to run here

    addAccelDatum(totalClock, accel.x,  accel.y, accel.z);

    lastExisted = +new Date();
}

And from there you can do whatever comparisons you need.

Upvotes: 1

Related Questions