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) —
Product Recommendations
- Make Your WordPress Fast Again! This WordPress is accelerated by WP Rocket Plugin!
- Free $10 Credit, when you sign up with Vultr Cloud VPS (4 Months Giveaway!)
- Free $10 Credit, when you sign up with Digital Ocean Cloud VPS (2 Months Giveaway!)
- Free $20 Credit, when you sign up with Linode Cloud VPS (4 Months Giveaway!) - (Coupon: PodCastInit2022)
- The easiest way to Buy and Sell Bitcoins/Litecoins via Wirex Visa Debit Card!
- Buy and Sell Bitcoins/Litecoins via Local Bitcoins