Measuring recurring elapsed time

R

Ross

I'm a newbie at this, and have searched a lot but can't find something
that seems appropriate for measuring a recurring elapsed time.

Creating an object with: var mydate = new Date(); seems clear, as is
creating a second: nowTime = new Date(); and getting a difference
between the two.

My quesiton is what if you have many, maybe thousands of intervals you
wish to measure? Should I do: nowTime = new Date(); with every pass
of a loop, maybe thousands of times?


For example, if I wish download thousands of messages from a server and
report the average elapsed time, would I create a new Date() object
every time through a loop? Would you constantly assign the same var to
new Date() objects?

If so, is that some issue in javascript, maybe creating a memory leak or
needing some sort of garbage collection?


Or, when I say myTime = new Date() it takes the current date. Is there
no reset method to tell that object to reset again, like...

myTime.ResetThis() //does something like this exist?


Thanks...
Ross.
 
E

Evertjan.

Ross wrote on 06 nov 2008 in comp.lang.javascript:
I'm a newbie at this, and have searched a lot but can't find something
that seems appropriate for measuring a recurring elapsed time.

Creating an object with: var mydate = new Date(); seems clear, as is
creating a second: nowTime = new Date(); and getting a difference
between the two.

My quesiton is what if you have many, maybe thousands of intervals you
wish to measure? Should I do: nowTime = new Date(); with every pass
of a loop, maybe thousands of times?


For example, if I wish download thousands of messages from a server and
report the average elapsed time, would I create a new Date() object
every time through a loop? Would you constantly assign the same var to
new Date() objects?

Just take the total time, and divide it by the number of messages.

If so, is that some issue in javascript, maybe creating a memory leak or
needing some sort of garbage collection?


Or, when I say myTime = new Date()

Yu first have to define ["var"] the variable:

var myTime = new Date();
it takes the current date. Is there
no reset method to tell that object to reset again, like...

myTime.ResetThis() //does something like this exist?

No.

myTime is not a time, but a variable name pointing to a value that is of
the variant type "time".
There is no sense in "resetting" a pointer-to-a-value but to another
value.

Just setting it to something else changes the pointer:

myTime = undefined;

or

myTime = 'Hello world';
 
R

RobG

I'm a newbie at this, and have searched a lot but can't find something
that seems appropriate for measuring a recurring elapsed time.

You will need to create your own stop watch or clock object to do
that.

Creating an object with: var mydate = new Date();  seems clear, as is
creating a second:  nowTime = new Date(); and getting a difference
between the two.

So the next step is to create an object wtih start, stop, lap and
reset methods.


My quesiton is what if you have many, maybe thousands of intervals you
wish to measure?  Should I do:  nowTime = new Date();  with everypass
of a loop, maybe thousands of times?

It depends. If you are just measuring simple elapsed time intervals,
that's all you need.

For example, if I wish download thousands of messages from a server and
report the average elapsed time, would I create a new Date() object
every time through a loop? Would you constantly assign the same var to
new Date() objects?

No, you'd need one variable to remember the start time, you may not
need to remember the second, e.g.:

var startTime = new Date();
// do stuff
var elapsedTime = new Date() - startTime; // Elapsed time in
milliseconds.


The elapsed time variable may not be necessary depending on what you
need the value for.

If so, is that some issue in javascript, maybe creating a memory leak or
needing some sort of garbage collection?

Not that I know of (but that's not saying much). The most infamous
leak regards IE, cicrular references involving DOM objects and
closures - avoidance and attenuation strategies are well known if that
becomes an issue.

Or, when I say myTime = new Date() it takes the current date.  Is there
no reset method to tell that object to reset again, like...

    myTime.ResetThis()          //does something like this exist?

If you build your own myTime object, you can give it whatever methods
you think it needs. Otherwise, go to system preferences and change
the clock... :)

The built-in Date object is defined in ECMA-262, implementations will
likely follow that pretty closely.

The following should get you started, it's written as a single object
but it could be turned into a constructor pretty easily so you could
have multiple stopwatches running at once:

var stopWatch = (function() {

var startTime;
var elapsedTime = 0;
var running = false;

return {
start: function () {
if (!running) {
startTime = new Date();
running = true;
}
},
stop: function() {
if (running) {
elapsedTime += new Date() - startTime;
startTime = null;
running = false;
}
return elapsedTime;
},
lap: function() {
if (running) {
return elapsedTime + (new Date() - startTime);
} else {
return elapsedTime;
}
},
reset: function () {
var t = this.lap();
startTime = null;
elapsedTime = 0;
running = false;
return t;
}
}
})();
 
R

Ross

Evertjan. said:
Ross wrote on 06 nov 2008 in comp.lang.javascript:


Just take the total time, and divide it by the number of messages.

lol - Ok it was just an example - think instead building a histogram of
delays.
If so, is that some issue in javascript, maybe creating a memory leak or
needing some sort of garbage collection?


Or, when I say myTime = new Date()

Yu first have to define ["var"] the variable:

