On WSH (Windows Scripting Host) environment, you can use VBScript or JScript (Microsoft’s implementation of Javascript). Both languages are supported and shipped on every Windows versions since Windows 98. On Win 95, users have to manually install WSH to use these two scripting languages.
Both scripting languages are perfect for Windows Administrative Tasks, and they are still good for many many tasks. But are they equally performing? Let’s take a comparison by implementing the Sieve of Eratosthenes’s Prime Finding algorithms.
VBScript Implementation
Instead of output the whole prime list, we concatenate the numbers into string and output its length, so the timing comparison will not be affected by console echo.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | Const N = 1000000 ReDim Primes(N) ' VBScript holds 1000000+1 elements Primes(0) = False Primes(1) = False For i = 2 To N Primes(i) = True Next M = Sqr(N) For j = 2 To M If Primes(j) Then i = j * j While i <= N ' Filtering out non-prime numbers Primes(i) = False i = i + j Wend End If Next S = "" For i = 0 To N If Primes(i) Then ' concatenate numbers into strings S = S & i & ", " End If Next ' Just output the length WScript.Echo Len(s) |
Const N = 1000000 ReDim Primes(N) ' VBScript holds 1000000+1 elements Primes(0) = False Primes(1) = False For i = 2 To N Primes(i) = True Next M = Sqr(N) For j = 2 To M If Primes(j) Then i = j * j While i <= N ' Filtering out non-prime numbers Primes(i) = False i = i + j Wend End If Next S = "" For i = 0 To N If Primes(i) Then ' concatenate numbers into strings S = S & i & ", " End If Next ' Just output the length WScript.Echo Len(s)
JScript Implementation
The equivalent JScript implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | (function() { var N = 1000000 + 1; var Primes = new Array(N); Primes[0] = false; Primes[1] = false; for (var i = 2; i < N; i ++) { Primes[i] = true; } var M = Math.sqrt(N); for (var j = 2; j <= M; j ++) { if (Primes[j]) { var i = j * j; for (; i < N; i += j) { Primes[i] = false; } } } var s = ""; for (var i = 0; i < N; i ++) { if (Primes[i]) { s += i + ", "; } } WScript.Echo(s.length); // output length of string to validate the result })(); |
(function() { var N = 1000000 + 1; var Primes = new Array(N); Primes[0] = false; Primes[1] = false; for (var i = 2; i < N; i ++) { Primes[i] = true; } var M = Math.sqrt(N); for (var j = 2; j <= M; j ++) { if (Primes[j]) { var i = j * j; for (; i < N; i += j) { Primes[i] = false; } } } var s = ""; for (var i = 0; i < N; i ++) { if (Primes[i]) { s += i + ", "; } } WScript.Echo(s.length); // output length of string to validate the result })();
Timing Comparison
We use the small utility timeit to measure the performance of both implementation by using the same algorithm.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | HP@HP-PC D:\ > timeit "cscript.exe primes.js" Microsoft (R) Windows Script Host Version 5.812 Copyright (C) Microsoft Corporation. All rights reserved. 616966 2015 HP@HP-PC D:\ > timeit "cscript.exe primes.vbs" Microsoft (R) Windows Script Host Version 5.812 Copyright (C) Microsoft Corporation. All rights reserved. 616966 46828 |
HP@HP-PC D:\ > timeit "cscript.exe primes.js" Microsoft (R) Windows Script Host Version 5.812 Copyright (C) Microsoft Corporation. All rights reserved. 616966 2015 HP@HP-PC D:\ > timeit "cscript.exe primes.vbs" Microsoft (R) Windows Script Host Version 5.812 Copyright (C) Microsoft Corporation. All rights reserved. 616966 46828
So, we can see that JScript is a lot faster (20 times faster) than its equivalent VBScript implementation. It does makes sense because the VBScript (latest version is 5.8 on Windows 10) is not updated (no new features added) since 2000.
This is the Microsoft’s Implementation of JScript, not to mention the Google v8 Javascript engine, which is expected to be a lot faster. But the point of this article is to measure two different scripting languages under the same environment, which is Windows Scripting Host.
Is VBScript Dead? what do you think?
–EOF (Coding For Speed) —
loading...
Next Post: Performance Comparison between PHP7.0.11 and PHP5.6.23 on Two VPS
Thx for sharing.
I just think that the form of writing in Visual Basic is much ugly. Yes, the decision is aesthetic.
And that’s why I write in JScript.
cheers
Yes, VBScript doesn’t have advanced/elegant programming syntax, but it is sufficient in some small, windows admin tasks.
This is a flawed example which just points at the fact that one needs to think about string concatenation when handling lots of concatenations. As a developer should know, string concats create new string objects in memory. So if one needs to do lots of additions one should use a stringbuilder class or similar. Apparently JS handles this internally. In VBScript one can use a ADODB.Stream object for this. Anyway, such a specific test is useless when comparing two languages. This one was comparing only lots of string concatenations without optimization or thinking about implementation.
When adjusted the result on my vm is plus/minus
JScript: 812 ms
VBScript: 969 ms
Code:
Option Explicit
Dim t1, t2, primes, m, i, j, s, a
t1 = Timer
Const N = 1000000
ReDim Primes(N) ‘ VBScript holds 1000000+1 elements
Primes(0) = False
Primes(1) = False
For i = 2 To N
Primes(i) = True
Next
‘WScript.Echo Timer – t1
M = Sqr(N)
For j = 2 To M
If Primes(j) Then
i = j * j
a = N+1
Do While i < a ' Filtering out non-prime numbers
Primes(i) = False
i = i + j
Loop
End If
Next
'WScript.Echo Timer – t1
Dim utf8s
Set utf8s = CreateObject("ADODB.Stream")
utf8s.Open
S = ""
For i = 0 To N
If Primes(i) Then ' concatenate numbers into strings
utf8s.WriteText i & ", "
'S = S & i & ", "
End If
Next
utf8s.Position = 0
s = utf8s.ReadText
' Just output the length
WScript.Echo Len(s)
'WScript.Echo Timer – t1
Btw if one wants to do without adodb.stream, one can use a buffer string and append to that shorter one until it reaches a specific length or iterations. This also circumvents making many many many string copies of a large string at each concat.
Quick example (result is 1031 ms)
S = “”
Dim buf
buf = “”
For i = 0 To N
If Primes(i) Then ‘ concatenate numbers into strings
If Len(buf) > 1000 Then
s = s & buf
buf = “”
End If
buf = buf & i & “, ”
End If
Next
s = s & buf
About the looks… JavaScript is just ugly imho (but PowerShell triumphs JavaScript in uglyness and unforgiveness ;)) and misses stuff like option explicit and seamless enumerators. VBScript looks friendlier to me and handles COM-objects cleaner. Anyway, one can even use them both in the same project. JScript that calls VBScript functions and viseversa in a wsh file. HTA (‘html applications’) is also a forgotten beauty for sysadmins/devs. A pitty the don’t get the love from MS. But it is stable proven technology and ‘just works and runs’. MS is all into ,NET
Thank you. You have make your point quite clear. And it is good to know the performance hits in VBScript is the string concatenation. But JScript is still slightly faster.
Since some of us have extensive experience with VBScript and less with JScript/Javascript, a few milliseconds isn’t going to make up for the time we’d waste not using VBScript.
It’s easier than all that! VBS has an specialized instruction to build big strings fast , just pass an array of primes and a separator to join()!