|
-
Aug 25th, 2005, 11:07 PM
#1
Thread Starter
Junior Member
argh! count number of full stops??
i have to count the number of full stops in my program thing. does anyone know how to do that? please??!
counting any individual characters really
-
Aug 25th, 2005, 11:20 PM
#2
Re: argh! count number of full stops??
You would eliminate the spaces, if that's what you mean.
VB Code:
MsgBox "Character count: " & Len(strBuff) - Len(Replace(strBuff, " ", "")) + 1
if you have any vbCRLF, you would also replace the two characters with one, or possibly none, depending if you want to count them or not.
VB Code:
MsgBox Len(strBuff) - Len(Replace(strBuff, vbCrLf, "x")) + 1
-
Aug 25th, 2005, 11:20 PM
#3
Re: argh! count number of full stops??
By full stops do you mean line breaks? If so then try this:
VB Code:
lCur = InStr(1, sMyStr, vbCrLf)
Do Until lCur = 0
lCount = lCount + 1
lCur = InStr(lCur , sMyStr, vbCrLf)
Loop
I didn't test it, but try that. There must be a better way though.
EDIT: ... And DG beat me to the punch with a better solution.
-
Aug 26th, 2005, 03:40 AM
#4
Frenzied Member
Re: argh! count number of full stops??
VB Code:
Public Function Sisic(sMain As String, sLookFor As String) As Long
'[S]tring [I]n [S]tring [I}nstance [C]ount
Dim nStart As Long
Dim nResult As Long
nStart = 1
nResult = 0
Do While InStr(nStart, sMain, sLookFor) > 0
nStart = InStr(nStart, sMain, sLookFor) + 1
Sisic = Sisic + 1
Loop
End Function
There are more efficient ways to do this, but this is the most readable, and is virtually identical to the post above.
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein
It's turtles! And it's all the way down
-
Aug 26th, 2005, 03:47 AM
#5
Frenzied Member
Re: argh! count number of full stops??
Try this:
VB Code:
Option Explicit
Private Sub Form_Load()
Debug.Print Sisic("Now is the time for all good men to come to the aid of the party", "o")
End Sub
Public Function Sisic(Str As String, LookFor As String) As Long
Dim lLookFor As Long
Dim Tmp() As Byte
Dim i As Long
If LenB(Str) > 0 Then
lLookFor = AscW(LookFor)
Tmp = Str
For i = 0 To LenB(Str) - 1 Step 2
If Tmp(i) = lLookFor Then
Sisic = Sisic + 1
End If
Next
End If
End Function
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein
It's turtles! And it's all the way down
-
Aug 26th, 2005, 04:35 AM
#6
Re: argh! count number of full stops??
VB Code:
Function InStrCount(ByRef pszString As String, ByRef pszFind As String) As Long
Dim sTemp As String
sTemp = Replace$(pszString, pszFind, vbNullString)
InStrCount = Len(pszString) - Len(sTemp)
End Function
-
Aug 26th, 2005, 04:47 AM
#7
Frenzied Member
Re: argh! count number of full stops??
VB Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Command1_Click()
Const TEST As String = "Now is the time for all good men to come to the aid of the party"
Dim Start As Long
Dim Finish As Long
Dim i As Long
Start = GetTickCount()
For i = 0 To 1000000
Sisic TEST, "o"
Next
Finish = GetTickCount()
Text1.Text = Finish - Start
Start = GetTickCount()
For i = 0 To 1000000
InStrCount TEST, "o"
Next
Finish = GetTickCount
Text2.Text = Finish - Start
End Sub
Public Function Sisic(Str As String, LookFor As String) As Long
Dim lLookFor As Long
Dim Tmp() As Byte
Dim i As Long
If LenB(Str) > 0 Then
lLookFor = AscW(LookFor)
Tmp = Str
For i = 0 To LenB(Str) - 1 Step 2
If Tmp(i) = lLookFor Then
Sisic = Sisic + 1
End If
Next
End If
End Function
Function InStrCount(ByRef pszString As String, ByRef pszFind As String) As Long
Dim sTemp As String
sTemp = Replace$(pszString, pszFind, vbNullString)
InStrCount = Len(pszString) - Len(sTemp)
End Function
Gives:
IDE Sisic:8266, InStrCount:5828
Compiled Sisic:1344, InStrCount:5375
on P4 2.79Ghz,1Gb RAM
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein
It's turtles! And it's all the way down
-
Aug 26th, 2005, 04:57 AM
#8
Frenzied Member
Re: argh! count number of full stops??
in VB6 "Strings are Evil"(TM)
I always find that the sooner you can treat them as numbers the better.
Don't be sad.
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein
It's turtles! And it's all the way down
-
Aug 26th, 2005, 05:03 AM
#9
Frenzied Member
Re: argh! count number of full stops??
VB Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Command1_Click()
Const TEST As String = "Now is the time for all good men to come to the aid of the party "
Dim Start As Long
Dim Finish As Long
Dim i As Long
Start = GetTickCount()
For i = 0 To 1000000
Sisic TEST, "o"
Next
Finish = GetTickCount()
Text1.Text = Finish - Start
Start = GetTickCount()
For i = 0 To 1000000
InStrCount TEST, "o"
Next
Finish = GetTickCount
Text2.Text = Finish - Start
Start = GetTickCount()
For i = 0 To 1000000
Sisic2 TEST, "o"
Next
Finish = GetTickCount
Text3.Text = Finish - Start
End Sub
Public Function Sisic2(sMain As String, sLookFor As String) As Long
'[S]tring [I]n [S]tring [I}nstance [C]ount
Dim nStart As Long
Dim nResult As Long
nStart = 1
nResult = 0
Do While InStr(nStart, sMain, sLookFor) > 0
nStart = InStr(nStart, sMain, sLookFor) + 1
Sisic2 = Sisic2 + 1
Loop
End Function
Public Function Sisic(Str As String, LookFor As String) As Long
Dim lLookFor As Long
Dim Tmp() As Byte
Dim i As Long
If LenB(Str) > 0 Then
lLookFor = AscW(LookFor)
Tmp = Str
For i = 0 To LenB(Str) - 1 Step 2
If Tmp(i) = lLookFor Then
Sisic = Sisic + 1
End If
Next
End If
End Function
Function InStrCount(ByRef pszString As String, ByRef pszFind As String) As Long
Dim sTemp As String
sTemp = Replace$(pszString, pszFind, vbNullString)
InStrCount = Len(pszString) - Len(sTemp)
End Function
Thought I'd try out the Instr method:
IDE Sisic:8235,InStrCount:5844,Sisic2:4296
Compiled Sisic:1250,InStrCount:5375,Sisic2:1890
I'm quite surprised how fast the Instr method executes.
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein
It's turtles! And it's all the way down
-
Aug 26th, 2005, 05:15 AM
#10
Re: argh! count number of full stops??
I came up with this monstrocity 
VB Code:
Private Function Woof(ByVal Text As String, ByVal SearchString As String) As Long
Dim lngIndex As Long
For lngIndex = 1 To Len(Text)
If Mid$(Text, lngIndex, 1) <> SearchString Then
Mid$(Text, lngIndex, 1) = " "
End If
Next lngIndex
Text = Replace$(Text, " ", vbNullString)
Woof = Len(Text)
End Function
Read it and weep...slower than all of your code 
Woof
-
Aug 26th, 2005, 05:25 AM
#11
Re: argh! count number of full stops??
 Originally Posted by yrwyddfa
