473,840 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ResetThi s() //does something like this exist?
Thanks...
Ross.
Nov 6 '08 #1
9 3326
Ross wrote on 06 nov 2008 in comp.lang.javas cript:
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.ResetThi s() //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.ijkw rote:
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.ResetThi s() * * * * *//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.javas cript:
>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.ResetThi s() //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.ijkw rote:
>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.ResetThi s() //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.javas cript:
>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(nextNumb er) {
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.net writes:
var average = 0; // start value unimpotrant
var count = 0;

function calcul(nextNumb er) {
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.protot ype.add = function add(n) {
this.sum += n;
this.count++;
};
Averager.protot ype.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.avera ge()); // 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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Nov 9 '08 #7
Lasse Reichstein Nielsen wrote on 09 nov 2008 in comp.lang.javas cript:
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.javas cript message <Xn************ ********@194.10 9.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.demo n.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.javas cript:
In comp.lang.javas cript message <Xn************ ********@194.10 9.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
12348
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 minutes 5 seconds". Any ideas? Thanks very much! function calcElapsedTime($time) { // calculate elapsed time (in seconds!) $diff = time()-$time;
5
3899
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 instance, user A creates an event that recurs every Tuesday, from 1/1/05 - 3/2/06. The developer has decided that it would be good to have another table that stores each individual recurring event in a separate record. His logic is that this will help...
5
4076
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 were added things began to slow down quite a bit. I've taken a different approach since, and decided to create a single record for each recurring appointment, but this has problems as well due to the fact that I have to search each time slot going...
3
12136
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
2431
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 appointments 1. Do I calculate and save all future appointments as table records at the time of creation of the Recurring appointment or .. 2. Do I just save the details and create the appointments in code whenever
74
7778
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 to output (time elapsed since program start would do just fine) - it should be as precise as possible, as there may be only differences in milliseconds between two measurements.
2
1765
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 accessed records 2) incorrectly linked records via primary and foreign keys (looking at the tables displays the correct key number but when filtered it links to a different number). 3) truncating of text entered in a memo field
12
16385
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()' function call for the purpose and regardless of how many time I invoke the function within my program, I get the same output!!! For better clarity consider the code snippet below. ... start_time = time(NULL);
11
2255
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 Can you check if I'm using the clock() function correctly? the function is finding the number in the array that is closest to a number given by the user. Here is the code for the pass by value function. Thanks for the help! relevant code:
0
9698
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10914
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10656
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7834
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5684
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4071
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.