Any of you coders know how to convert the following VBsript code to code that would function in Visual Basic console app?
--Script Begin ---
Dim goal
Dim before
Dim x
Dim y
Dim i
goal = 2181818
Do While True
before = Timer
For i = 0 to goal
x = 0.000001
y = sin(x)
y = y + 0.00001
Next
y = y + 0.01
WScript.Echo "I did three million sines in " & Int(Timer - before + 0.5) & " seconds!"
Loop
-Script End-
Thanks in advance.
Geob
Here you go:
I just made it a .net using VS 2010 express, set as a console applcation, compiled as .net 2.0, and made a few tweeks to make it run in .net.
It would be pretty nerdy fun to race the .vbs vs this .exe..
Now if you ask me to make this a multi-threaded app, Ill have to go ask my friends..
Zip file is also here:
Source code
-
>8----
Module Module1
Sub Main()
Console.Out.WriteLine(vbCrLf)
Console.Out.WriteLine("ported to VB .net by Jonathan Reininger VCP 3.5/4.0 5/19/2010")
Console.Out.WriteLine(vbCrLf)
Dim goal
Dim before
Dim x
Dim y
Dim i
y = 0
goal = 2181818
Do While True
before = Timer
For i = 0 To goal
x = 0.000001
'y = sin(x) 'from orig .vbs version
y = System.Math.Sin(x)
y = y + 0.00001
Next
y = y + 0.01
Console.Out.WriteLine("I did three million sines in " & Timer - before & " seconds!")
Loop
End Sub
I took it one step further, and made it multi-threaded. I compiled this in VB 2010, and it worked great. I then set a batch file up to run from VMware Tools at start up. It will peg a 2 Proc VM.
-
Begine Code -
#Region " Imports "
Imports System.Threading
#End Region
#Region " Module Module1 "
Module Module1
#Region " Sub Main "
Sub Main()
Try
Dim tmpWorker1 As New Thread(AddressOf Worker_Thread_1)
Dim tmpWorker2 As New Thread(AddressOf Worker_Thread_2)
tmpWorker1.Name = "Worker Thread # 1"
tmpWorker2.Name = "Worker Thread # 2"
tmpWorker1.Priority = ThreadPriority.Normal
tmpWorker2.Priority = ThreadPriority.Normal
'tmpWorker1.IsBackground = True
'tmpWorker2.IsBackground = True
tmpWorker1.Start()
tmpWorker2.Start()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
#End Region
#Region " Private Sub Worker_Thread_1 "
Private Sub Worker_Thread_1()
Try
Dim goal As Long
Dim x As Long
Dim y As Long
Dim i As Long
goal = 2181818
Do While True
Dim BeginTime As DateTime = Now
For i = 0 To goal
x = 0.000001
y = Math.Sin(x)
y = y + 0.00001
Next
y = y + 0.01
Dim Span As TimeSpan = DateTime.Now.Subtract(BeginTime)
Console.WriteLine("CPUWorkOut(Worker1) processed three million sines in " & Span.Milliseconds & " milliseconds!")
Loop
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
#End Region
#Region " Private Sub Worker_Thread_2 "
Private Sub Worker_Thread_2()
Try
Dim goal As Long
Dim x As Long
Dim y As Long
Dim i As Long
goal = 2181818
Do While True
Dim BeginTime As DateTime = Now
For i = 0 To goal
x = 0.000001
y = Math.Sin(x)
y = y + 0.00001
Next
y = y + 0.01
Dim Span As TimeSpan = DateTime.Now.Subtract(BeginTime)
Console.WriteLine("CPUWorkOut(Worker2) processed three million sines in " & Span.Milliseconds & " milliseconds!")
Loop
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
#End Region
End Module
#End Region
-
End Code -
Dang just 3 min of FAME for me.... Thanks for posting, great way for me to learn MT app creation..
Next 'we' should pass command line attributes into the .exe so 'all of use' can pick how many vCPUs we want to peg.. or maybe a 'max' value to peg all the vCPUS..
I did time them for me the .exe is about ~2.5x faster than the .vbs ( ~0.5 sec on the .exe vs ~1.3 sec for the .vbs)
You still did a fine job. Im no coder, but it looks like what i did works great.
And yes you could pass a commandline option, and the use a select case statement to kick off multiple threads.:D
Kudos to jreininger and geob! This thread was on point for me this week.
A few tidbits for anyone that may follow.
Code: I needed to remove the Begin/End Region statements to get this to compile.
Compiler
[Microsoft Windows 10 Build 1909]
The command-line Visual Basic compiler, when .NET is installed, lives at C:\Windows\Microsoft.NET\<Framework>\<.NETVersion>\vbc.exe
where <Framework> is one of:
• Framework
• Framework64
where <.NETVerison> is one of:
• v1.0.3705
• v1.1.4322
• v1.1.4322
• v2.0.50727
• v3.0
• v3.5
• v4.0.30319
https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/versions-and-dependencies
Compilation
C:\Users\rdkoziel\Publish>dir
Directory of C:\Users\rdkoziel\Publish
12/31/2019 10:04 AM <DIR> .
12/31/2019 10:04 AM <DIR> ..
12/31/2019 09:41 AM 2,589 cpubusymt.vb
1 File(s) 2,589 bytes
2 Dir(s) 742,713,499,648 bytes free
C:\Users\rdkoziel\Publish>c:\Windows\Microsoft.NET\Framework64\v4.0.30319\vbc.exe cpubusymt.vb /out:cpubusymt.exe /target:exe
Microsoft (R) Visual Basic Compiler version 14.8.3752
for Visual Basic 2012
Copyright (c) Microsoft Corporation. All rights reserved.
This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to Visual Basic 2012, which is no longer the latest version. For compilers that support newer versions of the Visual Basic programming language, see http://go.microsoft.com/fwlink/?LinkID=533241
C:\Users\rdkoziel\Publish>dir
12/31/2019 10:06 AM <DIR> .
12/31/2019 10:06 AM <DIR> ..
12/31/2019 10:06 AM 8,192 cpubusymt.exe
12/31/2019 09:41 AM 2,589 cpubusymt.vb
2 File(s) 10,781 bytes
2 Dir(s) 742,713,257,984 bytes free
C:\Users\rdkoziel\Publish>
• Note that the source code filename is the first argument after the path to vbc.exe
• /out: specifies the resulting filename
• /target:exe specifies the executable type; in this case, a console application