var myTime = new Date();
it takes the current date. Is there
no reset method to tell that object to reset again, like...

myTime.ResetThis() //does something like this exist?

No.

myTime is not a time, but a variable name pointing to a value that is of
the variant type "time".
There is no sense in "resetting" a pointer-to-a-value but to another
value.

Just setting it to something else changes the pointer:

myTime = undefined;

or

myTime = 'Hello world';

Thanks for the suggestions.
 
R

Ross

RobG said:
You will need to create your own stop watch or clock object to do
that.



So the next step is to create an object wtih start, stop, lap and
reset methods.




It depends. If you are just measuring simple elapsed time intervals,
that's all you need.

I've implemented it now, and it seems to work well. I'm grabbing events
which need to be accurately sync'd to the mS, and nothings blown up yet,
so I assume it's working okay. I'll have to look around for a profiler
of some kind to get a sense of what resources I'm using.
No, you'd need one variable to remember the start time, you may not
need to remember the second, e.g.:

var startTime = new Date();
// do stuff
var elapsedTime = new Date() - startTime; // Elapsed time in
milliseconds.


The elapsed time variable may not be necessary depending on what you
need the value for.

That looks about right. Thx that's reassuring.

Not that I know of (but that's not saying much). The most infamous
leak regards IE, cicrular references involving DOM objects and
closures - avoidance and attenuation strategies are well known if that
becomes an issue.



If you build your own myTime object, you can give it whatever methods
you think it needs. Otherwise, go to system preferences and change
the clock... :)

The built-in Date object is defined in ECMA-262, implementations will
likely follow that pretty closely.

The following should get you started, it's written as a single object
but it could be turned into a constructor pretty easily so you could
have multiple stopwatches running at once:

var stopWatch = (function() {

var startTime;
var elapsedTime = 0;
var running = false;

return {
start: function () {
if (!running) {
startTime = new Date();
running = true;
}
},
stop: function() {
if (running) {
elapsedTime += new Date() - startTime;
startTime = null;
running = false;
}
return elapsedTime;
},
lap: function() {
if (running) {
return elapsedTime + (new Date() - startTime);
} else {
return elapsedTime;
}
},
reset: function () {
var t = this.lap();
startTime = null;
elapsedTime = 0;
running = false;
return t;
}
}
})();

Thanks Rob, that all sounds good, and it's working well for me.

Ross.
 
E

Evertjan.

Ross wrote on 08 nov 2008 in comp.lang.javascript:
lol - Ok it was just an example - think instead building a histogram of
delays.

Even if you do not have consecutive times, but just numbers coming in now
and then, you do not need to save them all to calculate the average.

var average = 0; // start value unimpotrant
var count = 0;

function calcul(nextNumber) {
average = (average * count + nextNumber) / ++count;
};

// testing:
calcul(2);
calcul(4);
calcul(2);
calcul(4);
calcul(2);
calcul(4);
alert(average); // 3.
 
L

Lasse Reichstein Nielsen

Evertjan. said:
var average = 0; // start value unimpotrant
var count = 0;

function calcul(nextNumber) {
average = (average * count + nextNumber) / ++count;
};

While a great idea in some cases, this way of doing it can cause problems
with rounding.
It might be better to keep the sum and the count, and then calculate
the everage (by dividing) when it's needed. That might still risk
overflowing the sum, but "average * count" above will do the same.

---
function Averager() {
this.sum = 0;
this.count = 0;
}
Averager.prototype.add = function add(n) {
this.sum += n;
this.count++;
};
Averager.prototype.average = function average() {
return this.sum / this.count;
}

var avg = new Averager();
avg.add(2);
avg.add(4);
avg.add(2);
avg.add(4);
avg.add(2);
avg.add(4);
alert(avg.average()); // 3.
 
E

Evertjan.

Lasse Reichstein Nielsen wrote on 09 nov 2008 in comp.lang.javascript:
While a great idea in some cases, this way of doing it can cause problems
with rounding.
It might be better to keep the sum and the count, and then calculate
the everage (by dividing) when it's needed. That might still risk
overflowing the sum, but "average * count" above will do the same.

As there is no specific rounding in the formula:

Are you sure that the precision overflowing ["rounding|?] of a floating
point division is worse than the precision overflowing of the floating sum?

The sum can also have exponent overflowing.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
The sum can also have exponent overflowing.

The age of the Universe is currently about 13e9 * 365 * 864e5
milliseconds, say 4.1e20 ms.

JavaScript Numbers can go up to about 1.7e308.

Exponent overflow in JavaScript time sums seems not immediately likely.
 
E

Evertjan.

Dr J R Stockton wrote on 09 nov 2008 in comp.lang.javascript:
In comp.lang.javascript message <[email protected]>


The age of the Universe is currently about 13e9 * 365 * 864e5
milliseconds, say 4.1e20 ms.

JavaScript Numbers can go up to about 1.7e308.

Exponent overflow in JavaScript time sums seems not immediately likely.

You are right in the case of time measurement as in the OQ, John,
but the technique of running avarage has other uses,
that perhaps could.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top