|
-
May 31st, 2002, 09:37 PM
#1
Thread Starter
PowerPoster
write a function to pad the number. Send it the number and an integer value representing the length it should be in characters.
VB Code:
Public Function LPAD (sNumber as String, Length as Integer) as String
LPAD = String$(Length - Len(sNumber) , "0") & sNumber
End Function
-
May 31st, 2002, 09:49 PM
#2
Possibly:
VB Code:
Format(yourNumber, "000000") 'Will pad out with precceding 0's
-
May 31st, 2002, 09:54 PM
#3
Thread Starter
PowerPoster
I like to do things the hard way.
I hate it when I don't think of things like that.
-
May 31st, 2002, 10:01 PM
#4
LOL 
Actually I was just testing for speed, and I think there identical.
-
May 31st, 2002, 10:16 PM
#5
Wow, it looks like the FUNCTION wins hands down 
Format ~ 500 and Function ~ 100!!!
VB Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Form_Load()
Dim x As Long, i As Long
Dim ans1 As Integer, ans2 As String
Dim t1 As Integer, t2 As Integer
'************* Test First Routine ***************
x = GetTickCount 'Load the Initial count, prior to test
For i = 0 To 50000
ans1 = Format("123", "000000")
Next
t1 = GetTickCount - x 'Determine time taken
'************* Test Second Routine ***************
x = GetTickCount 'Load the Initial count, prior to test
For i = 1 To 50000
ans2 = LPAD("123", 6)
Next
t2 = GetTickCount - x 'Determine time taken
'************* Display the Results ***************
Debug.Print t1 & vbTab & t2
End Sub
Private Function LPAD(sNumber As String, Length As Integer) As String
LPAD = String$(Length - Len(sNumber), "0") & sNumber
End Function
Bruce.
-
May 31st, 2002, 10:51 PM
#6
Thread Starter
PowerPoster
Bruce, I'm playing around with these function. Did you know you declared ans1 and ans2 as different types?
-
May 31st, 2002, 10:52 PM
#7
Hyperactive Member
I am probably stupid, but...
Dim ans2 As String
Dim x As Integer
Dim i As Long
x = Text1
For i = Text1 To Text2
ans2 = LPAD(x, Text3)
x = x + 1
Next
MsgBox "Finsihed", , "End"
Why isn't this working ?
text 1 = starting number
text 2 = ending number
text 3 = number of 0's to pad with
-
May 31st, 2002, 10:53 PM
#8
Yep, cause the Format returns an Integer, and the Function returns a String.
-
May 31st, 2002, 10:57 PM
#9
Hyperactive Member
Ya...
I noticed the he declared them differently as well, but he probably had a reason.
-
May 31st, 2002, 10:58 PM
#10
Hyperactive Member
LOL
Guess I should refressh the page before I goto reply.
-
May 31st, 2002, 11:02 PM
#11
Thread Starter
PowerPoster
Originally posted by Bruce Fox
Yep, cause the Format returns an Integer, and the Function returns a String.
OK, that makes sense to me, but I wasn't sure if you did it on purpose or not.
My computer just crashed again - new ram. I took the ram out and now have to start over again since I lost all the code.
Basically, I'm doing it about the same as you, but I'm trying to send the number to the LPAD function as a string, a long and a variant to see the difference.
I was 3/4 of the way there when my puter died.
-
May 31st, 2002, 11:06 PM
#12
Thanks Cafeenman the results will be interesting.
Animelion,
The answer to your last question is in this tread too! 
(Hint: our loops are for testing...)
-
May 31st, 2002, 11:12 PM
#13
Thread Starter
PowerPoster
Bruce, I ran each test 3 times - not scientific or anything, but the results were pretty much the same each time.
Column 1 is the Format result
Column 2 is the LPAD result
Code:
480 421 String
471 371 Long ' best time here using Long in LPAD
471 591 Variant
This is the code I used. Slightly different than yours. I wanted to see how it would do with varying length numbers. The only thing I changed for each test was the type for sNumber as an argument in LPAD. I didn't change any of the types in the Form_Load event.
VB Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Form_Load()
Dim i As Long
Dim x As Long
Dim sAnswer As String
Dim t1 As Long
Dim t2 As Long
' Test using Format
x = GetTickCount 'Load the Initial count, prior to test
For i = 0 To 100000
sAnswer = Format(Int(Rnd * 999999) + 1, "000000")
Next
t1 = GetTickCount - x 'Determine time taken
' Test using LPAD
x = GetTickCount 'Load the Initial count, prior to test
For i = 1 To 100000
sAnswer = LPAD(Int(Rnd * 999999) + 1, 6)
Next
t2 = GetTickCount - x 'Determine time taken
' Display the Results
Debug.Print t1 & vbTab & t2
MsgBox "Finished"
End Sub
Private Function LPAD(lngNumber As Long, Length As Integer) As String
LPAD = String$(Length - Len(lngNumber), "0") & sNumber
End Function
What I didn't check was to see if the function actually returned the number in the proper format. I'm pretty sure it did, but the type may need to be coerced into a string before padding it because of that thing where VB puts a space in front of a number when it turns it into a string. Do you know what I'm talking about?
Underscore here = space
Dim iNum as long
iNum = 256
Debug.Print CStr(iNum)
' debug window output
_256
Last edited by cafeenman; May 31st, 2002 at 11:17 PM.
-
May 31st, 2002, 11:23 PM
#14
Thread Starter
PowerPoster
Originally posted by Bruce Fox
Yep, cause the Format returns an Integer, and the Function returns a String.
I read the "Yes I knew that, CafeenMan" part of this reply, but not the rest of it.
Format returns a variant.
Format$ returns a string.
Why do you say it returns an integer? You're saying it's coerced? If it returns an integer, then the 0's will be dropped off, so it must get coerced into a string?
-
May 31st, 2002, 11:25 PM
#15
Interesting, what I find wierd is difference between our
results!
when I tested, it looked as if the Function (although Fixed value)
was around 500 times faster... I'm glad then that used a random
number sequence (to test the range).
In any case Animelion asked for the fastest way, and the Function
(generaly) is the way to go 
I guess, there may be some overhead associated with the FORMAT
function too.
Cheers,
Bruce.
-
May 31st, 2002, 11:26 PM
#16
Thread Starter
PowerPoster
I edited my post above to show the fastest LPAD function. The function will need to be modified if the number is a decimal, of course.
-
May 31st, 2002, 11:27 PM
#17
Originally posted by cafeenman
I read the "Yes I knew that, CafeenMan" part of this reply, but not the rest of it.
Format returns a variant.
Format$ returns a string.
Why do you say it returns an integer? You're saying it's coerced? If it returns an integer, then the 0's will be dropped off, so it must get coerced into a string?
S****, sorry I missed that too..... Hmmmm
-
May 31st, 2002, 11:28 PM
#18
Thread Starter
PowerPoster
You know, we probably shoud have reversed the order in which the functions get called to be more thorough. I think it may be possible that Format is getting penalized while background things are happening with VB during start up and the form loading. Maybe I'm wrong about that, but it would be the only way to fairly test them.
-
May 31st, 2002, 11:32 PM
#19
Thread Starter
PowerPoster
OK, I just tried two other things.
I changed Format to Format$ with no difference at all in the time.
Then I switched the order of the functions and LPAD with the number being a long type was still faster
371 LPAD vs. 470 for Format$
-
May 31st, 2002, 11:33 PM
#20
Hyperactive Member
One more Q
how can I open a text file and clear it's contents ?
-
May 31st, 2002, 11:33 PM
#21
I have retested with a few changes.
I placed the code in a cmdButton, and made ans1 String type.
Also Format$. (and used Int(Rnd * 999999) + 1)
The results were that same, the Function is faster.
Bruce.
-
May 31st, 2002, 11:34 PM
#22
Thread Starter
PowerPoster
Animelion - this turned out to be the fastest of what Bruce and I tried. Call it using the examples in previous posts.
VB Code:
Private Function LPAD(lngNumber As Long, Length As Integer) As String
LPAD = String$(Length - Len(lngNumber), "0") & sNumber
End Function
-
May 31st, 2002, 11:36 PM
#23
Thread Starter
PowerPoster
Re: One more Q
Originally posted by Animelion
how can I open a text file and clear it's contents ?
Using open for output erases the contents
VB Code:
Sub WriteFile ()
Dim i as Long
Dim sFilename as String
Dim iFilenum as Integer
sFilename = app.path & "\numbers.txt"
iFilenum = FreeFile
Open sFilename For Output As #iFilenum
For i = 1 to 50000
Print #iFilenum, LPAD(i, 6)
Next i
Close #iFilenum
End Sub
-
May 31st, 2002, 11:46 PM
#24
Thread Starter
PowerPoster
I just thought of something else. Using a string in a loop is bad news. This would probably increase the speed for the format function.
This way VB can pull it out of RAM instead of reallocating memory each time through the loop. I'm going to test again like this:
VB Code:
Const NUMBER_FORMAT As String = "000000"
x = GetTickCount 'Load the Initial count, prior to test
For i = 0 To 100000
sAnswer = Format(Int(Rnd * 999999) + 1, NUMBER_FORMAT)
Next
t1 = GetTickCount - x 'Determine time taken
' Test using LPAD
x = GetTickCount 'Load the Initial count, prior to test
For i = 1 To 100000
sAnswer = LPAD(Int(Rnd * 999999) + 1, 6)
Next
t2 = GetTickCount - x 'Determine time taken
-
May 31st, 2002, 11:49 PM
#25
Thread Starter
PowerPoster
I was wrong... no improvement at all declaring the format as a const.
-
Jun 1st, 2002, 12:00 AM
#26
Hyperactive Member
Check this
Dim sfilename As String
Dim x As String
Dim i As Long
sfilename = App.Path & "\NumberList.txt"
x = Text2 - 1
Open sfilename For Output As #1
For i = Text2 To Text3 'number to count to
x = x + 1
Print #1, LPAD(x, Text1)
Next i
Close #1
text 1 = number of 0's to pad with
text 2 = number to start at
text 3 = number to end at
For some reson it does not print to the file, however I know it is returning the right numbers.
-
Jun 1st, 2002, 12:01 AM
#27
Thanks for all that (side tracking ).
However, I think its good to know that not allways the easiest
path is the best (quickest).
Cheers,
Bruce.
-
Jun 1st, 2002, 12:10 AM
#28
Thread Starter
PowerPoster
I have no idea what you're doing here. You're declaring x as a string and trying to count with it. If Text2 and Text3 contain numeric values, convert them first.
Also, make sure the For loop is being entered by putting a breakpoint (F9) on the For line and then pressing F8 to step through.
Also, you're passing the string to LPAD instead of the Length of the string.
Try this instead:
VB Code:
Dim sfilename As String
Dim i As Long
dim lngMin as Long
dim lngMax as Long
dim iLen as Integer
lngMin= CLng(Text2.Text)
lngMax = CLng(Text3.Text)
iLen = CInt(Text1.Text)
sfilename = App.Path & "\NumberList.txt"
Open sfilename For Output As #1
For i = lngMin To lngMax ' number to count to
Print #1, LPAD(i, iLen)
Next i
Close #1
-
Jun 1st, 2002, 12:27 AM
#29
Hyperactive Member
My resons why...
Well.... In your version LPAD(i, iLen) is problematic.
You have i dim as an long
then your throw it into the LPAD thing, but only something dimed as a a string can be thrown into the LPAD thing.
As of now I get (ByRef argument type mismatch) and the i in LPAD(i, iLen) is highlighted.
Can I temporarily redim i as something else ???
Thanks for any help you can give.
-
Jun 1st, 2002, 12:31 AM
#30
Thread Starter
PowerPoster
This is what you should be using for LPAD:
VB Code:
Private Function LPAD(lngNumber As Long, Length As Integer) As String
LPAD = String$(Length - Len(lngNumber), "0") & lngNumber
End Function
-
Jun 1st, 2002, 12:34 AM
#31
Thread Starter
PowerPoster
Hang on while I actually check this out. I may be giving you bad info here. I'll post when I'm sure it's right.
-
Jun 1st, 2002, 12:40 AM
#32
Thread Starter
PowerPoster
OK, the problem is what I thought it might be. The Len(lngNumber) thing isn't giving the right answer, so the numbers don't line up in the text file. I have no idea how the time of what I'm posting here compares to what we did already, but this will do what you need:
VB Code:
Option Explicit
Private Sub Command_Click()
Dim i As Long
Dim lngMin As Long
Dim lngMax As Long
Dim iLen As Integer
Dim sFilename As String
sFilename = App.Path & "\NumberList.txt"
iLen = CInt(Text1.Text)
lngMin = CLng(Text2.Text)
lngMax = CLng(Text3.Text)
Open "c:\temp\numbers.txt" For Output As #1
For i = lngMin To lngMax ' number to count to
Print #1, LPAD(CStr(i), iLen)
Next i
Close #1
MsgBox "Finished"
End Sub
Private Function LPAD(sNumber As String, Length As Integer) As String
LPAD = String$(Length - Len(sNumber), "0") & sNumber
End Function
-
Jun 1st, 2002, 12:51 AM
#33
Hyperactive Member
Something is still wrong.
If I set the buffer to 10 0's I get this
higher number
0000001638
0000001639
lower numbers
0000000
0000001
??? - why would it just change like that
Is it calculating how many 0's to add based on the final number reached instead of the current number it is at ?
-
Jun 1st, 2002, 12:53 AM
#34
Thread Starter
PowerPoster
it works now. Read my previous post.
-
Jun 1st, 2002, 12:59 AM
#35
Hyperactive Member
Just checking....
I read the post, so your saying it doesn't work ?
I don't get this
"I have no idea how the time of what I'm posting here compares "
-
Jun 1st, 2002, 01:02 AM
#36
Thread Starter
PowerPoster
I'm saying the last code I posted does what you want it to do.
I do not know how the changes I made in that code compare in speed to the code we were testing. The code I last posted is slightly different than what we were testing for speed. But it does work. I put it in a project and it printed to the file with the numbers lined up as you want them to.
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
|