dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 41

Thread: How to "untrim" variable?

  1. #16
    Tony Smith Guest

    Re: How to "untrim" variable?

    I've always loved this argument.

    It comes down to 'what's it worth?'. Time is money. Odds are, this
    line of code gets executed once only, so a sub-millisecond time saving
    is worth zero.

    However, someone, someday, is going to eyeball that line of code. In
    this case, the most easily understood code is necessary. In a
    personal app, or if it absolutely needed to fast, I'd use LSet or
    Mid. Very few people understand LSet. (Ain't in VBScript, either).
    Few people understand putting Mid() on the left-hand side of an
    equation either.

    In a app for others, I'd use Left$(A$ & Space$(10), 10). This is a
    personal habit from the days before Format(), but most people can
    figure it out. Quickly. Cheaply!

    I rarely time my code these days. If it 'seems' slow I'll take a
    look, but if it works, I'm leaving it as is.

    Tony


    Phil Weber wrote:
    >
    > > You seem to have ignored the point I made about
    > > skill-level.

    >
    > Rick: I think less-experienced developers are better served by being taught
    > which techniques are inefficient so they can avoid getting into the habit of
    > using them. Format$ is such a poor performer that I believe it should be
    > avoided except in cases where it saves a great deal of effort on the
    > developer's part. This is not such a case. It would have been a disservice
    > to John (and other readers of this thread) to have not pointed out the
    > performance differences between Format$ and the other solutions presented.
    > ---
    > Phil Weber


  2. #17
    Bill McCarthy Guest

    Re: How to "untrim" variable?

    Hi Phil & Rick,

    I would have to agree with Phil regarding avoiding using the format
    function. Format$ is indeed a powerful function, but simply isn't required
    for this task. The power of the format function has a speed (and probably
    memory) overhead that goes with it.

    However, on my system, the LSet and Mid$ methods both came in around the
    same time (seems to be alternating between which one is faster by only a few
    ms). My personal recommendation would be to use the Mid$ function, although
    at times I could see real benifit by using LSet. The timing for FL strings
    is misleading in that it does not include the setup time for the string (IOW
    different when compared to calling a function). Also I tend to steer well
    clear of FL strings because of the slowness associated with them for other
    string tasks. The only time I think FL's should be used is when you have to
    , such as file IO.

    Well that's my $0.02

    Bill.


    "Phil Weber" <pweber@teleport.com> wrote in message
    news:390c45b7@news.devx.com...
    > > You seem to have ignored the point I made about
    > > skill-level.

    >
    > Rick: I think less-experienced developers are better served by being

    taught
    > which techniques are inefficient so they can avoid getting into the habit

    of
    > using them. Format$ is such a poor performer that I believe it should be
    > avoided except in cases where it saves a great deal of effort on the
    > developer's part. This is not such a case. It would have been a disservice
    > to John (and other readers of this thread) to have not pointed out the
    > performance differences between Format$ and the other solutions presented.
    > ---
    > Phil Weber
    >
    >




  3. #18
    Rick Rothstein Guest

    Re: How to "untrim" variable?

    > Rick: I think less-experienced developers are better served by
    > being taught which techniques are inefficient so they can avoid
    > getting into the habit of using them.


    We agree to disagree then.

    > Format$ is such a poor performer that I believe it should be
    > avoided except in cases where it saves a great deal of effort
    > on the > developer's part.



    It works so quickly up here in the "human" world that I usually don't give
    it a second thought to its turtle like pace in the "computer" world. (See
    Tony Smith's post for some additional thoughts on this -- all of which I
    agree with -- thanks Tony). Yes, if my app needed to perform 100,000 Format
    statements in a row, I might pause to consider it, but seriously, for one
    execution?


    > This is not such a case. It would have been a disservice
    > to John (and other readers of this thread) to have not pointed
    > out the performance differences between Format$ and the
    > other solutions presented.


    I have no problem with your doing that. It is definitely a good idea to
    expose people to performance issues. I just wish you would have scaled those
    reported times back to seconds per single execution rather than for an
    unreported 100,000 loops in order to give a feel for what this might mean in
    *perceived* time. At 0.00000841 of a second to execute a single Format
    statement as compared to 0.0000008 of a second for the LSet statement, in an
    app that only will execute this only one time at a particular moment under
    consideration, I just don't think it matters.

    Rick





  4. #19
    Audie F. Thomas Guest

    Re: How to "untrim" variable?

    While it is a matter of opinion, I have to disagree that the Format$
    is more readable. Unless you are already familiar with the cryptic
    symbols used by this function it is anything but readable. To make
    matters worse, the MS documentation on the function is totally
    inadequate and incomplete (as usual).

    --
    Regards,
    Audie F. Thomas
    Audie Technology, Inc.
    sales@audietech.com



  5. #20
    Phil Weber Guest

    Re: How to "untrim" variable?

    > Someone, someday, is going to eyeball that line of code.
    > In this case, the most easily understood code is necessary.
    > In a personal app, or if it absolutely needed to fast, I'd use
    > LSet or Mid$...In an app for others, I'd use Left$.


    Tony: So what you're saying is that not only is Format$ the slowest
    technique, but it's not particularly readable either. I completely agree.
    ---
    Phil Weber



  6. #21
    Phil Weber Guest

    Re: How to "untrim" variable?

    > PW: I think less-experienced developers are better served
    > by being taught which techniques are inefficient so they can
    > avoid getting into the habit of using them.
    >
    > RR: We agree to disagree then.


    Rick: You don't think beginners should learn how to write efficient code?
    Boy, we *do* disagree.

    > [Format$] works so quickly up here in the "human" world
    > that I usually don't give a second thought to its turtle-like
    > pace in the "computer" world...Yes, if my app needed to
    > perform 100,000 Format statements in a row, I might pause
    > to consider it, but seriously, for one execution?


    A software application is a collection of hundreds or thousands of
    individual operations. Using a slower technique for any one of them may seem
    insignificant, but if you get into the habit of writing inefficient code
    because, after all, "how much difference could one statement make?", you'll
    end up with slow, bloated apps.

    In the article "Shift Your Code Into Overdrive" in the January, 1996 issue
    of VBPJ, Joe Robison, Scott Swanson and Drew Fletcher, members of
    Microsoft's VB development team, wrote: "Keep in mind that optimization is
    not a single set of tricks or techniques. It's not a simple process you can
    tack on the end of your development cycle, as if you were thinking, 'There,
    it works. Now I'll speed it up and make it smaller.' To create a truly
    optimized application, you must be optimizing it all the time while it is
    being developed. You choose your algorithms carefully, weighing speed
    against size and other constraints."

    A classic example of this was a Visual Basic accounting package (Microsoft
    Profit) written by Great Plains Software and marketed by Microsoft several
    years ago. The software performed very poorly, so the developers dug into
    the code to try to optimize it. They discovered that the code used string
    compares (If A$ = "" Then...) throughout to determine whether a string
    variable contained data. Simply by changing those hundreds of statements to
    a more efficient numeric comparison (If Len(A$) = 0 Then...), the developers
    were able to appreciably improve the app's performance and reduce its memory
    requirements.

    During development, the developers may very well have thought, "Sure, a
    numeric compare is twice as fast as a string compare, but who's going to
    notice the difference between 0.0004 and 0.0002 milliseconds?" When that
    seemingly insignificant difference was multiplied hundreds of times
    throughout the application, however, it made the app's performance
    noticeably sluggish.
    ---
    Phil Weber



  7. #22
    Tony Smith Guest

    Re: How to "untrim" variable?

    Phil Weber wrote:
    >

    <snip>
    >
    > In the article "Shift Your Code Into Overdrive" in the January, 1996 issue
    > of VBPJ, Joe Robison, Scott Swanson and Drew Fletcher, members of
    > Microsoft's VB development team, wrote: "Keep in mind that optimization is
    > not a single set of tricks or techniques. It's not a simple process you can
    > tack on the end of your development cycle, as if you were thinking, 'There,
    > it works. Now I'll speed it up and make it smaller.' To create a truly
    > optimized application, you must be optimizing it all the time while it is
    > being developed. You choose your algorithms carefully, weighing speed
    > against size and other constraints."
    >
    > A classic example of this was a Visual Basic accounting package (Microsoft
    > Profit) written by Great Plains Software and marketed by Microsoft several
    > years ago. The software performed very poorly, so the developers dug into
    > the code to try to optimize it. They discovered that the code used string
    > compares (If A$ = "" Then...) throughout to determine whether a string
    > variable contained data. Simply by changing those hundreds of statements to
    > a more efficient numeric comparison (If Len(A$) = 0 Then...), the developers
    > were able to appreciably improve the app's performance and reduce its memory
    > requirements.
    >
    > During development, the developers may very well have thought, "Sure, a
    > numeric compare is twice as fast as a string compare, but who's going to
    > notice the difference between 0.0004 and 0.0002 milliseconds?" When that
    > seemingly insignificant difference was multiplied hundreds of times
    > throughout the application, however, it made the app's performance
    > noticeably sluggish.
    > ---
    > Phil Weber



    Ah, but the program worked properly. That's the point. Get to work,
    optimise it later. It's quite possible that Microsoft could make the
    VB compiler smarter, so A$="" becomes Len(A$)=0. The more readable
    A$="" is now perfectly ok.

    I'd rather teach a newcomer the most basic methods, rather than
    confuse them with obscure techniques. The aim of the exercise is to
    see if a string is blank. A$="" does that perfectly. It's like the C
    trick of dividing by 2 by doing a bitshift. Get it wrong, and your
    program is toast. And get it wrong they do!

    I remember when QuickBasic/PDS started using far memory for strings,
    many of my optimisation tricks simply died... Look at how many books
    say that declaring variables as Integer produced the fastest code.
    Not true these days.

    Tony

  8. #23
    Tony Smith Guest

    Re: How to "untrim" variable?

    Actually, I don't mind Format(). Most people are familiar with t,
    after all, you need it to do date formatting. I don't use it for
    general string formatting simply because of habit.

    I do teach other people to use it though. They seem pretty happy with
    it. It's just familiarity.

    Tony


    Phil Weber wrote:
    >
    > > Someone, someday, is going to eyeball that line of code.
    > > In this case, the most easily understood code is necessary.
    > > In a personal app, or if it absolutely needed to fast, I'd use
    > > LSet or Mid$...In an app for others, I'd use Left$.

    >
    > Tony: So what you're saying is that not only is Format$ the slowest
    > technique, but it's not particularly readable either. I completely agree.
    > ---
    > Phil Weber


  9. #24
    Phil Weber Guest

    Re: How to "untrim" variable?

    > Ah, but the program worked properly. That's
    > the point. Get [it] to work, optimise it later.


    Tony: If you've ever had time to go back and "optimise it later," you live
    in a different world than I do. Doesn't it make more sense to code it right
    the first time, especially if you can do so with little or no additional
    effort?
    ---
    Phil Weber



  10. #25
    Rick Rothstein Guest

    Re: How to "untrim" variable?

    Boy, everything is black or white with you, huh? You just won't acknowledge
    that a beginner must crawl before they walk (or in the case of your
    proposals -- run -- skipping walking altogether).

    I didn't say they *never* should learn optimization, just not at the
    beginning. Isn't it better for a beginner to be able to do many things with
    VB (kind of encourages them to stay in the "game", wouldn't you think)
    rather than just one thing efficiently -- especially if he is not sure of
    how it fits in with the rest of the VB world? Go back and take a look at
    John K's original posted code again. He is supposed to be trying to left
    justify a string of characters within a larger string of spaces -- for God's
    sake, he was trying to use the Rnd function to accomplish this!

    Do you really think he is ready to contemplate the finer points of code
    optimization? Or do you think he might rather get a handle on some standard
    commands that will allow him to do begin doing things with the language? My
    vote is with the latter. Sure the documentation is not the best for the
    Format statement; but truthfully, doesn't it fall a little short for LSet
    too (as well as many other commands)? I don't assume learning the Format
    function (or any other in command VB) will come to John K without some work
    on his part. I expect him to search out information and help elsewhere (web
    sites, books, whatever). All I was saying regarding Format is that once he
    learns it, it will provide him with the flexibility to accomplish many
    things in VB all from a single function.

    As to your "Shift Your Code Into Overdrive" reference . . . I repeat, I
    didn't say he should not learn to optimize -- just not initially. Once he
    "masters" the basics and at that point becomes dissatisfied with the speed
    of its execution, then he will be ready to search out the various
    optimization techniques -- not before. And once he begins learning them,
    then he can incorporate them into his future projects (right at the initial
    stages). In other words he must "mature" in his use of, and appreciation
    for, the language in order to master its finer points. You just shouldn't
    throw them all at him in the beginning.

    As to the Microsoft Profit program -- I really don't think John K posted the
    question he did because he is writing a program of equal complexity. With
    respect to this whole thread, I think your problem is you know too much. I
    really don't think you can step back to the very beginnings and see the
    problem of getting started in programming from the eyes of a new or
    relatively new programmer.

    Rick



  11. #26
    Tony Smith Guest

    Re: How to "untrim" variable?

    Well, it's a chicken & egg situation. It's nice if it works well the
    first time. I always thought optimisation was fixing it after someone
    complains it's slow!

    Actually, I sometimes cringe when I write something, and 'dumb it
    down'. In the long run, I know it's going to haunt me if I do
    something 'clever'. I have written stuff when six months later -
    'what the ****?'.

    A firm I worked for once hired a VB programmer. A few weeks later he
    asked - 'How do I get a value out of a subroutine?'. No joke. I
    didn't know what to say. He solved his own problem 'Oh, don't worry,
    I just make everything global'. I left soon after... but everything I
    showed him was generally the 'easy' way, so his code at least worked.

    Want to talk about standards next, or will we leave that for another
    thread?

    Tony


    Phil Weber wrote:
    >
    > > Ah, but the program worked properly. That's
    > > the point. Get [it] to work, optimise it later.

    >
    > Tony: If you've ever had time to go back and "optimise it later," you live
    > in a different world than I do. Doesn't it make more sense to code it right
    > the first time, especially if you can do so with little or no additional
    > effort?
    > ---
    > Phil Weber


  12. #27
    Rick Rothstein Guest

    Re: How to "untrim" variable?

    I'd like to apologize for the "tone" of my previous response. It was written
    at 4:15 am (that is the wee hours of the morning here) and the words I used
    in the post seem to have a little bit of an "edge" to them. That was
    unintentional; I really do try to post responses in newsgroups that are a
    little more "reserved" than perhaps those words show. With that said, I
    still stand by the ideas and positions expressed therein.

    Rick


    "Rick Rothstein" <rick_newsgroup@email.com> wrote in message
    news:390d3d02$1@news.devx.com...
    > Boy, everything is black or white with you, huh? You just won't

    acknowledge
    > that a beginner must crawl before they walk (or in the case of your
    > proposals -- run -- skipping walking altogether).
    >
    > I didn't say they *never* should learn optimization, just not at the
    > beginning. Isn't it better for a beginner to be able to do many things

    with
    > VB (kind of encourages them to stay in the "game", wouldn't you think)
    > rather than just one thing efficiently -- especially if he is not sure of
    > how it fits in with the rest of the VB world? Go back and take a look at
    > John K's original posted code again. He is supposed to be trying to left
    > justify a string of characters within a larger string of spaces -- for

    God's
    > sake, he was trying to use the Rnd function to accomplish this!
    >
    > Do you really think he is ready to contemplate the finer points of code
    > optimization? Or do you think he might rather get a handle on some

    standard
    > commands that will allow him to do begin doing things with the language?

    My
    > vote is with the latter. Sure the documentation is not the best for the
    > Format statement; but truthfully, doesn't it fall a little short for LSet
    > too (as well as many other commands)? I don't assume learning the Format
    > function (or any other in command VB) will come to John K without some

    work
    > on his part. I expect him to search out information and help elsewhere

    (web
    > sites, books, whatever). All I was saying regarding Format is that once he
    > learns it, it will provide him with the flexibility to accomplish many
    > things in VB all from a single function.
    >
    > As to your "Shift Your Code Into Overdrive" reference . . . I repeat, I
    > didn't say he should not learn to optimize -- just not initially. Once he
    > "masters" the basics and at that point becomes dissatisfied with the speed
    > of its execution, then he will be ready to search out the various
    > optimization techniques -- not before. And once he begins learning them,
    > then he can incorporate them into his future projects (right at the

    initial
    > stages). In other words he must "mature" in his use of, and appreciation
    > for, the language in order to master its finer points. You just shouldn't
    > throw them all at him in the beginning.
    >
    > As to the Microsoft Profit program -- I really don't think John K posted

    the
    > question he did because he is writing a program of equal complexity. With
    > respect to this whole thread, I think your problem is you know too much. I
    > really don't think you can step back to the very beginnings and see the
    > problem of getting started in programming from the eyes of a new or
    > relatively new programmer.
    >
    > Rick
    >
    >




  13. #28
    Alan Gillott Guest

    Re: How to "untrim" variable?

    ....and VB is now inheriting other techniques which effectively make format
    obscolete:
    FormatDateTime
    FormatCurrency
    CStr (I use this a lot for timestamps etc.)
    Str
    etc...
    Format seems to have been eliminated in VBScript

    Audie F. Thomas <audietech@erols.com> wrote in message
    news:390c9b56@news.devx.com...
    > While it is a matter of opinion, I have to disagree that the Format$
    > is more readable. Unless you are already familiar with the cryptic
    > symbols used by this function it is anything but readable. To make
    > matters worse, the MS documentation on the function is totally
    > inadequate and incomplete (as usual).
    >
    > --
    > Regards,
    > Audie F. Thomas
    > Audie Technology, Inc.
    > sales@audietech.com
    >
    >




  14. #29
    Brent Guest

    Re: How to "untrim" variable?


    Lets just say that if people come here they want to LEARN!?!

    What you all have done is appropriate, you provided examples of how to solve
    the question. If John posted his question here to LEARN then John, being
    the intelligent, knowledge seeking person he is, went and did further research
    (scanned the On-Line help for more detailed information about each of the
    suggested solutions. If John posted his question here to merely get his
    task done faster and could care less about what each provided solution accomplishes
    then he has done himself and all others here a disservice.

    Lets all hope that John is the John in the first scenario...

    But I digress, I come here to see how others accomplish certain tasks, if
    I see something I don't understand then I do my own research to figure it
    out (I come here to LEARN, and help other LEARN).

    By getting into a bickering competition with one another all you have accomplished
    is diluting the importance of the original question and the missed an opportunity
    to explain the pros and cons of each solution (would have made the dialect
    more interesting and more informative).

    Just my $0.02 worth......

    "Phil Weber" <pweber@teleport.com> wrote:
    > > PW: I think less-experienced developers are better served
    > > by being taught which techniques are inefficient so they can
    > > avoid getting into the habit of using them.
    > >
    > > RR: We agree to disagree then.

    >
    >Rick: You don't think beginners should learn how to write efficient code?
    >Boy, we *do* disagree.
    >
    > > [Format$] works so quickly up here in the "human" world
    > > that I usually don't give a second thought to its turtle-like
    > > pace in the "computer" world...Yes, if my app needed to
    > > perform 100,000 Format statements in a row, I might pause
    > > to consider it, but seriously, for one execution?

    >
    >A software application is a collection of hundreds or thousands of
    >individual operations. Using a slower technique for any one of them may

    seem
    >insignificant, but if you get into the habit of writing inefficient code
    >because, after all, "how much difference could one statement make?", you'll
    >end up with slow, bloated apps.
    >
    >In the article "Shift Your Code Into Overdrive" in the January, 1996 issue
    >of VBPJ, Joe Robison, Scott Swanson and Drew Fletcher, members of
    >Microsoft's VB development team, wrote: "Keep in mind that optimization

    is
    >not a single set of tricks or techniques. It's not a simple process you

    can
    >tack on the end of your development cycle, as if you were thinking, 'There,
    >it works. Now I'll speed it up and make it smaller.' To create a truly
    >optimized application, you must be optimizing it all the time while it is
    >being developed. You choose your algorithms carefully, weighing speed
    >against size and other constraints."
    >
    >A classic example of this was a Visual Basic accounting package (Microsoft
    >Profit) written by Great Plains Software and marketed by Microsoft several
    >years ago. The software performed very poorly, so the developers dug into
    >the code to try to optimize it. They discovered that the code used string
    >compares (If A$ = "" Then...) throughout to determine whether a string
    >variable contained data. Simply by changing those hundreds of statements

    to
    >a more efficient numeric comparison (If Len(A$) = 0 Then...), the developers
    >were able to appreciably improve the app's performance and reduce its memory
    >requirements.
    >
    >During development, the developers may very well have thought, "Sure, a
    >numeric compare is twice as fast as a string compare, but who's going to
    >notice the difference between 0.0004 and 0.0002 milliseconds?" When that
    >seemingly insignificant difference was multiplied hundreds of times
    >throughout the application, however, it made the app's performance
    >noticeably sluggish.
    >---
    >Phil Weber
    >
    >



  15. #30
    Phil Weber Guest

    Re: How to "untrim" variable?

    > Missed an opportunity to explain the pros and
    > cons of each solution.


    Brent: I thought I did explain the pros and cons of each solution. All the
    suggested solutions work, but some work much more efficiently than others. I
    posted timings earlier in the thread so that people who are interested in
    writing efficient code can make an informed choice.
    ---
    Phil Weber



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center