---<Code begin>
Option Explicit
Dim oIE, oIEDocPW, sScript, i, saResults(29)
Set oIE=CreateObject("InternetExplorer.Application")
With oIE
.AddressBar = False
.StatusBar = False
.Left = 0
.Top = 0
.Height = 480
.Width = 320
.ToolBar = False
.MenuBar = False
.Visible = True
.navigate "about:blank"
Do Until .readyState = 4: wscript.sleep 100: Loop
End With
Set oIEDocPW = oIE.document.parentWindow
sScript = "var d, s = """";" & _
"var c = "":"";" & _
"d = new Date();" & _
"s += d.getHours() + c;" & _
"s += d.getMinutes() + c;" & _
"s += d.getSeconds() + c;" & _
"s += d.getMilliseconds();"
For i = 0 to 29
oIEDocPW.execScript sScript, "JScript"
saResults(i) = oIEDocPW.s
' WScript.Sleep 2
Next 'i
MsgBox Join(saResults, vbCrLf)
oIE.Quit
---<Code end>
Results:
7:43:28:702
7:43:28:722
7:43:28:742
7:43:28:762
7:43:28:772
7:43:28:802
7:43:28:812
7:43:28:832
7:43:28:842
7:43:28:862
7:43:28:882
7:43:28:902
7:43:28:922
7:43:28:932
7:43:28:952
7:43:28:962
7:43:28:982
7:43:29:2
7:43:29:22
7:43:29:112
7:43:29:182
7:43:29:272
7:43:29:322
7:43:29:332
7:43:29:352
7:43:29:362
7:43:29:382
7:43:29:403
7:43:29:423
7:43:29:463
I have found that the minimum time span of one trip through the loop
is 10 milliseconds, the maximum is 60 milliseconds and the average is
about 26 milliseconds on my 1.7 GHz Celeron computer.
Questions:
1. Does anyone have JScript code that will run faster that they can
share here?
2. Does anyone have JScript code that will left-pad the fields so
they line up nicely?
3. Does anyone have code for some other active scripting language, such
as Rex, Python, Perl, Ruby, Tcl, that can produce the timestamp faster?
4. Does anyone have a better method for getting a millisecond timestamp
in VBScript?
Thanks,
-Paul Randall
If you don't mind using a "third-party control", then there
are several "high-resolution timers" available, with source
code.
My favorite source is Steve McMahon:
http://www.vbaccelerator.com/home/vb/code/Libraries/HiResTimer/article.asp
Another is the CCRP (Common Controls Replacement Project),
a bunch of guys (no gals) who were so arrogant that they
thought that they could do a better job of coding controls
than microsoft, and generally succeeded. This is a link
to the CCRP high-resolution timer":
http://vb.mvps.org/tools/ccrpTmr/
If you google high-resolution timer, you will find several
more.
cheers, jw
____________________________________________________________
You got questions? WE GOT ANSWERS!!! ..(but,
no guarantee the answers will be applicable to the questions)
Although designed for server side scripts (ASP), it works fine with VBS scripts,
as well. It is simple to install (just register) and easy to use.
--
Crash
> Paul Randall wrote:
>> 3. Does anyone have code for some other active scripting language, such
>> as Rex, Python, Perl, Ruby, Tcl, that can produce the timestamp faster?
Hi, jw
I appreciate the info you posted.
I don't mind using a third party control. I read the info in the links you
gave me. Perhaps I was not methodical enough, but I didn't find a direct
way to download the controls and register them. I learned a lot from the
article.
Before digging through it again to see if I could just download and install
the controls, I read Crash's response. I quickly installed and tested the
control he pointed me to. I found that in a tight loop, I could query that
control for the elapsed time as many as 115 times per second, but that the
elapsed time changes only once every 10 seconds or so, which was kind of
hinted at in the article in your first link. I posted my test script and
results to Crash's response.
Can your favorite timer control yield a new elapsed time more often than
once every 10 seconds?
' Create an instance of Time Class
Set oTimer = WScript.CreateObject("ASPTime.Time")
' Start the stop-clock
oTimer.Start()
iPreviousTime = 0
sMsg = "TimeGap - LoopCount - Change in LoopCount" & vbCrLf
For i = 0 to 20000
oTimer.Stop
If oTimer.TimeGap <> iPreviousTime Then
sMsg = sMsg & oTimer.TimeGap & vbTab & i & vbTab & _
i - iPreviousLoopCount & vbCrLf
iPreviousTime = oTimer.TimeGap
iPreviousLoopCount = i
End If
Next 'i
MsgBox sMsg
-------<End Code>
Results:
TimeGap - LoopCount - Change in LoopCount
10 180 180
20 922 742
30 1838 916
40 2868 1030
50 3694 826
60 4762 1068
70 5685 923
80 6839 1154
90 7840 1001
101 8544 704
111 9502 958
121 10331 829
131 11182 851
141 12322 1140
151 13300 978
161 14470 1170
171 15381 911
181 16522 1141
191 17439 917
201 18542 1103
211 19470 928
As you can see, the timer's timegap property changes every 10 milliseconds
or so, even though I checked it as many as 115 times per millisecond.
For my purposes, this is much better than running JScript in an IE window,
but I would still like find a way to quickly read the time to a one
millisecond resolution.
"Paul Randall" <paul...@cableone.net> wrote in message news:%> Before
>I have a fairly complex VBSscript to which I would like to
>add a time stamping capability, accurate to milliseconds
>or a few tens of milliseconds. Since VBScript can't do milliseconds,
VBScript can do tens of milliseconds, more or less. Use Timer and
convert to H M S.ss by ordinary code. See
<URL:http://www.merlyn.demon.co.uk/vb-date1.htm#Tim>.
I guess that Timer and new Date() have the same update interval
on any particular system. I expect 54.9 ms in Win 98 & 15.625 ms in Win
XP. See
<URL:http://www.merlyn.demon.co.uk/js-dates.htm#Ress>.
>I'm trying a JScript routine which I invoke by hosting it in an Internet
>Explorer window.
> ...
>Questions:
>1. Does anyone have JScript code that will run faster that they can
>share here?
Pure javascript *might* do better.
>2. Does anyone have JScript code that will left-pad the fields so
>they line up nicely?
Yes.
>4. Does anyone have a better method for getting a millisecond timestamp
>in VBScript?
If Timer is good enough, use it.
It's a good idea to read news:comp.lang.javascript and its FAQ. See below.
--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Strange. I cut and pasted your script into a file and ran it on my machine (W2K)
and got the following result:
TimeGap - LoopCount - Change in LoopCount
1 25 25
2 97 72
3 133 36
4 206 73
5 278 72
6 350 72
7 423 73
8 497 74
9 570 73
10 644 74
11 718 74
12 791 73
13 865 74
15 888 23
16 932 44
17 1005 73
18 1078 73
19 1152 74
20 1225 73
21 1298 73
22 1372 74
23 1443 71
24 1514 71
25 1587 73
26 1639 52
27 1711 72
28 1784 73
29 1857 73
30 1930 73
31 2003 73
32 2075 72
33 2148 73
34 2220 72
35 2293 73
36 2365 72
37 2438 73
38 2510 72
39 2581 71
40 2646 65
41 2718 72
42 2791 73
43 2863 72
44 2934 71
45 3006 72
46 3080 74
47 3153 73
48 3227 74
49 3301 74
50 3373 72
51 3446 73
52 3520 74
53 3593 73
54 3666 73
55 3740 74
56 3812 72
57 3886 74
58 3960 74
59 4033 73
60 4107 74
61 4181 74
62 4254 73
63 4328 74
64 4401 73
65 4475 74
66 4548 73
67 4621 73
68 4695 74
69 4769 74
70 4842 73
71 4916 74
72 4988 72
73 5062 74
74 5135 73
75 5209 74
76 5282 73
77 5356 74
78 5429 73
79 5501 72
80 5575 74
81 5648 73
82 5722 74
83 5795 73
84 5867 72
85 5941 74
86 6009 68
87 6082 73
88 6156 74
89 6228 72
90 6301 73
91 6374 73
92 6448 74
93 6521 73
94 6593 72
95 6667 74
96 6740 73
97 6813 73
98 6886 73
99 6959 73
100 7030 71
101 7104 74
102 7177 73
103 7250 73
104 7323 73
105 7395 72
106 7467 72
107 7540 73
108 7613 73
109 7686 73
110 7760 74
111 7833 73
112 7906 73
113 7978 72
114 8051 73
115 8124 73
116 8196 72
117 8270 74
118 8343 73
119 8416 73
120 8489 73
121 8562 73
122 8634 72
123 8707 73
124 8779 72
125 8852 73
126 8925 73
127 8998 73
128 9071 73
129 9144 73
130 9217 73
131 9289 72
132 9361 72
133 9434 73
134 9507 73
135 9580 73
136 9653 73
137 9726 73
138 9799 73
139 9833 34
140 9905 72
141 9977 72
142 10050 73
143 10122 72
144 10195 73
145 10268 73
146 10332 64
147 10404 72
148 10477 73
149 10550 73
150 10622 72
151 10693 71
152 10766 73
153 10838 72
154 10910 72
155 10981 71
156 11054 73
157 11127 73
158 11200 73
159 11272 72
160 11344 72
161 11417 73
162 11490 73
163 11562 72
164 11634 72
165 11707 73
166 11779 72
167 11852 73
168 11925 73
169 11998 73
170 12070 72
171 12143 73
172 12213 70
173 12285 72
174 12357 72
175 12429 72
176 12502 73
177 12574 72
178 12647 73
179 12720 73
180 12792 72
181 12865 73
182 12937 72
183 13010 73
184 13082 72
185 13155 73
186 13222 67
187 13295 73
188 13367 72
189 13437 70
190 13509 72
191 13581 72
192 13653 72
193 13726 73
194 13798 72
195 13870 72
196 13939 69
197 14012 73
198 14084 72
199 14156 72
200 14228 72
201 14299 71
202 14370 71
203 14442 72
204 14514 72
205 14585 71
206 14657 72
207 14729 72
208 14802 73
209 14874 72
210 14946 72
211 15018 72
212 15091 73
213 15163 72
214 15234 71
215 15307 73
216 15379 72
217 15451 72
218 15523 72
219 15594 71
220 15666 72
221 15738 72
222 15808 70
223 15880 72
224 15952 72
225 16023 71
226 16095 72
227 16168 73
228 16240 72
229 16312 72
230 16384 72
231 16456 72
232 16528 72
233 16600 72
234 16671 71
235 16743 72
236 16809 66
237 16880 71
238 16951 71
239 17023 72
240 17095 72
242 17113 18
243 17159 46
244 17231 72
245 17300 69
246 17358 58
247 17429 71
248 17497 68
249 17568 71
250 17640 72
251 17712 72
252 17780 68
253 17850 70
254 17921 71
255 17948 27
256 18018 70
257 18079 61
258 18145 66
259 18208 63
260 18278 70
261 18349 71
262 18415 66
263 18483 68
264 18551 68
265 18622 71
266 18690 68
267 18752 62
268 18817 65
269 18885 68
270 18956 71
271 19027 71
272 19097 70
273 19162 65
274 19220 58
275 19290 70
276 19358 68
277 19428 70
278 19492 64
279 19560 68
280 19630 70
281 19701 71
282 19773 72
283 19844 71
284 19913 69
285 19979 66
That is strange. I'm running WXP SP1 which was upgraded to SP2, with other
stuff added, in order to run the Vista Upgrade Advisor (which doesn't run
properly). I want your results :-(
-Paul Randall
What kind of stuff do you have running in the background? Any AV/AT stuff?
Whatever?
--
Dudley
Can you do me a favor? Go to http://www.merlyn.demon.co.uk/js-dates.htm,
about half way down the page, to a section titled [吞 Resolutions of new
Date(). It contains a table, at the end of which is a place to enter a loop
count and a test button which will display info for your computer. Try it a
few times and let me know what Updating Interval and Numerical Resolution it
displays.
On my system, (WXP, IE6) Updating Interval is 10 and Numerical Resolution is
1.
Thanks,
-Paul Randall
Thanks for the info and links. I'm impressed by your website. There I
found the link to http://www.lochan.org/2005/keith-cl/useful/win32time.html
especialy helpful. Strange that W2K/IE6 combination may provide 1ms
updating of the time in a VBScript run under WSH.
-Paul Randall
Same here (W2KSP4 IE6SP1).
--
Crash
-Paul Randall
I don't have an answer, but I have a clue, sort of. I am running a script on my
active desktop that uses setTimeout and setInterval. They are both supposed to
be able to resolve time to the millisecond, I think. I don't know how, but when
that script is running and has used the setTimeout function at least once, 1 ms
resolution is available for ASPTime. If I stop the script, it reverts to 10 ms.
--
Crash
-----sound.htm----------
<body>
<bgsound src="ding.wav" loop=0>
</body>
----------------------------
My other script includes a bgsound file, and that is what was enabling 1 ms
resolution, not setTimeout.
I have no idea what is going on, but try it and see what happens. (Be sure the
proper path to whatever sound file you choose is correct.)
--
Crash
-----sound.htm----------
<body>
<bgsound src="ding.wav" loop=0>
</body>
----------------------------
If I close the browser (or remove the wallpaper), I revert to 10ms.
I have no idea what is going on, but try it and see what happens.
--
Crash
Hi, Crash. Thank you for investigating this. Opening Sound.htm has the
same effect for me -- a truely one millisecond resolution. Here are my
results:
TimeGap - LoopCount - Change in LoopCount
1 5 5
2 122 117
3 202 80
4 318 116
5 432 114
6 546 114
7 660 114
16 761 101
17 869 108
18 989 120
19 1007 18
20 1107 100
21 1220 113
22 1334 114
23 1352 18
24 1462 110
26 1575 113
27 1652 77
34 1772 120
35 1815 43
36 1934 119
37 2047 113
38 2167 120
39 2281 114
41 2284 3
42 2387 103
43 2453 66
44 2496 43
45 2614 118
46 2730 116
52 2845 115
53 2942 97
54 3062 120
55 3165 103
56 3285 120
57 3400 115
58 3479 79
59 3592 113
60 3676 84
61 3787 111
62 3872 85
63 3983 111
64 4097 114
65 4216 119
66 4330 114
74 4432 102
75 4503 71
76 4609 106
77 4725 116
79 4827 102
81 4869 42
82 4870 1
83 4973 103
84 5087 114
85 5200 113
86 5313 113
91 5422 109
92 5454 32
93 5564 110
94 5676 112
95 5793 117
96 5907 114
97 6008 101
100 6119 111
There are only 65 results in 100 milliseconds, but that's probably because
my computer was a little busy. At the time, I had 14 IE windows open
(Sound.htm was the most recent I had opened), as well as two HTAs, Outlook
Express, windows task manager, 12 Notepad windows, PSPad (free text editor
with script & other language highlighting), and 16 folder explorer windows
open.
While browsing the links posted by Dr J R Stockton I noticed some references
to the fact that sound on the computer would not sound good if less than 1
millisecond timer capability were achieved. So I'm thinking that <bgsound
src="C:\WINDOWS\Media\ding.wav" loop=0> invokes a better timer than the
default timer. I don't know how much overhead this fast timer causes, but I
sure would like to be able to get to it directly through VBScript.
I just noticed that my CPU usage is running at about 90%. Closing the
Sound.htm window reduced it to 60%. Task Manager shows I've got 9
WScript.exe's running too, using maybe 20 to 30%. Time to clean up and
reboot.
-Paul Randall
Please do not (top-post,) over-quote, or quote sigs. Where did you find
the "lochan" link?
In message <#TklhkT$GHA....@TK2MSFTNGP05.phx.gbl>, Tue, 31 Oct 2006
15:09:14, Paul Randall <paul...@cableone.net> writes
>
>On my system, (WXP, IE6) Updating Interval is 10 and Numerical
>Resolution is
>1.
In <URL:http://www.merlyn.demon.co.uk/js-date0.htm#Ress>, with WinXP pro
sp2 IE6, I get 15.625 and 1. The page is slightly changed; default
count is now 40.
You are right. Its been 3 weeks since I booted my computer.
Freshly booted system - CPU usage runs 1% to 4% with occasional spikes to
7%.
TimeGap - LoopCount - Change in LoopCount
10 926 926
20 2097 1171
30 3256 1159
40 4441 1185
50 5692 1251
60 6840 1148
70 8053 1213
80 9199 1146
90 10418 1219
100 11561 1143
110 12814 1253
120 13992 1178
130 15222 1230
140 16372 1150
150 17578 1206
160 18714 1136
170 19927 1213
Opened Sound.htm and minimized the window. CPU usage runs 2% to 6% with
occasional spikes to 9%. Ran the ASPTimer routine. No missed timegaps from
1 to 100, but later there were some, with a big gap from 187 to 215.
Closed Sound.htm. CPU usage runs 1% to 4% with occasional spikes to 7%.
Ran the ASPTimer routine. Timegap interval is back to 10.
So I'd say the 1-ms timer might be eating up 1 or 2% of my cpu cycles. What
Microsoft newsgroup might have info on forcing a timer to be the default?
Thanks for the HTML file.
-Paul Randall
I think those gaps are unavoidable. The system times out periodically to set the
clock and do other things. That is, after all, what the timer is for.
> So I'd say the 1-ms timer might be eating up 1 or 2% of my cpu cycles. What
> Microsoft newsgroup might have info on forcing a timer to be the default?
I haven't a clue. I just stumbled on it while trying to help you solve your
problem. You could always do it the way I do it. Take the sound.htm file, add a
pretty background, and make it your desktop wallpaper. It'll just ding once when
you boot up.
--
Crash
I've found that there is only one system timer common to all applications.
By default, on NT type systems, it has a 10 millisecond interval. Any
application can increase the resolution (lower the interval) to as much as 1
millisecond; that resolution reverts to the previous resolution when the
application ends. Later applications can request to change the timer
resolution, but only requests of higher than the current resolution are
honored.
Your solution of running a sound file in the desktop wallpaper seems like a
very good solution. Thanks.
-Paul Randall