What you should be sad about is the performance of the Aussies in the Ashes . . . 
You'll pay for that 
VB Code:
Function InStrCount2(ByRef pszString As String, ByRef pszFind As String) As Long
Dim chBuf() As Byte
Dim chSearch() As Byte
Dim lchSearch As Long
Dim lSearchLen As Long
Dim lChunkLen As Long
Dim i As Long
Dim j As Long
chBuf = pszString
lChunkLen = LenB(pszFind)
lSearchLen = LenB(pszString)
If (lChunkLen < lSearchLen) Then
If (lChunkLen = 2) Then
lchSearch = AscW(pszFind)
For i = 0 To lSearchLen - 1 Step 2
If (chBuf(i) = lchSearch) Then _
InStrCount2 = InStrCount2 + 1
Next i
Else
chSearch = pszFind
For i = 0 To lSearchLen - 1 Step lChunkLen
For j = 0 To lChunkLen Step 2
If (chBuf(i + j) <> chSearch(j)) Then
Exit For
Else
If (j = lChunkLen) Then _
InStrCount2 = InStrCount2 + 1
End If
Next j
Next i
End If
Else
Err.Raise 1, , "You suck"
End If
End Function
Results (compiled and run at realtime priority):
Sisic() - 1265 ms
InStrCount() - 4329 ms
InStrCount2() - 1218 ms
Specs: Athlon XP 2600+, 512 RAM.
-
Aug 26th, 2005, 05:28 AM
#12
Frenzied Member
Re: argh! count number of full stops??
This code certainly squeezes the rep point from me. Nice one
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein
It's turtles! And it's all the way down
-
Aug 26th, 2005, 05:44 AM
#13
Re: argh! count number of full stops??
I had a hunch this would work and it did. Change lchSearch from Long to Byte and the results are as follows:
Sisic() - 1265 ms
InStrCount() - 4313 ms
InStrCount2() - 1172 ms
To me that suggests a Byte/Long comparison is a two step operation (pad the byte out to a Long and then compare) whereas a Byte/Byte comparison can be done in one hit.
-
Aug 26th, 2005, 06:16 AM
#14
Frenzied Member
Re: argh! count number of full stops??
VB Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private BufStr(511) As Byte
Private BufFind(511) As Byte
Private Sub Command1_Click()
Const TEST As String = "Now is the time for all good men to come to the aid of the party "
Dim Start As Long
Dim Finish As Long
Dim i As Long
Start = GetTickCount()
For i = 0 To 1000000
Sisic3 TEST, "o"
Next
Finish = GetTickCount
Text3.Text = Finish - Start
End Sub
Public Function Sisic3(Str As String, Find As String) As Long
Dim i As Long
Dim j As Long
Dim lenStr As Long
Dim lenFind As Long
Dim Flag As Long
CopyMemory BufStr(0), ByVal StrPtr(Str), LenB(Str)
CopyMemory BufFind(0), ByVal StrPtr(Find), LenB(Str)
lenStr = LenB(Str)
lenFind = LenB(Find)
For i = (lenStr - 1) To lenFind Step -2
Flag = 0
For j = (lenFind - 1) To 0 Step -2
If Not (BufStr(i - (lenFind - j)) = BufFind(j - 1)) Then
Flag = -1
Exit For
End If
Next
If Flag = 0 Then
Sisic3 = Sisic3 + 1
End If
Next
End Function
Gives a compiled speed of 890.
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein
It's turtles! And it's all the way down
-
Aug 26th, 2005, 06:17 AM
#15
Frenzied Member
Re: argh! count number of full stops??
VB Code:
Public Function Sisic3(Str As String, Find As String) As Long
Dim i As Long
Dim j As Long
Dim lenStr As Long
Dim lenFind As Long
Dim Flag As Long
lenStr = LenB(Str)
lenFind = LenB(Find)
CopyMemory BufStr(0), ByVal StrPtr(Str), lenStr
CopyMemory BufFind(0), ByVal StrPtr(Find), lenFind
For i = (lenStr - 1) To lenFind Step -2
Flag = 0
For j = (lenFind - 1) To 0 Step -2
If Not (BufStr(i - (lenFind - j)) = BufFind(j - 1)) Then
Flag = -1
Exit For
End If
Next
If Flag = 0 Then
Sisic3 = Sisic3 + 1
End If
Next
End Function
gives a compiled speed of 811
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein
It's turtles! And it's all the way down
-
Aug 26th, 2005, 06:28 AM
#16
Frenzied Member
Re: argh! count number of full stops??
VB Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private BufStr(511) As Byte
Private BufFind(511) As Byte
Private Sub Command1_Click()
Const TEST As String = "Now is the time for all good men to come to the aid of the party "
Dim Start As Long
Dim Finish As Long
Dim i As Long
Dim Str As String
Str = "o"
Start = GetTickCount()
For i = 0 To 1000000
Sisic3 StrPtr(TEST), StrPtr("is"), LenB(TEST), LenB(Str)
Next
Finish = GetTickCount
Text3.Text = Finish - Start
End Sub
Public Function Sisic3(ByVal pStr As Long, ByVal pFind As Long, ByVal lenStr As Long, ByVal lenFind As Long) As Long
Dim i As Long
Dim j As Long
Dim Flag As Long
CopyMemory BufStr(0), ByVal pStr, lenStr
CopyMemory BufFind(0), ByVal pFind, lenFind
For i = (lenStr - 1) To lenFind Step -2
Flag = 0
For j = (lenFind - 1) To 0 Step -2
If Not (BufStr(i - (lenFind - j)) = BufFind(j - 1)) Then
Flag = -1
Exit For
End If
Next
If Flag = 0 Then
Sisic3 = Sisic3 + 1
End If
Next
End Function
Compiled execution time of 515
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein
It's turtles! And it's all the way down
-
Aug 26th, 2005, 06:34 AM
#17
Re: argh! count number of full stops??
Cheat... you took the byte array out of the procedure
-
Aug 26th, 2005, 07:12 AM
#18
Re: argh! count number of full stops??
w00000t! 
VB Code:
Function InStrCount3(ByVal pszString As Long, ByVal pszFind As Long) As Long
Static chBuf(1024) As Byte
Static chFind(1024) As Byte
Dim chSearchChar As Byte
Dim lStringLen As Long
Dim lFindLen As Long
Dim i As Long
Dim j As Long
RtlMoveMemory lStringLen, ByVal (pszString - 4), 4&
RtlMoveMemory chBuf(0), ByVal pszString, lStringLen
RtlMoveMemory lFindLen, ByVal (pszFind - 4), 4&
If (lFindLen = 2) Then
RtlMoveMemory chSearchChar, ByVal pszFind, 1
For i = 0 To lStringLen Step 2
If (chBuf(i) = chSearchChar) Then _
InStrCount3 = InStrCount3 + 1
Next i
Else
RtlMoveMemory chFind(0), ByVal pszFind, lFindLen
For i = 0 To lStringLen Step lFindLen
For j = 0 To lFindLen Step 2
If (chBuf(i + j) <> chFind(j)) Then
Exit For
Else
If (j = lFindLen) Then _
InStrCount3 = InStrCount3 + 1
End If
Next j
Next i
End If
End Function
Sisic3() - 391 ms
InStrCount3() - 343 ms
-
Aug 26th, 2005, 07:59 AM
#19
Re: argh! count number of full stops??
u forgot to include the API declarations.
Woof
-
Aug 26th, 2005, 08:05 AM
#20
Re: argh! count number of full stops??
Come on Woka... I can write them off the top of my head 
VB Code:
Declare Sub RtlMoveMemory Lib "ntdll.dll" ( _
ByRef lpvDest As Any, _
ByRef lpvSrc As Any, _
ByVal cbLen As Long _
)
-
Aug 26th, 2005, 08:11 AM
#21
Frenzied Member
Re: argh! count number of full stops??
Nice code.
I would think, though, that the difference of 50 is negligable. What's the average over many process runs?
I suspect that any performance increase is due to the fact you determine whether it's a single character, or a phrase to look for.
I'd be interested to see the performance for phrases.
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein
It's turtles! And it's all the way down
-
Aug 26th, 2005, 08:17 AM
#22
Re: argh! count number of full stops??
Yeah, yours enters two loops while mine enters only one for a single character.
I ran it at realtime priority and it was always the same time every time. At normal priority it would obviously vary more but it wouldn't be such an accurate reflection of the code's efficiency.
-
Aug 26th, 2005, 08:20 AM
#23
Frenzied Member
Re: argh! count number of full stops??
VB Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private BufStr(511) As Byte
Private BufFind(511) As Byte
Private Sub Command1_Click()
Const TEST As String = "Now is the time for all good men to come to the aid of the party "
Dim Start As Long
Dim Finish As Long
Dim i As Long
Dim Str As String
Str = "o"
Start = GetTickCount()
For i = 0 To 1000000
Sisic3 StrPtr(TEST), StrPtr(Str), LenB(TEST), LenB(Str)
Next
Finish = GetTickCount
Text3.Text = Finish - Start
End Sub
Public Function Sisic3(pStr As Long, pFind As Long, lenStr As Long, lenFind As Long) As Long
Dim i As Long
Dim j As Long
Dim Flag As Long
CopyMemory BufStr(0), ByVal pStr, lenStr
CopyMemory BufFind(0), ByVal pFind, lenFind
If lenFind = 2 Then
For i = (lenStr - 1) To lenFind Step -2
If BufStr(i) = BufFind(0) Then
Sisic3 = Sisic3 + 1
End If
Next
Else
For i = (lenStr - 1) To lenFind Step -2
Flag = 0
For j = (lenFind - 1) To 0 Step -2
If Not (BufStr(i - (lenFind - j)) = BufFind(j - 1)) Then
Flag = -1
Exit For
End If
Next
If Flag = 0 Then
Sisic3 = Sisic3 + 1
End If
Next
End If
End Function
Nope. Your routine to check for the two byte length is very significant
I've added it to this routine and it comes in at 271
Last edited by yrwyddfa; Aug 26th, 2005 at 08:40 AM.
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein
It's turtles! And it's all the way down
-
Aug 26th, 2005, 08:40 AM
#24
Re: argh! count number of full stops??
Bah. Your last function doesn't work. No wonder it's so much faster
-
Aug 26th, 2005, 08:44 AM
#25
Frenzied Member
Re: argh! count number of full stops??
VB Code:
Public Function Sisic3(pStr As Long, pFind As Long, lenStr As Long, lenFind As Long) As Long
Dim i As Long
Dim j As Long
Dim Flag As Long
CopyMemory BufStr(0), ByVal pStr, lenStr
CopyMemory BufFind(0), ByVal pFind, lenFind
If lenFind = 2 Then
For i = (lenStr - 1) To lenFind Step -2
If BufStr(i - 1) = BufFind(0) Then
Sisic3 = Sisic3 + 1
End If
Next
Else
For i = (lenStr - 1) To lenFind Step -2
Flag = 0
For j = (lenFind - 1) To 0 Step -2
If Not (BufStr(i - (lenFind - j)) = BufFind(j - 1)) Then
Flag = -1
Exit For
End If
Next
If Flag = 0 Then
Sisic3 = Sisic3 + 1
End If
Next
End If
End Function
Yup! Whoops
It works now but is a measly 453. What does it run at on your machine?
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein
It's turtles! And it's all the way down
-
Aug 26th, 2005, 08:51 AM
#26
Frenzied Member
Re: argh! count number of full stops??
VB Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private BufStr(511) As Byte
Private BufFind(511) As Byte
Private Sub Command1_Click()
Const TEST As String = "Now is the time for all good men to come to the aid of the party "
Dim Start As Long
Dim Finish As Long
Dim i As Long
Dim str As String
str = "o"
Start = GetTickCount()
For i = 0 To 1000000
Sisic TEST, "o"
Next
Finish = GetTickCount()
Text1.Text = Finish - Start
Start = GetTickCount()
For i = 0 To 1000000
InStrCount3 StrPtr(TEST), StrPtr(str)
Next
Finish = GetTickCount
Text2.Text = Finish - Start
Start = GetTickCount()
For i = 0 To 1000000
Sisic2 TEST, "o"
Next
Finish = GetTickCount
Text3.Text = Finish - Start
Start = GetTickCount()
For i = 0 To 1000000
Sisic3 StrPtr(TEST), StrPtr(str), LenB(TEST), LenB(str)
Next
Finish = GetTickCount
Text4.Text = Finish - Start
End Sub
Public Function Sisic(str As String, LookFor As String) As Long
Dim lLookFor As Long
Dim Tmp() As Byte
Dim i As Long
If LenB(str) > 0 Then
lLookFor = AscW(LookFor)
Tmp = str
For i = 0 To LenB(str) - 1 Step 2
If Tmp(i) = lLookFor Then
Sisic = Sisic + 1
End If
Next
End If
End Function
Public Function Sisic2(sMain As String, sLookFor As String) As Long
'[S]tring [I]n [S]tring [I}nstance [C]ount
Dim nStart As Long
Dim nResult As Long
nStart = 1
nResult = 0
Do While InStr(nStart, sMain, sLookFor) > 0
nStart = InStr(nStart, sMain, sLookFor) + 1
Sisic2 = Sisic2 + 1
Loop
End Function
Public Function Sisic3(pStr As Long, pFind As Long, lenStr As Long, lenFind As Long) As Long
Dim i As Long
Dim j As Long
Dim Flag As Long
CopyMemory BufStr(0), ByVal pStr, lenStr
CopyMemory BufFind(0), ByVal pFind, lenFind
If lenFind = 2 Then
For i = (lenStr - 1) To lenFind Step -2
If BufStr(i - 1) = BufFind(0) Then
Sisic3 = Sisic3 + 1
End If
Next
Else
For i = (lenStr - 1) To lenFind Step -2
Flag = 0
For j = (lenFind - 1) To 0 Step -2
If Not (BufStr(i - (lenFind - j)) = BufFind(j - 1)) Then
Flag = -1
Exit For
End If
Next
If Flag = 0 Then
Sisic3 = Sisic3 + 1
End If
Next
End If
End Function
Function InStrCount3(ByVal pszString As Long, ByVal pszFind As Long) As Long
Static chBuf(1024) As Byte
Static chFind(1024) As Byte
Dim chSearchChar As Byte
Dim lStringLen As Long
Dim lFindLen As Long
Dim i As Long
Dim j As Long
CopyMemory lStringLen, ByVal (pszString - 4), 4&
CopyMemory chBuf(0), ByVal pszString, lStringLen
CopyMemory lFindLen, ByVal (pszFind - 4), 4&
If (lFindLen = 2) Then
CopyMemory chSearchChar, ByVal pszFind, 1
For i = 0 To lStringLen Step 2
If (chBuf(i) = chSearchChar) Then _
InStrCount3 = InStrCount3 + 1
Next i
Else
CopyMemory chFind(0), ByVal pszFind, lFindLen
For i = 0 To lStringLen Step lFindLen
For j = 0 To lFindLen Step 2
If (chBuf(i + j) <> chFind(j)) Then
Exit For
Else
If (j = lFindLen) Then _
InStrCount3 = InStrCount3 + 1
End If
Next j
Next i
End If
End Function
My results are:
Sisic1: 1281
Sisic2: 1875
Sisic3: 469
InstrCount3: 516
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein
It's turtles! And it's all the way down
-
Aug 26th, 2005, 08:51 AM
#27
Re: argh! count number of full stops??
They are, alternatingly, both 344 ms, and every second run yours is about 10-20 ms faster.
I also found that my phrase checking doesn't work, umm, so will fix that...
-
Aug 26th, 2005, 08:54 AM
#28
Frenzied Member
Re: argh! count number of full stops??
When run at realtime not much siginificant difference:
Sisic1: 1250
Sisic2: 1843
Sisic3: 455
InstrCount3: 516
How fast does it run on your system?
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein
It's turtles! And it's all the way down
-
Aug 26th, 2005, 08:57 AM
#29
Frenzied Member
Re: argh! count number of full stops??
 Originally Posted by penagate
