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. 9 3214
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)
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
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.
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.
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)
"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.'
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)
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.
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) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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()'...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
| |