Click to See Complete Forum and Search --> : Left or Left$
Juan Carlos Rey
Jan 15th, 2000, 09:23 AM
I think both XxEvilxX and wossname are pouring water outside the flower vase! :)
"Left$" is a function in Q Basic to retrieve a portion of a string counting from left side, and "Rigth$" is the like for right side.
In VB they are "Left" and "Right", respectively.
Right$ and Left$ still work only for the sake of compatibility but they are not guaranteed to work in VB7 or future versions.
chrisjk
Jan 15th, 2000, 11:02 AM
Hey guys
What's the difference between Left and Left$ or Right and Right$?
Help doesn't explain very well.
Thanks
Regards,
------------------
- Chris
chris.kilhams@btinternet.com
If it ain't broke - don't fix it :)
XxEvilxX
Jan 15th, 2000, 11:33 AM
Well Hey The $ After Right or left Just Means That Left$ = String Cuase $ means string and The Same As Right$
OK, im going to set a trend now and build a coherent sentence...
always use Left$ or right$ when you are using strings, because it is about 60% faster than without the $. this is to do with the memory allocation routines that Windows uses for strings. Trust me. plus $ does bad things to numbers in certain circumstances!
Rad_Brad
Jan 16th, 2000, 06:04 AM
Sorry, Juan Carlos, XxEvilxX and wossname are right. Left$ and Right$ work at least twice as fast, especially when doing work with arrays and collections, than your Left and Right functions. There are also other functions that most of us use that have a similar $ counterpart function that you can use with strings.
Granted, both were old QBasic functions, but the reason they were held over is because of their speed. I don't think they would have kept them through 6 versions of a different language for no apparent reason, do you?
Rad Brad
Al Smith
Jan 16th, 2000, 06:42 AM
Oh No!
I assume the same is true for mid$ and right$. I've now got a ton of code in several programs that I now have to edit.
Sometimes ignorance IS bliss.
Al.
------------------
A computer is a tool, not a toy.
<A HREF="mailto:asmith3914@aol.com
asmith@spxateg.com">asmith3914@aol.com
asmith@spxateg.com</A>
Clunietp
Jan 16th, 2000, 06:45 AM
I can't find any documentation on the differences between Left and Left$....how do you guys know that it is faster?
I can find much of a difference - left is faster to type then left$ so I just use that!
:)
Juan Carlos Rey
Jan 16th, 2000, 09:50 AM
Rad_Brad:
If you read carefully the XxEvilxX answer, at least as I interpret it, he is talking about data types, as "Porky" being an Integer and "Porky$" a String.
With wossname there are doubts left, but I assume the same, he is talking about data types.
In my VB 4.0 Help I found something interesting:
"Algunas funciones tienen dos versiones, una que devuelve un tipo de dato Variant y otra que devuelve un tipo de dato String. Las versiones de variante tienden a ser las más convenientes ya que los variantes manejan las conversiones entre los diferentes tipos de dato automáticamente."
And this means approximately:
"Some functions have two versions, one returns a Variant data type, the other a String one. Variants tend to be more appropriate because they manage automatically the conversion between different data types"
Well, it is true that working with Variants is slower than with Strings. That can be a clue for the speed increase.
And to Al Smith: yes, you are right, the same is for mid$ and others:
"Las siguientes funciones devuelven valores en una variable de tipo String cuando se agrega un signo de dólar ($) al nombre de la función. Estas funciones tienen el mismo uso y sintaxis que sus equivalentes de variante sin el signo de dólar:
Chr$ - ChrB$ - CurDir$ - Date$ - Dir$ - Error$ - Format$ - Hex$ - Input$ - InputB$ - LCase$ - Left$ - LeftB$ - Mid$ - MidB$ - Oct$ - Right$ - RightB$ - RTrim$ - Space$ - Str$ - String$ - Time$ - Trim$ - UCase$"
I leave the translation to you as a home work. :)
Well, just to test the speed, I tried the following code:
-------
Private Sub Command1_Click()
Text1.Text = "Who and see to sake ago in lost pant alone east"
StartTime = Timer
For i = 1 To 5000
Text2.Text = Left$(Text1.Text, 20)
Text3.Text = Right$(Text1.Text, 20)
Next
Text4.Text = Timer - StartTime
End Sub
-------
Run it three ore four times, record the times (in the order of 6.4 seconds in my Pentium S 166 MHz)
Changed Left$ to Left and Right$ to Right, repeated the test, and obtained the same times. Perhaps there are better routines to test speed, but for me it's enough.
[This message has been edited by Juan Carlos Rey (edited 01-16-2000).]
Al Smith
Jan 16th, 2000, 10:06 AM
Rough translation?
The following functions return values in a variable of string type when a dollar sign is attached to them. These functions have the same use and syntax as their equivelents without the dollar sign.
Al.
Al.
------------------
A computer is a tool, not a toy.
<A HREF="mailto:asmith3914@aol.com
asmith@spxateg.com">asmith3914@aol.com
asmith@spxateg.com</A>
Juan Carlos Rey
Jan 16th, 2000, 10:36 AM
I dare to correct Al a litlle:
"These functions have the same use and syntax as their Variant equivalents without the dollar sign"
chrisjk
Jan 16th, 2000, 11:15 AM
Have I started an aurgument? Does this mean no-one really knows the difference? well, just incase Left$/Right$ is quicker, I'm going to replace all instances of Left or Right in my programs.
Juan, I appologise for being typically English, but is that Spanish I see...?
Thanks all
kind regards,
------------------
- Chris
chris.kilhams@btinternet.com
If it ain't broke - don't fix it :)
Crazy D
Jan 16th, 2000, 03:06 PM
The answer is given...
The $-versions of the function return a string (Left$) while the other (Left) is returning a variant, so a conversion is needed when you assign the return value to a string. (but then, Variants are always slower and consume more memory then the 'real' type variables)
Wonder though when you start to notice the difference in speed.
Juan Carlos Rey
Jan 17th, 2000, 12:18 AM
chrisjk:
1- yes, it seems you started an argument...
2- today I opened HELP in my VB 6 and couldn't find any reference to "left$", all links pointed toward "left" and nothing was told about variants, strings, etc.
3- I wouldn't care to replace Left with Left$ as I couldn't see any speed improvement at all (you'll see in my code each conversion was done 5000 times!)
4- If that argentine dialect could be called Spanish...
I have Spanish version of VB 4, I just copied/pasted in sake of precision. My translation could be not too accurate.
regards :)
[This message has been edited by Juan Carlos Rey (edited 01-17-2000).]
just because you cant find something in a help file doesnt qualify you to discount other peoples knowledge of something.
if you use it in very heavy string manipulations app's then you will find a noticable difference in the amount of time between $ and non-$ functions, can be as much as 7 minutes over an hour of hard calculations. I have used it when manipulating names and addresses from CSV files containing hundreds of thousands of people's details. I know what i am talking about
Frans C
Jan 17th, 2000, 01:29 AM
I compared the two in a loop of 50000, and noticed the $ version is about 1 % faster then the other one. Not enough to spend time changing my code.
Frans C
Jan 17th, 2000, 01:40 AM
I have to correct myselve. I used the code of Juan Carlos, but this appears not to be a correct test. Inside the loop, two properties are set, and this takes 99 % of the time. I changed assigning the result to a property to assigning the result to a variable, and there was a dramatic change.
The $ version is 63.4 % faster then the other one, so there is a good reason to use it if used a lot.
Juan Carlos Rey
Jan 17th, 2000, 07:23 AM
Oh, wossname, I am not trying to argue!
I didn't mean to disqualify nobody, I am still an apprentice. If you see my previous post, in VB 4.0 I found the difference, they said "Left$" returns a string and "Left" a Variant, but in VB 6 they did not mention a word about!
Please don't Blame.Me! :)
Frans C, can I have a look at your code? Because your results are consistent with wossname 60 % faster assertion!
Gimpster
Jan 17th, 2000, 07:52 AM
Juan Carlos Rey. When you ran your test program and got the same times, could the reason for that be because you used a timer?
I couldn't quite follow your code through, it looked like there were some things missing. Anyway, just a thought.
------------------
Ryan
JeffSM
Jan 17th, 2000, 10:17 AM
wossname
You are sure, the help doesn't metter, 'cause Microsoft could forget to include theese functions in VB help (or they don't care - that is worst).
I know that functions with $ is faster since VB4 (the first version that I learn)!
Jefferson
Clunietp
Jan 17th, 2000, 11:19 AM
I used this code, only changing the Left to Left$, and got less than a 1% performance increase by using Left$....
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Form_Load()
Dim strString As String
Dim I As Long
Dim lngBegin As Long
Dim lngEnd As Long
Dim strTemp As String
lngBegin = GetTickCount
For I = 1 To 10000
strString = strString & "asdf"
Next I
For I = 1 To Len(strString)
strTemp = Left$(strString, I)
Next I
lngEnd = GetTickCount
MsgBox lngEnd - lngBegin
End
End Sub
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.