SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What is considered "faster?"

    PHP Code:
    echo 'Go here'"\n";
    //or
    echo "Go here\n"
    On a much larger scale of course.

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    It such a small difference it doesn't even matter.

    Trying to get the fastest you can out of echo or print is a waste of time focus on other things.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by orlandu63 View Post
    PHP Code:
    echo 'Go here'"\n";
    //or
    echo "Go here\n"
    On a much larger scale of course.
    interesting question, to figure out which statement is faster, your need to install "Parsekit" extension, you can find it on php.net.

    following code is used to test which one faster.
    <?php
    echo 'Go here'. "\n";
    $ops = parsekit_compile_string('
    echo "Go here"."\n";
    ', $errors, PARSEKIT_QUIET);


    var_dump($ops);

    echo "Go here\n";

    $ops = parsekit_compile_string('
    echo "Go here \n";
    ', $errors, PARSEKIT_QUIET);

    var_dump($ops);
    ?>

    from result, seems echo "Go here\n"; is faster.

    regards

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    A better test.

    Code php:
    <?php
     
    header('Content-type: text/plain');
     
     
    $t1Start = time() + microtime();
     
        for ($i = 0; $i < 1000; $i++) {
            echo 'test', "\n";
        }
     
    $t1Stop = time() + microtime();
     
    //------------------------------------------------------------------------------
     
    $t2Start = time() + microtime();
     
        for ($i = 0; $i < 1000; $i++) {
            echo 'test' . "\n";
        }
     
    $t2Stop = time() + microtime();
     
    //------------------------------------------------------------------------------
     
    $t3Start = time() + microtime();
     
        for ($i = 0; $i < 1000; $i++) {
            echo "test\n";
        }
     
    $t3Stop = time() + microtime();
     
    print "\n\n---------------------------------------------------------------------\n\n";
     
    print 'Test 1: ' . round($t1Stop - $t1Start, 10) . "\n";
    print 'Test 2: ' . round($t2Stop - $t2Start, 10) . "\n";
    print 'Test 3: ' . round($t3Stop - $t3Start, 10) . "\n";

    In all seriousness this is pointless its hardly even a nano second between each other. Again focus on other things.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do whichever seems more readable for you.

  6. #6
    SitePoint Wizard HarryR's Avatar
    Join Date
    Dec 2004
    Location
    London, UK
    Posts
    1,376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Personally I'd go with the more readable syntax, e.g.:

    PHP Code:
    echo <<<EOS
    Some string {$variable} And {$this->variable} other stuff\n
    EOS; 
    Just think it's more readable with the bracing and you don't have to bother escaping all the characters.

  7. #7
    SitePoint Wizard TheRedDevil's Avatar
    Join Date
    Sep 2004
    Location
    Norway
    Posts
    1,196
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by HarryR View Post
    Just think it's more readable with the bracing and you don't have to bother escaping all the characters.
    What I dont like about the heredoc syntax is that you can not add ternary conditionals to it.

    Though it can be very useful at times, I mostly stick with regular echo statements.

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2007
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $test 
    "1111111111";
    $test2 "2222222222";
    $test3 "3333333333";

    $t1Start time() + microtime();
     
        for (
    $i 0$i 1000$i++) {
            
    ?>
            <table border=1 cellspacing=0 cellpadding=2>
            <tr>
               <td><?=$test?> - <?=$test2?> = <?=$test3?></td> 
            </tr>
            </table>
    <?php
        
    }
     
    $t1Stop time() + microtime();
     
    //------------------------------------------------------------------------------
     
    $t2Start time() + microtime();
     
        for (
    $i 0$i 1000$i++) {
    echo <<<BODY
            <table border=1 cellspacing=0 cellpadding=2>
            <tr>
               <td>
    {$test} - {$test2} = {$test3}</td> 
            </tr>
            </table>
    BODY;
    //echo $body;
        
    }
     
    $t2Stop time() + microtime();

    print 
    'Test 1: ' round($t1Stop $t1Start10) . "<br>";
    print 
    'Test 2: ' round($t2Stop $t2Start10) . "\n";

    ?>

  9. #9
    SitePoint Zealot darksystem's Avatar
    Join Date
    Jan 2005
    Location
    Fayettville, North Carolina
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree. this is pointless
    Ebay API, OSC/CRE/OscMax/ZenCart/SEO Services
    Lucki Multimedia - Email


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •