rwniceing
asked on
subtract long number in javascript
Dear Experts,
I try to find out the running time for "for loop" , I guess probably it will be in milliseconds.
so I try to convert the new Date() into milli-second and subtracting it before and after the
loop code. but when I console.log(c-b); it will show wrong answer, why ?
How to define the b, c in double or long format for number and get the correct result of "c-b"
I expect it should be c-b=80 in milli seconds
Pleas advise
Rwniceing
Wrong console.log output from the following code
1- 1407193070584
2- 1407193070664
3- 1407166064013 //it is wrong I think
I try to find out the running time for "for loop" , I guess probably it will be in milliseconds.
so I try to convert the new Date() into milli-second and subtracting it before and after the
loop code. but when I console.log(c-b); it will show wrong answer, why ?
How to define the b, c in double or long format for number and get the correct result of "c-b"
I expect it should be c-b=80 in milli seconds
Pleas advise
Rwniceing
Wrong console.log output from the following code
1- 1407193070584
2- 1407193070664
3- 1407166064013 //it is wrong I think
var i, a, b, c, max;
max = 2e7;
var d = Date.now();
var a = new Date();
var y= a.getFullYear();
var m=a.getMonth();
var d=a.getDay();
var b = Date.UTC(y,m,d,a.getHours(),a.getMinutes(),a.getSeconds(),a.getMilliseconds());
console.log("1-",b);
for (i = 0; i < max; i++) {
a = 1234 + 5678 + i;
b = 1234 * 5678 + i;
c = 1234 / 2 + i;
}
var a = new Date();
var y= a.getFullYear();
var m=a.getMonth();
var d=a.getDay();
var c = Date.UTC(y,m,d,a.getHours(),a.getMinutes(),a.getSeconds(),a.getMilliseconds());
console.log("2-",c);
console.log("3-",c-b);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This is kind of simplified, but it seemed to work OK. Only tested for very short intervals, and it probably would need at least the addition of minutes to achieve greater reliability. That said, using the confirm() and clicking it quickly I was able to get down to about 900ms.
http://iconoun.com/demo/temp_rwniceing.php
http://iconoun.com/demo/temp_rwniceing.php
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<!-- SEE http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28507319.html -->
<meta charset="utf-8" />
<meta name="robots" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML5 Page with Simple JavaScript Elapsed Time (ms) Calculation</title>
</head>
<body>
<script>
var ad = new Date();
var am = ad.getMilliseconds();
var as = ad.getSeconds();
var xx = confirm('Continue?');
var zd = new Date();
var zm = zd.getMilliseconds();
var zs = zd.getSeconds();
var az = (zs * 1000 + zm) - (as * 1000 + am);
alert('This took ' + az + ' ms.');
</script>
</body>
</html>
ASKER
When I change those Date variable from a to a2 ,b to b2 and c to c2 in previous post code. it will echo out the correct result on "c2-b2" without using .valueOf() function. I don;t know why ?
I have checked the previous code that is okay that there is no any variable conflict since a, b,c
variable in the for loop that doesn't affect the date() extraction.
Any expert could test those post code (previous on topic code and this post code as follows) to see what I say?
I have checked the previous code that is okay that there is no any variable conflict since a, b,c
variable in the for loop that doesn't affect the date() extraction.
Any expert could test those post code (previous on topic code and this post code as follows) to see what I say?
var i, a, b, c, max;
max = 2e7;
var d = Date.now();
var a2 = new Date();
var y= a2.getFullYear();
var m=a2.getMonth();
var d=a2.getDay();
var b2 = Date.UTC(y,m,d,a2.getHours(),a2.getMinutes(),a2.getSeconds(),a2.getMilliseconds());
console.log("1-",b2);
for (i = 0; i < max; i++) {
a = 1234 + 5678 + i;
b = 1234 * 5678 + i;
c = 1234 / 2 + i;
}
var a2 = new Date();
var y= a2.getFullYear();
var m=a2.getMonth();
var d=a2.getDay();
var c2 = Date.UTC(y,m,d,a2.getHours(),a2.getMinutes(),a2.getSeconds(),a2.getMilliseconds());
console.log("2-",c2);
console.log("3-",c2-b2);
ASKER
Sorry again, the topic post code is wrong since variable b
at var b = Date.UTC(y,m,d,a.getHours( ),a.getMin utes(),a.g etSeconds( ),a.getMil liseconds( ));
is overwritten by b in for loop so that came out the wrong output
Conclude again topic post code with the mistake corrected by changed b to b2 for date that works , and .valueOf() also works if variable assignment is correct
this post is solved and closed.
at var b = Date.UTC(y,m,d,a.getHours(
is overwritten by b in for loop so that came out the wrong output
Conclude again topic post code with the mistake corrected by changed b to b2 for date that works , and .valueOf() also works if variable assignment is correct
this post is solved and closed.
ASKER