String manipulation, optimize

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rune

    String manipulation, optimize

    Is it best to use double quotes and let PHP expand variables inside strings,
    or is it faster to do the string manipulation yourself manually?

    Which is quicker?

    1)
    $insert = 'To Be';
    $sentence = "$insert or not $insert. That is the question.";

    or
    2)
    $insert = 'To Be';
    $sentence = $insert . ' or not ' . $insert . '. That is the question.';


    Small thing, but might as well get it right.

    Thanks
    /Rune


  • Alvaro G Vicario

    #2
    Re: String manipulation, optimize

    *** Rune wrote/escribió (Mon, 8 Aug 2005 10:46:18 +0200):[color=blue]
    > Which is quicker?[/color]
    [color=blue]
    > $sentence = "$insert or not $insert. That is the question.";[/color]
    [color=blue]
    > $sentence = $insert . ' or not ' . $insert . '. That is the question.';[/color]

    A quick-and-dirty benchmark in my old server showed the second code is
    twice as fast as the first one. I suggest you test it in your own server.

    However, you'll realize you need several thousand iterations to make a
    difference so just use whatever makes your code clearer to understand.
    (Unless, of course, you'll be manipulating several thounsand strings ;-)

    --
    -- Álvaro G. Vicario - Burgos, Spain
    -- http://bits.demogracia.com - Mi sitio sobre programación web
    -- Don't e-mail me your questions, post them to the group
    --

    Comment

    • Erwin Moller

      #3
      Re: String manipulation, optimize

      Rune wrote:
      [color=blue]
      > Is it best to use double quotes and let PHP expand variables inside
      > strings, or is it faster to do the string manipulation yourself manually?
      >
      > Which is quicker?
      >
      > 1)
      > $insert = 'To Be';
      > $sentence = "$insert or not $insert. That is the question.";
      >
      > or
      > 2)
      > $insert = 'To Be';
      > $sentence = $insert . ' or not ' . $insert . '. That is the question.';
      >
      >
      > Small thing, but might as well get it right.
      >
      > Thanks
      > /Rune[/color]

      Hi,

      I do not want to sound like your teacher/parent/whatever, BUT...

      Why do you care about minimal optimalizations ?
      In practice, do you expect ANYBODY to notice the difference?

      My point being:
      Write code YOU like.
      Write code YOU think is good readable.
      And have a little trust in the language you use.

      Almost ALL cases where I saw a performancedegr adation is was due to poorly
      constructed databasequeries , NOT because of 'slow language' constructs.

      I know I am not answering your question, but I just want to point out you
      better focus on other issues. :-)

      To answer your question:
      It depends.
      I wouldn't be surprised if you get different results on PHP4.3 than on PHP5.
      Or different results on W2000 and Wxp and Debian Sarge, and.. etc.etc.

      To get some idea on your current setup: Just run a benchmark.
      And even benchmarks can give you a wrong impression because of all kinds of
      smart optimizing happing behind your back by the engine.


      just my 2 cents.
      And again, sorry for sounding pedantic. :P

      Regards,
      Erwin Moller

      Comment

      • Rune

        #4
        Re: String manipulation, optimize

        Ok thanks both,

        You are right I should probably just stick with what seems the most
        readable - which is the double quotes for me.

        But I have some code which is running too slow and you're right it's the SQL
        which is performing badly. But I have been unable to really do anything
        about that, so I'm trying to get whatever else performance improvements I
        can otherwise. Probably it's not worth it, but on the other hand the string
        concatenations are inside a loop which is run up to a thousand times and
        sometimes up to a hundred thousand times.

        /Rune


        Comment

        • R. Rajesh Jeba Anbiah

          #5
          minimal (?) optimization (Was Re: String manipulation, optimize)

          Erwin Moller wrote:[color=blue]
          > Rune wrote:[/color]
          <snip>[color=blue]
          > Why do you care about minimal optimalizations ?[/color]
          <snip>

          Sorry, I can't agree with you. In the world of optimization, there is
          no minimal or maximal optimization--there is only onething, it's
          optimization. Consider a situation/expression in which you can improve
          the speed to 0.000001 second--and consider the same in 1000000 loop.
          So, IMHO, definitely it is worth to optimize, even it is less than a
          nano second thing.

          --
          <?php echo 'Just another PHP saint'; ?>
          Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com

          Comment

          • Chung Leong

            #6
            Re: String manipulation, optimize

            Ach, the meaningless debate that won't go away! The difference is so
            miniscule that no one really knows which one is faster in a real-life
            situations. Benchmark codes just give you bogus answers. The following
            snippet, for instance, shows that the concatenation is more than an
            order of magnitude slower than interpolation:

            function getmicrotime() {
            list($usec, $sec) = explode(" ", microtime());
            return ((float)$usec + (float)$sec);
            }

            $i = "hello";
            $a = array_fill(0, 30000, "\$i");
            $code1 = "\$s = " . implode(".", $a) . ";";
            $code2 = "\$s = \"" . implode("", $a) . "\";";

            $time_start = getmicrotime();
            eval($code1);
            $time_end = getmicrotime();
            $time1 = $time_end - $time_start;

            $time_start = getmicrotime();
            eval($code2);
            $time_end = getmicrotime();
            $time2 = $time_end - $time_start;

            echo "$time1 vs $time2";


            The bottomline: it doesn't matter.

            Comment

            • Erwin Moller

              #7
              Re: minimal (?) optimization (Was Re: String manipulation, optimize)

              R. Rajesh Jeba Anbiah wrote:
              [color=blue]
              > Erwin Moller wrote:[color=green]
              >> Rune wrote:[/color]
              > <snip>[color=green]
              >> Why do you care about minimal optimalizations ?[/color]
              > <snip>
              >
              > Sorry, I can't agree with you. In the world of optimization, there is
              > no minimal or maximal optimization--there is only onething, it's
              > optimization. Consider a situation/expression in which you can improve
              > the speed to 0.000001 second--and consider the same in 1000000 loop.
              > So, IMHO, definitely it is worth to optimize, even it is less than a
              > nano second thing.[/color]

              Hi, Rajesh

              Well of course.
              If you traverse through a loop zillions times, it is worth it.
              I was only pointing out that in almost all cases i see, real optimalization
              can be achieved by examining your databasequeries .

              For example: The fact that you are looping a zillion times on some results
              from a database, is an indication that you better learn SQL a little
              better, and let the database do that calculations.
              That is faster.

              Of course you can find situations where you need it, mostly mathematical
              issues, like numerical solutions for certain problems.
              But in everyday life for a webdeveloper, in most situations: Make better
              queries.

              So we do not have a contradiction here.
              I was making a statement about looping over databaseresults , where you are
              making a more general statement.

              Regards,
              Erwin Moller
              [color=blue]
              >
              > --
              > <?php echo 'Just another PHP saint'; ?>
              > Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com[/color]

              Comment

              • Jerry Stuckle

                #8
                Re: minimal (?) optimization (Was Re: String manipulation, optimize)

                R. Rajesh Jeba Anbiah wrote:[color=blue]
                > Erwin Moller wrote:
                >[color=green]
                >>Rune wrote:[/color]
                >
                > <snip>
                >[color=green]
                >>Why do you care about minimal optimalizations ?[/color]
                >
                > <snip>
                >
                > Sorry, I can't agree with you. In the world of optimization, there is
                > no minimal or maximal optimization--there is only onething, it's
                > optimization. Consider a situation/expression in which you can improve
                > the speed to 0.000001 second--and consider the same in 1000000 loop.
                > So, IMHO, definitely it is worth to optimize, even it is less than a
                > nano second thing.
                >
                > --
                > <?php echo 'Just another PHP saint'; ?>
                > Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com
                >[/color]

                Rajesh,

                Sorry, this is an overly simplistic view. The IT industry generally recognizes
                several levels of optimization.

                Optimization is always a trade off. It might be between development time vs.
                run time or a trade off between memory, disk space and runtime, for instance.

                The problem is there are multiple ways to optimize. In the questioned case,
                minimal optimization might be to run samples of both and determine which is
                faster in this instance.

                A higher level of optimization might be to run benchmarks on all the code to
                determine what is taking the longest time and looking for ways to improve code
                speed there. This would be important, for instance, if the server is becoming
                heavily loaded.

                If the server is completely overloaded and something serious has to be done, the
                highest level of optimization would be to rewrite all the code in assembler.
                Of course, it would take the longest. But it would have the highest level of
                optimization possible for the user code.

                Obviously there are other optimizations which may be made as well. For
                instance, RDB's. Fully normalized databases (5th normal) have the least amount
                of duplicate data - but are not the most efficient space wise, and definitely
                not performance-wise. Most databases are closer to 3rd normal form which is
                more efficient space wise than 2nd form normal, but less efficient in
                processing. Where performance is important, sometimes it's better to regress
                critical parts of the database to 2nd normal form for performance.

                The above examples are just the tip of the iceberg. Optimization is not black
                and white, as you indicate. There are many, many levels of optimization involved.

                --
                =============== ===
                Remove the "x" from my email address
                Jerry Stuckle
                JDS Computer Training Corp.
                jstucklex@attgl obal.net
                =============== ===

                Comment

                • R. Rajesh Jeba Anbiah

                  #9
                  Re: String manipulation, optimize

                  Chung Leong wrote:[color=blue]
                  > Ach, the meaningless debate that won't go away! The difference is so
                  > miniscule that no one really knows which one is faster in a real-life
                  > situations. Benchmark codes just give you bogus answers. The following
                  > snippet, for instance, shows that the concatenation is more than an
                  > order of magnitude slower than interpolation:
                  >
                  > function getmicrotime() {
                  > list($usec, $sec) = explode(" ", microtime());
                  > return ((float)$usec + (float)$sec);
                  > }[/color]
                  <snip>

                  I know, Chung Leong is advocating no optimization doctrine for a
                  long time. But, I would like to point out that your *benchmarking*
                  procedure is flawed in optimization world. We have APD and other tools,
                  your microtime() based benchmarking is not acceptable.

                  --
                  <?php echo 'Just another PHP saint'; ?>
                  Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com

                  Comment

                  • R. Rajesh Jeba Anbiah

                    #10
                    Re: minimal (?) optimization (Was Re: String manipulation, optimize)

                    Jerry Stuckle wrote:[color=blue]
                    > R. Rajesh Jeba Anbiah wrote:[/color]
                    <snip[color=blue][color=green]
                    > > Sorry, I can't agree with you. In the world of optimization, there is
                    > > no minimal or maximal optimization--there is only onething, it's
                    > > optimization. Consider a situation/expression in which you can improve
                    > > the speed to 0.000001 second--and consider the same in 1000000 loop.
                    > > So, IMHO, definitely it is worth to optimize, even it is less than a
                    > > nano second thing.[/color][/color]

                    <snip explanation and examples on optimization>
                    [color=blue]
                    > The above examples are just the tip of the iceberg. Optimization is not black
                    > and white, as you indicate. There are many, many levels of optimization involved.[/color]

                    True that we don't have enterprise level code metrics tools (like
                    devpartner) for PHP. It is true that there will be a time where the
                    testing department will do the code metrics, performance/stress testing
                    and the development team will again sit and try to improve on the next
                    cycle. But, my point is that, always better to code based on set of
                    coding standards that even care what is minimal optimization--so, that
                    in the next cycle you'd be better off spending time in improving other
                    things than doing what is known (though it proivides minimal effect).

                    My concern is that newbies shouldn't be impressed to follow "build
                    crap-remove crap" kind life cycles. FWIW, my humble opinion is to form
                    a coding standard that even cares these (what is trivial) things first
                    and work on that.

                    --
                    <?php echo 'Just another PHP saint'; ?>
                    Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com

                    Comment

                    Working...