473,379 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Measuring recurring elapsed time

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.
Nov 6 '08 #1
9 3283
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';

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Nov 6 '08 #2
On Nov 7, 6:01*am, Ross <ab...@fgh.ijkwrote:
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;
}
}
})();

--
Rob
Nov 6 '08 #3
Evertjan. wrote:
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.
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.
Nov 8 '08 #4
RobG wrote:
On Nov 7, 6:01 am, Ross <ab...@fgh.ijkwrote:
>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 every pass
of a loop, maybe thousands of times?

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.
>
>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.
That looks about right. Thx that's reassuring.

>
>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;
}
}
})();

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

Ross.
Nov 8 '08 #5
Ross wrote on 08 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.
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.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Nov 8 '08 #6
"Evertjan." <ex**************@interxnl.netwrites:
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.
---

(It's much harder to find, e.g., the spread efficiently :)

/L
--
Lasse Reichstein Holst Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Nov 9 '08 #7
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.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Nov 9 '08 #8
In comp.lang.javascript message <Xn********************@194.109.133.242>
, Sun, 9 Nov 2008 18:41:57, Evertjan. <ex**************@interxnl.net>
posted:
>
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.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Nov 9 '08 #9
Dr J R Stockton wrote on 09 nov 2008 in comp.lang.javascript:
In comp.lang.javascript message <Xn********************@194.109.133.242>
, Sun, 9 Nov 2008 18:41:57, Evertjan. <ex**************@interxnl.net>
posted:
>>
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.
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.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Nov 9 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: NotGiven | last post by:
Below is a good elapsed time function I found. However, I'd like to return total seconds instead of broken down into days, hours, minutes & seconds. In other words, I want "125" instead of "2...
5
by: Shabam | last post by:
I'm having a calendar feature developed whereby users can add recurring events. These recurring events can have start and end dates, meaning they will recur only within a date range. For...
5
by: gil | last post by:
I initially tried building a coded system where numbers 1 through 10 referenced a certain type of recurring appointment, and would then call a specific function for each, but as more appointments...
3
by: Ramona van Riet | last post by:
In Delphi, I would use timeGetTime to measure the time something takes. But how can I do that in C#? -- Ramona
1
by: steve | last post by:
Hi All I am writing a program for a gymnasium for membership control It is the first time I have had to deal with appointment diaries and I want to know the best way to store recurring...
74
by: Dominik Wallner | last post by:
Hi! I'm currently implementing a program which measures voltages through an external USB-AD-converter. It should output those values as time/voltage pairs. My problem is to measure the time...
2
by: nepdae | last post by:
Please forgive me, this is a long one. My 11-user Access 2000 database is having recurring corruption problems. The symptoms include the following: 1) corrupted fields in recently created or...
12
by: Spitfire | last post by:
I've a requirement to find the elapsed time between two function calls. I need to find the time elapsed accurate to 1 millisecond. The problem I'm facing right now is that, I'm using the 'time()'...
11
by: blackx | last post by:
I'm using clock() to measure the speed of my code (testing the speed of passing by value vs passing by reference in function calls). The problem is, the speed returned by my code is always 0.0000000...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.