They are, alternatingly, both 344 ms, and every second run yours is about 10-20 ms faster.
I also found that my phrase checking doesn't work, umm, so will fix that...
I think that perhaps we should call a draw? Before long we'll be by passing every VB call (for instance SafeArrayCreateVectorEx will half the amount of heap allocations) for weird and wonderful API calls.
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein
It's turtles! And it's all the way down
-
Aug 26th, 2005, 09:00 AM
#30
Frenzied Member
Re: argh! count number of full stops??
Still both are examples of extremely fast VB code.
I hope the author of the thread appreciates this lot
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein
It's turtles! And it's all the way down
-
Aug 26th, 2005, 09:04 AM
#31
Frenzied Member
Re: argh! count number of full stops??
It's tricky to directly do stack allocations using VB. You'll need a machine code snippet that pushes and pops stuff at the right place at the right time.
Do you think I'm gonna tell ya how? Never 
There is a way of using heap allocations, and tricking VB to copy the local heap onto the stack, though . . .
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein
It's turtles! And it's all the way down
-
Aug 26th, 2005, 09:07 AM
#32
Re: argh! count number of full stops??
 Originally Posted by yrwyddfa
It's tricky to directly do stack allocations using VB. You'll need a machine code snippet that pushes and pops stuff at the right place at the right time.
Do you think I'm gonna tell ya how? Never
There is a way of using heap allocations, and tricking VB to copy the local heap onto the stack, though . . .
QBASIC could do these in 1 statement couldn't it? 
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Aug 26th, 2005, 09:08 AM
#33
Frenzied Member
Re: argh! count number of full stops??
Cricket is the best sport ever invented. It isn't even a sport. It's about drinking and smoking and occasionally throwing/hitting/catching a ball.
The times listed are for 1million goes at the function. I suspect the Sudoku people will be using either mine, or Penegate's code when they find it.
I can't easily (ie without stepping up in a magnitude of ability) see how either piece of code can be directly improved.
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein
It's turtles! And it's all the way down
-
Aug 26th, 2005, 09:12 AM
#34
Re: argh! count number of full stops??
I know how to do inline ASM but not how to call it without using an API and I think that would negate any advantage the stack allocation would have.
-
Aug 26th, 2005, 09:20 AM
#35
Frenzied Member
Re: argh! count number of full stops??
In this instance I would load an array with the ASM the final JMP instruction pointing to a module level Sub.
Using subclassing you can get Windows to vector to the array, the array should then vector to your local sub where you can clean up.
I haven't tried it (on this thing) but the setup code is bound to be expensive.
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein
It's turtles! And it's all the way down
-
Aug 26th, 2005, 10:45 AM
#36
Re: argh! count number of full stops??
I tried a variety of things and all made it slower, so I'm willing to call it a draw here. I think we can safely say we have got fairly close to the fastest it can be done in pure VB.
BTW, why do you go backwards in your loop? I tried it and it makes no difference.
-
Aug 26th, 2005, 10:50 AM
#37
Re: argh! count number of full stops??
I just deleted a half-dozen chit chat posts from this thread and on a day when the server is slow, that doesn't make me happy so please let's stick to VB. Also there are 30+ replies here and we are not even sure what the original poster wants. Shouldn't we wait to find out?
-
Aug 26th, 2005, 03:43 PM
#38
Frenzied Member
Re: argh! count number of full stops??
Marty: We know what the poster wants, it's in the first post. He wants an algorithm to count the number of radixes in a string. OK we hijacked the post a little . . .
Penagate: the reason why I reversed the loops is because in assembly, and on the x86 architecture loops always ran faster backwards because there was one less counter allocation. I have no empirical evidence that this works with compiled VB one way or the other, but it certainly worked with TASM on my old 386 . . .
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein
It's turtles! And it's all the way down
-
Aug 26th, 2005, 11:43 PM
#39
Re: argh! count number of full stops??
In assembly you would loop using %ecx. Here we're looping using i and j which you tell me is heap-allocated (I always thought locals were stack allocated but eh). So a loop is always gonna be faster in ASM.
As for backwards I see that you can use the "loop" instruction and it decreases the counter register. However that only decreases it by one and we are using Step. So I don't think either way is going to have any difference on the compiled output (probably just difference between an "add" and "sub").
-
Aug 27th, 2005, 02:18 AM
#40
Frenzied Member
Re: argh! count number of full stops??
Non-pointer variables (simple types) are - as you say - stack allocated. I'm pretty sure that SafeArray's are heap allocated.
"As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein
It's turtles! And it's all the way down
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|