i want to konw which code is more faster.
compare this two code
Dim x As Boolean
Dim y As Boolean
If x = True Then
If y = True Then
Else
End If
Else
If y = True Then
Else
End If
End If
'=============================================
Enum xx
o = 0
x = 1
xy = 2
y = 3
End Enum
Sub Script_validate()
Dim nn As xx
If nn.o Then
ElseIf nn.x Then
ElseIf nn.xy Then
ElseIf nn.y Then
End If
End Sub
which code is faster and which code had low memory usage
I'm trying to use Julia to solve the common tile game 15 Puzzle using Julia using A* algorithm. I am quite new to the language and my style may seem very C like. When I try the following code, I run out of memory. I'm not sure if its related to the use of a pointer style in my structs or just bad design.
struct Node
parent
f::Int64
board::Array{Int64,1}
end
function findblank(A::Array{Int64,1})
x = size(A,1)
for i = 1:x
if A[i] == x
return i
end
end
return -1
end
function up(A::Array{Int64,1})
N = size(A,1)
Nsq = isqrt(N)
blank = findblank(A)
B = copy(A)
if blank / Nsq <= 1
return nothing
end
B[blank-Nsq],B[blank] = B[blank],B[blank-Nsq]
return B
end
function down(A::Array{Int64,1})
N = size(A,1)
Nsq = isqrt(N)
blank = findblank(A)
B = copy(A)
if (blank / Nsq) > (Nsq -1)
return nothing
end
B[blank+Nsq],B[blank] = B[blank],B[blank+Nsq]
return B
end
function left(A::Array{Int64,1})
N = size(A,1)
Nsq = isqrt(N)
blank = findblank(A)
B = copy(A)
if (blank % Nsq) == 1
return nothing
end
B[blank-1],B[blank] = B[blank],B[blank-1]
return B
end
function right(A::Array{Int64,1})
N = size(A,1)
Nsq = isqrt(N)
blank = findblank(A)
B = copy(A)
if (blank % Nsq) == 0
return nothing
end
B[blank+1],B[blank] = B[blank],B[blank+1]
return B
end
function manhattan(A::Array{Int64,1})
N = size(A,1)
Nsq = isqrt(N)
r = 0
for i in 1:N
if (A[i]==i || A[i]==N)
continue
end
row1 = floor((A[i]-1) / Nsq)
col1 = (A[i]-1) % Nsq
row2 = floor((i-1) / Nsq)
col2 = (i-1) % Nsq
r+= abs(row1 - row2) + abs(col1 - col2)
end
return r
end
# start = [1,2,3,4,5,6,7,9,8]
# start = [6,5,4,1,7,3,9,8,2] #26 moves
start = [7,8,4,11,12,14,10,15,16,5,3,13,2,1,9,6] # 50 moves
goal = [x for x in 1:length(start)]
# println("The manhattan distance of $start is $(manhattan(start))")
g = 0
f = g + manhattan(start)
pq = PriorityQueue()
actions = [up,down,left,right]
dd = Dict{Array{Int64,1},Int64}()
snode = Node(C_NULL,f,start)
enqueue!(pq,snode,f)
pos_seen = 0
moves = 0
while (!isempty(pq))
current = dequeue!(pq)
if haskey(dd,current.board)
continue
else
push!(dd, current.board =>current.f)
end
if (current.board == goal)
while(current.board != start)
println(current.board)
global moves +=1
current = current.parent[]
end
println(start)
println("$start solved in $moves moves after looking at $pos_seen positions")
break
end
global pos_seen+=1
global g+=1
for i in 1:4
nextmove = actions[i](current.board)
if (nextmove === nothing || nextmove == current.board || haskey(dd,nextmove))
continue
else
global f = g+manhattan(nextmove)
n = Node(Ref(current),f,nextmove)
enqueue!(pq,n,f)
end
end
end
println("END")
I'm trying to send this to SerialPort to command the devices' LED to lit on.
SData(0) = 85
SData(1) = 170
SData(2) = 36
SData(3) = 1
SData(4) = 2
SData(5) = 0
SData(6) = 1
SData(7) = 0
SData(8) = 0
SData(9) = 0
SData(10) = 0
SData(11) = 0
SData(12) = 0
SData(13) = 0
SData(14) = 0
SData(15) = 0
SData(16) = 0
SData(17) = 0
SData(18) = 0
SData(19) = 0
SData(20) = 0
SData(21) = 0
SData(22) = 39
SData(23) = 1
SerialPort1.Open()
For i = 0 To 23
SerialPort1.Write(Chr(SData(i)))
Next
SerialPort1.Close()
I don't get response from the device or anything. Then i noticed that the module freezes for 2 seconds, i think it's the loop.
I think i need to know the steps of how a SerialPort runs from open to close.
Btw, there's a original copy of this but it was in VB6
here's whats working: MSComm1.Output = Chr(SData(i))
Is there a faster way to define the following variables. notAct is the name, after the name comes the cells positions. The "b" is for the column and the number refers to the row. The value is 0. Very single cell has to be defined so that I can use the variables.
Dim notAct_b1, notAct_c1, notAct_d1, ..., notAct_d20 As Integer
notAct_b1 = 0
notAct_c1 = 0
notAct_d1 = 0
notAct_b2 = 0
notAct_c2 = 0
notAct_d2 = 0
notAct_b3 = 0
notAct_c3 = 0
notAct_d3 = 0
notAct_b4 = 0
notAct_c4 = 0
notAct_d4 = 0
notAct_b5 = 0
notAct_c5 = 0
notAct_d5 = 0
notAct_b6 = 0
notAct_c6 = 0
notAct_d6 = 0
notAct_b7 = 0
notAct_c7 = 0
notAct_d7 = 0
notAct_b8 = 0
notAct_c8 = 0
notAct_d8 = 0
notAct_b9 = 0
notAct_c9 = 0
notAct_d9 = 0
notAct_b10 = 0
notAct_c10 = 0
notAct_d10 = 0
notAct_b11 = 0
notAct_c11 = 0
notAct_d11 = 0
notAct_b12 = 0
notAct_c12 = 0
notAct_d12 = 0
notAct_b13 = 0
notAct_c13 = 0
notAct_d13 = 0
notAct_b14 = 0
notAct_c14 = 0
notAct_d14 = 0
notAct_b15 = 0
notAct_c15 = 0
notAct_d15 = 0
notAct_b16 = 0
notAct_c16 = 0
notAct_d16 = 0
notAct_b17 = 0
notAct_c17 = 0
notAct_d17 = 0
notAct_b18 = 0
notAct_c18 = 0
notAct_d18 = 0
notAct_b19 = 0
notAct_c19 = 0
notAct_d19 = 0
notAct_b20 = 0
notAct_c20 = 0
notAct_d20 = 0
First of all, you don't need to initialize an Integer variable in VBA - it will be 0 after it is Dimmed.
However, instead of defining 4x20 variables, better use an array:
Dim notAct(1 to 20,1 to 43) As Integer
If you need to initialize or reset all values, you can use a small loop:
Dim col As Integer, row As Long
'Initialize/reset
For col = 1 To 4
For row = 1 To 20
notAct(row, col) = 0 'setting 0 not required after Dim
Next row
Next col
If you need to assign the value of a variable to a cell, use this syntax:
'Assigns a value from the array to B3
Cells(3, 2).Value = notAct(3, 2)
And best of all, if you want to assign all 4x20 cells with their values, use this one line of code:
Range("A1:D20").Value = notAct
I am new to VB.net and facing a strange situation. I have a structure that contains another structure. The inner structure is nullable. Now wht I do is something like the following. I create a new instance of the inner structure, set the member variables and assign the whole structure to the inner structure of the parent. But it is giving an error. The assignment is not successful. If I try to peek into the structure in the watch window, it says "property evaluation failed" for the HasValue and Value properties.
Dim testData As List(Of TestData) = Nothing
Dim testData_List1 As New TestData
With testData_List1.commonTestParam
.AccuchekActiveEnergy = 2.56
.AccuchekActiveEnergyUnit = ActiveEnergyUnit.KiloWattHour
.AccuchekApparentEnergy = 34.56
.AccuchekApparentEnergyUnit = ApparentEnergyUnit.VoltAmpereHour
.AccuchekFrequency = 1
.AccuchekRange = "20474 ewr 34324"
.AccuchekType = AccuchekType.AccuchekOne
.ActiveLoadRPhase = 145
.AvgActiveLoad = 2.56
.AvgActiveLoadUnit = ActiveLoadUnit.Watt
.AvgPowerFactor = 0
.AvgPowerFactorType = PowerFactorType.PFLag
.ConditionalFlag1 = 0
.ConsumerNo = "343122050242"
.CurrentRPhase = 1
.GeneralFlag1 = 0
.InstantaneousPFRPhase = 2
.ManufacturingYear = 2009
.MeterActiveEnergy = 258.89
.MeterActiveEnergyUnit = ActiveEnergyUnit.KiloWattHour
.MeterActiveError = 20
.MeterApparentError = 14
.MeterConstant = 3200
.MeterConstantUnit = MeterConstantUnit.RevsPerkVAh
.MeterMake = "DS"
.MeterSNo = "6563402"
.MeterTypeAndClass = MeterTypeAndClass.MTCElectorMechWith20
.MTFID = "123456789"
.NoofTestRevolutions = 100
.PulseCriteria = 0
.RatedBasic = 25
.RatedMax = 30
.RatedVoltage = 15
.ReactiveCurrentRPhase = 145
.RemarkID = 0
.TestDateAndTime = "100320101545"
.TestDuration = 2145
.TesterCode = 0
.TestID = "147852"
.TestMode = TestMode.TMOpticalScanner
.TestNumber = 0
.VoltageRPhase = 145
End With
Dim accuchek3TestParameters1 As New Accuchek3PhaseTestParameters
With accuchek3TestParameters1
.AccuchekReactiveLagEnergy = 2.46
.AccuchekReactiveLagEnergyUnit = ReactiveEnergyUnit.KiloVoltAmpereReactiveHour
.AccuchekReactiveLeadEnergy = 2.56
.AccuchekReactiveLeadEnergyUnit = ReactiveEnergyUnit.KiloVoltAmpereReactiveHour
.ActiveLoadBPhase = 14
.ActiveLoadYPhase = 15
.AvgApparentLoad = 10
.AvgApparentLoadUnit = ApparentLoadUnit.KiloVoltAmpere
.AvgReactiveLagLoad = 14
.AvgReactiveLagLoadUnit = ReactiveLoadUnit.KiloVoltAmpereReactive
.AvgReactiveLeadLoad = 15
.AvgReactiveLeadLoadUnit = ReactiveLoadUnit.KiloVoltAmpereReactive
.ConditionalFlag2 = 0
.ConditionalFlag3 = 0
.CTRatio = 1.23
.CurrentBPhase = 10
.CurrentYPhase = 11
.InstantaneousPFBPhase = 0
.InstantaneousPFYPhase = 1
.MeterApparentEnergy = 1.01
.MeterApparentUnit = ApparentEnergyUnit.KiloVoltAmpereHour
.MeterReactiveLagEnergy = 1.25
.MeterReactiveLagError = 1.25
.MeterReactiveLagUnit = ReactiveEnergyUnit.KiloVoltAmpereReactiveHour
.MeterReactiveLeadEnergy = 1.45
.MeterReactiveLeadError = 1.56
.MeterReactiveLeadUnit = ReactiveEnergyUnit.KiloVoltAmpereReactiveHour
.PercentageLoad = 1
.PTRatio = 1
.ReactiveCurrentBPhase = 10
.ReactiveCurrentYPhase = 11
.VoltageBPhase = 10
.VoltageYPhase = 10
End With
testData_List1.accuchek3TestParameters = accuchek3TestParameters1
testData.Add(testData_List1)
Can somebody please guide me?
Your first line is:
Dim testData As List(Of TestData) = Nothing
Then at the bottom you do
testData.Add(testData_List1)
I can't see anywhere in between where you do something like:
testData = New List(Of TestData)()
Though it's slightly hard to read since you've got both a variables and types with TestData as their name (or part of their name) so I might just be missing that.