[Info-vax] Listeners in VMS Basic, was: Re: Integrity iLO Configuration?
Arne Vajhøj
arne at vajhoej.dk
Tue Jun 29 13:01:31 EDT 2021
On 6/26/2021 2:53 PM, Arne Vajhøj wrote:
> On 6/25/2021 10:02 PM, Dave Froble wrote:
>> I'll just address my opinion of threads. Not saying they are useless,
>> but, if the OS, VMS, already gives me separate "threads" called
>> processes, why do I want the aggravation of re-inventing it inside my
>> apps?
>>
>> I'm sure someone will cry out "performance", but, with today's
>> processors, performance is most likely a rare issue.
>
> Threading provides a very convenient programming model.
>
> Shared heap is a lot easier than IPC.
And if Basic is better than English.
Imports System
Imports System.Linq
Imports System.Threading
Imports System.Threading.Tasks
Namespace T
Public Class Program
Public Class SumTask
Public Property X() As Double()
Public Property IxStart() As Integer
Public Property IxEnd() As Integer
Public Property Result() As Double
Public Sub New(_x As Double(), ix1 As Integer, ix2 As Integer)
X = _x
IxStart = ix1
IxEnd = ix2
End Sub
Public Sub Run()
Dim temp As Double() = X
Dim ix1 As Integer = IxStart
Dim ix2 As Integer = IxEnd
Dim res As Double = 0
For i As Integer = ix1 To ix2 - 1
res += temp(i)
Next
Result = res
End Sub
End Class
Private Shared Function ManSeq(x As Double()) As Double
Dim res As Double = 0
For i As Integer = 0 To x.Length - 1
res += x(i)
Next
Return res
End Function
Private Shared Function ManThread(x As Double()) As Double
Dim t As Thread() = New Thread(3) {}
Dim st As SumTask() = New SumTask(t.Length - 1) {}
For k As Integer = 0 To t.Length - 1
st(k) = New SumTask(x, k * x.Length \ t.Length, (k + 1)
* x.Length \ t.Length)
t(k) = New Thread(AddressOf st(k).Run)
t(k).Start()
Next
Dim res As Double = 0
For k As Integer = 0 To t.Length - 1
t(k).Join()
res += st(k).Result
Next
Return res
End Function
Private Shared Function ManPool(x As Double()) As Double
Dim t As Task() = New Task(15) {}
Dim st As SumTask() = New SumTask(t.Length - 1) {}
For k As Integer = 0 To t.Length - 1
st(k) = New SumTask(x, k * x.Length \ t.Length, (k + 1)
* x.Length \ t.Length)
t(k) = Task.Factory.StartNew(AddressOf st(k).Run)
Next
Dim res As Double = 0
For k As Integer = 0 To t.Length - 1
t(k).Wait()
res += st(k).Result
Next
Return res
End Function
Private Shared Function LinqSeq(x As Double()) As Double
Return x.Sum()
End Function
Private Shared Function LinqPar(x As Double()) As Double
Return x.AsParallel().Sum()
End Function
Private Const N As Integer = 100000000
Private Const M As Integer = 10
Private Const REP As Integer = 5
Private Delegate Function Summer(x As Double()) As Double
Private Shared Sub Test(lbl As String, sumf As Summer, x As
Double())
Dim dt1 As DateTime = DateTime.Now
For j As Integer = 0 To M - 1
Dim res As Double = sumf(x)
If res < 0.49 * N OrElse 0.51 * N < res Then
Throw New Exception("We have a bad RNG")
End If
Next
Dim dt2 As DateTime = DateTime.Now
Console.WriteLine("{0,-20} : {1,5:F0}", lbl, (dt2 -
dt1).TotalMilliseconds)
End Sub
Private Shared ReadOnly rng As New Random()
Public Shared Sub Main(args As String())
Dim x As Double() = New Double(N - 1) {}
For i As Integer = 0 To x.Length - 1
x(i) = rng.NextDouble()
Next
For j As Integer = 0 To REP - 1
Test("Manual sequential", AddressOf ManSeq, x)
Test("Manual threading", AddressOf ManThread, x)
Test("Manual thread pool", AddressOf ManPool, x)
Test("LINQ sequential", AddressOf LinqSeq, x)
Test("LINQ parallel", AddressOf LinqPar, x)
Console.WriteLine("----")
Next
Console.ReadKey()
End Sub
End Class
End Namespace
output:
Manual sequential : 3245
Manual threading : 1534
Manual thread pool : 769
LINQ sequential : 6609
LINQ parallel : 1716
----
Manual sequential : 3245
Manual threading : 1498
Manual thread pool : 749
LINQ sequential : 6568
LINQ parallel : 1529
----
Manual sequential : 3229
Manual threading : 1420
Manual thread pool : 842
LINQ sequential : 6583
LINQ parallel : 1544
----
Manual sequential : 3229
Manual threading : 1778
Manual thread pool : 733
LINQ sequential : 6571
LINQ parallel : 1654
----
Manual sequential : 3382
Manual threading : 1413
Manual thread pool : 761
LINQ sequential : 6673
LINQ parallel : 1482
----
Arne
More information about the Info-vax
mailing list