SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 43 of 43
  1. #26
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,039
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by ServerStorm View Post
    Have you looked to see how they achieve this?
    I haven't, but since it uses lexical parser, I guess it's just a matter of unsetting all symbols except for the one(s) supplied.

    Basically unsetting all other variables for the current scope. Where the scope has nothing to do with PHP's scope, but with Twig's lexer scope.
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  2. #27
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, as I mentioned before the only places I ever use includes are when including class files or templates.

    Whenever I do it via templates, this is the function I use. Basic but useful. It's within a 'content' class, static because it's instance-independent.

    PHP Code:
        public static function loadTemplate($Module$Name, array $Variables = array()){
            
    $File "Framework/Modules/{$Module}/Templates/{$Name}.tpl";
            if(
    file_exists($File)){
                
    extract($Variables);
                
    ob_start();
                include 
    $File;
                return 
    ob_get_clean();
            }else{
                return 
    "Template not found: {$Module}/{$Name}";
            }
        } 
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #28
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ServerStorm View Post
    Have you looked to see how they achieve this?
    Simple, the values ARE still passed to TWIG, it just doesn't use them in it's string processing. That's it -- no magic. It's basically a separate interpreter running atop PHP (an interpreter)... as such anything it doesn't do to the strings it's processing, isn't in 'scope'. It actually IS in scope inside TWIG, but because TWIG doesn't make use of them in it's engine, you can't see them from inside the engine.

    It's like the variables in the code written in C for PHP, JAVA, JavaScript, or any other interpreted language (and for all the "bytecode compiler" and "virtual machine" raving, a VM is at best a JIT compiler, at worst an interpreter! Those of us who used pascal to make P-Code three decades ago at least admit this) -- you can't directly access any of those inside the language because the interpreter lacks any mechanism for passing them. Since I'm writing my own interpreted language right now, I've been getting a refresher on that subject!

    Quote Originally Posted by ServerStorm View Post
    DeathShadow60, what are the things you don't like about it, other than it is not needed because PHP is a perfectly acceptable templating technology itself?
    Like we need a better reason? I mean, it's for all intents and purposes an interpreter written in an interpreted language -- SLOW be thy name! Though there are plenty of other reasons...

    For starters, to me it's uselessly cryptic for no good reason. Concise does NOT mean shorten everything into abbreviations and symbols; when it comes to being concise, it seems many folks out there concentrate too much on the 'short' half of the equation, while completely forgetting the other half -- being CLEAR. {{ var|escape }} is NOT clear...

    It also seems crafted for the folks who like to open and close php on every blasted line -- which BTW is more work for the interpreter; naturally we want to make more work for an interpreter that's running atop another interpreter. What can we call that? A herpaderpeter?

    In a lot of ways, it strikes me as the same type of nonsense as the folks who try to write entire "applications" using nothing more than BASH scripts -- which would make sense given the syntax seems similar. That's not a good thing...

  4. #29
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    deathshadow60, the argument that "language within an a language" is inane. PHP is an interpreted language written in C. Why use PHP instead of C? C is compiled into ASM. Why don't we all code in that? That'd fit nicely with your obsession with performance, too. If performance is that critical you shouldn't be using PHP.

    The point of abstraction--and that's all it is--is to make common tasks easier to code for the developer by reducing repeated code.

    Clarity... well that's personal preference and not a valid argument.

  5. #30
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jake Arkinstall View Post
    Whenever I do it via templates, this is the function I use. Basic but useful. It's within a 'content' class, static because it's instance-independent.
    Interesting... as a rule of thumb I dislike letting even template files run code directly, wrapping them in functions as well.

    I'm writing the Mk8 version of my little CMS right now -- it's template loading function pretty much just includes the file, since the template is all functions.

    Code:
    	public function loadTemplate($name) {
    		$fullName=safeName($name).'.template.php';
    		if (file_exists($temp=$this->paths['LOCAL_THEME'].$fullName)) {
    			safeRequire($temp);
    			return true;
    		} else if (file_exists($temp=$this->paths['LOCAL_ROOT'].'theme/default/'.$fullName)) {
    			safeRequire($temp);
    			return true;
    		} else if (file_exists($temp=$this->paths['LOCAL_ROOT'].'modules/'.$name.'/'.$fullName)) {
    			safeRequire($temp);
    			return true;
    		}
    		return false;
    	} // paladin::loadTemplate
    Basically just calling my scope-breaking require wrapper since everything there is wrapped in functions too. It checks through each possible location in turn since if the current theme has it, it should run from there, otherwise run from default, otherwise use the copy that came with the 'module'.

    Then instead of increasing the memory footprint, I just pass the data by reference.

    for example common.template.php

    Code:
    function theme_header(&$paladin,$admin=false) {
    global $texts;
    
    	foreach ($paladin->httpHeaders as $header) {
    		header($header);
    	}
    
    	echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html
    	xmlns="http://www.w3.org/1999/xhtml"
    	lang="en"
    	xml:lang="en"
    ><head>
    etc... etc... etc... Another bonus is it lets me keep multiple common commands you know will be used, like theme_header, theme_footer, theme_subSectionStart, theme_subSectionEnd, all in one file/include.

    Technically $paladin is a object so it's always passed by reference on modern PHP flavors, but I like to state it with & just to be sure. In this version "paladin" is the core object, "mount" is the database connection, and modules are extensions of class "squire"... was thinking on having module sub-functions be "page", but was worried people would confuse "page" as in printed with "page" as in pre-teen serf.

    I believe you may have seen something similar as a project you were on for a short while was loosely based on my Mk6 that someone else is running with the concept of.

    With yours, I'd suck it up and type the $variable[''] inside the template, rather than making duplicates of every variable in memory... of course that you're passing the output on the stack means you really weren't keeping memory use in mind... NOT that it's easy to keep in mind with in any complex CMS -- my own approach of "generate all the data before even making ANY output, even headers" troubles me in terms of the size of it's memory footprint. I'm half tempted to let the routines that pull/process the data call the theme directly -- but that goes against the entire "layered security" policy I'm working from.... having that same type of setup WITH the overhead of markup added to it? Ouch.

    Though it would STILL be leaner than any of the "templating engine" nonsense by a factor of four.

  6. #31
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ahhh me and that framework had a lot to disagree on :P

    I suppose there are just different ways of doing things; For example - I trust the developers. If they want to include a filename which is not 'safe', well I'd let them; If they wanted to do some damage they'd just write their own code anyway. If the developer was just being stupid - well, again, let them. My job is to make the framework work. I certainly do validation - I will check if a file exists, but not if it's formatted correctly. Mainly, I have 0% trust in users, but 99% trust in the developers.

    The variables aren't duplicated in memory, because everything passed to that template (so far) has been an object - oh and a boolean or two. As for using an array... I dislike using an array unless everything within it is the same variable type. The only time I don't is when passing an array to the above method, which I do inline.

    Micro-optimisation (worrying about a variable or two being cloned) etc is certainly something I care little for. I don't like wasting memory etc, but servers nowadays really don't get affected unless you're pulling in millions of views a day. By which point I've have sold up and started working on something new.

    In the next few days I'll possibly post a new framework I've been working on; but I'll comment it all up first. I've been building it alongside a website for my university's physics society ( Big leagues! ) and it times up pretty darn well.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  7. #32
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,246
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by deathshadow60 View Post
    It also seems crafted for the folks who like to open and close php on every blasted line -- which BTW is more work for the interpreter; naturally we want to make more work for an interpreter that's running atop another interpreter. What can we call that? A herpaderpeter?
    I put your claim to the test. The difference between just one php tag vs multiple opened and closed tags is in the ballpark of one one-millionth of one millisecond. And neither approach was consistently faster than the other.

    You may want to retract your "herpdaderp".

    And the next time you get the urge to insinuate that the smartest in the business are stupid, I strongly suggest you first test your claim, because your gut instinct doesn't have the greatest track record.

  8. #33
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,023
    Mentioned
    62 Post(s)
    Tagged
    0 Thread(s)
    Getting back to the original topic. I would like to think I know a little bit more than most coders about formatting text because I've actually had art classes on the topic as part of my college major. For all the bru ha ha ha going on the important part is missed - be consistent. Know your formatting rules, know why you made them, then you'll know when to break them.

    As for testing Jason's claim..

    Code php:
     
    $echoey = str_repeat("\<\?php echo 'Hello World'; \?\>", 10000);
    $noEchoEy = str_repeat("echo 'Hello World';", 10000);
     
    for($i=0;$i<1000;$i++) {
    	ob_start();
     
    	$before = microtime();
    	eval($echoey);
    	$echoEyExeTime[] = microtime() - $before;
     
    	ob_end_clean(); 
    	ob_start();
     
    	$before = microtime();
    	eval($noEchoEy);
    	$noEchoEyExeTime[] = microtime() - $before;
     
    	ob_end_clean();
    }
     
    $echoEyExeTime = array_sum($echoEyExeTime) / count($echoEyExeTime);
    $noEchoEyExeTime = array_sum($noEchoEyExeTime) / count($noEchoEyExeTime);
     
    echo "Execution time with tag wraps: {$echoEyExeTime}<br> Execution time without tag wraps: {$noEchoEyExeTime}";

    I used a double looping to try to rule out the effect of other concurrent threads on the test. While the non Echoey method is faster, it's hardly a speed revolution.

    EDIT - Also noted this gem:

    Quote Originally Posted by deathshadow
    Then instead of increasing the memory footprint, I just pass the data by reference.
    All objects in PHP are passed by reference anyway. Values aren't copied in the PHP interpreter until the values become changed in different scopes. Are you sure you really understand how the Zend Engine works?

  9. #34
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,246
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Michael Morris View Post
    As for testing Jason's claim..
    I tried to run your test, but I got syntax errors. In fact the docs for eval say that the code mustn't be wrapped in opening and closing PHP tags. Also, probably best to pass "true" to microtime, otherwise it returns a string with a space in it, which doesn't work well for arithmetic.

  10. #35
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    To make eval work in that way you'll need to use:
    PHP Code:
    eval('?>' $echoey); 

  11. #36
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,023
    Mentioned
    62 Post(s)
    Tagged
    0 Thread(s)
    Well, someone make the corrections and run the test. I'm curious (also harried at work)

  12. #37
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,246
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    It turned out that most of the time was being spent on eval itself, so I change it up to instead require an actual template file.

    PHP Code:

    <?php

    // prepare template files
    file_put_contents('one_tag.php'"<?php\n\n" str_repeat("echo 'Hello World';\n"10000));
    file_put_contents('many_tags.php'str_repeat("<?php echo 'Hello World'; ?>\n"10000));

    $oneTagTimes = array();
    $manyTagsTimes = array();

    for (
    $i 0$i 1000$i++) {
        
    // test one tag
        
    ob_start();
        
    $before microtime(true);
        require 
    'one_tag.php';
        
    $oneTagTimes[] = microtime(true) - $before;
        
    ob_end_clean();

        
    // test many tags
        
    ob_start();
        
    $before microtime(true);
        require 
    'many_tags.php';
        
    $manyTagsTimes[] = microtime(true) - $before;
        
    ob_end_clean();
    }

    $oneTagAverage array_sum($oneTagTimes) / count($oneTagTimes);
    $manyTagsAverage array_sum($manyTagsTimes) / count($manyTagsTimes);

    echo 
    "oneTagAverage -> $oneTagAverage\n",
         
    "manyTagsAverage -> $manyTagsAverage\n",
         
    "\n",
         
    "diff -> ", ($oneTagAverage $manyTagsAverage), "\n";
    I get a difference of about 0.002 milliseconds. And even that number is inflamed by a factor of 10 or more. Template files are usually nowhere near 10,000 lines (or at least mine aren't). They're closer to perhaps a few hundred, and most of those lines are plain HTML... which is the whole reason we choose to close PHP in the first place.

  13. #38
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    <chris tucker>DO YOU UNDERSTAND THE WORDS THAT ARE COMING OUT OF MY MOUTH!?!</chris tucker>

    Quote Originally Posted by Michael Morris View Post
    All objects in PHP are passed by reference anyway. Values aren't copied in the PHP interpreter until the values become changed in different scopes. Are you sure you really understand how the Zend Engine works?
    Quoting out of context too -- given said statement was comparing to how Jake was using EXTRACT to take an array and/or object and put it in the local namespace? I AM using objects, it's WHY I'm using objects and/or arrays, and it's why I made that statement as he is not. Jake was using extract to make extra copies therein using more memory instead of passing by reference.

    Quote Originally Posted by Jeff Mott View Post
    I put your claim to the test.
    Care to share your 'test'? Mike was nice enough to share a test -- broken test since it doesn't account for rollover/ms inaccuracy and doesn't pass 'true' to microtime so none of the results make any sense (since the returned string is microseconds first, meaning his test can return negative values) -- but at least it's an attempt.

    With 1000 per loop, and such a large output size, the timer isn't likely firing and the numbers would be skewed due to malloc and garbage collection... Though I notice you did catch the incorrect use of microtime.

    Quote Originally Posted by Jeff Mott View Post
    And neither approach was consistently faster than the other.
    Funny, I get a five to nine percent speed difference in favor of without... consistently.

    Basing off Mike's code, we'll switch it around to check for timer rollover since we don't know the granularity, then set up to test for iterations over a fixed period of time rather than a fixed number of loops -- 1) so we don't have to wait forever, 2) again we don't know the timer granularity/accuracy. A one second delay before each test should also give the _clean method enough time to get garbage collection out of the way -- something that can wildly skew the numbers in favor of whoever goes first.

    We'll also pull the eval since that adds a lot of execution overhead, and we really don't need 10000K loops to figure this out... 24 echo's each should handle it quite nicely. We'll also plug in a variable in addition to the literal so that we can test the ACTUAL difference in a 'real world' type scenario, and then change that variable so as to eliminate value caching as a possible effect on the test.

    I also had to move the buffering around each call, as the malloc's were triggering enough to effect the results... and blow past the 32 meg memory limit I have set up

    Something good to add is a routine that does everything except output the values - gives us a baseline we can subtract from our execution times.

    Gah, code's too long. I'll upload it as .txt to my host (since it seems phps isn't set up... new host, still adjusting to not running my own).

    http://www.cutcodedown.com/for_other...agWrap.php.txt

    Which on this lappy (2.4ghz Core 2) under XAMPP gave the following results:

    http://www.cutcodedown.com/for_other...PhpVsNoPhp.png

    On my workstation the results are a bit lower - around 4.97% advantage as opposed to the 8.88% above - and the disparity does seem to drop the faster the CPU.

    But still, we're talking ~5%... though the real kicker is what happens if you put string additions in... compared to using period instead of comma's on echos, string additions are many times worse; Combine the two, and well...

    I would suspect you either had a flawed testing methodology, were testing on a machine fast enough to lower the difference, or had some other process or an inaccurate timer interfering with the results.

    Quote Originally Posted by TomB View Post
    deathshadow60, the argument that "language within an a language" is inane. PHP is an interpreted language written in C. Why use PHP instead of C? C is compiled into ASM. Why don't we all code in that? That'd fit nicely with your obsession with performance, too. If performance is that critical you shouldn't be using PHP.
    C is compiled to machine language, the abstaction exists to make it portable across CPU platforms/families, since machine language for x86 won't run on ARM... or PPC... or MIPS... or 68k... or Z80... or 6809... or RCA 1802...

    PHP is written in C and is interpreted so that it is easier to port across platforms without the endless low level hardware differences that you have to deal with in C getting in the way. Since most all of it's functions are written in C they get the same speed as C, which is why PHP is best used as glue between compiled libraries (like mySQL, like Regex) instead of making complex userland functions.

    TWIG takes something you can already do in PHP without the library and makes it cryptic, with an extra layer of abstraction for... for... for what exactly?!? For the sake of making it more complex and harder to use? To make the end result slower than Ruby? It does the opposite of what PHP is for, being glue, and instead treats it as a general purpose programming language... something it most certainly is not.

    At least C and PHP have legitimate reasons for their abstractions.

  14. #39
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,246
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by deathshadow60 View Post
    Care to share your 'test'?
    After the adjustments I made to Mike's test, his and mine are largely identical. The only significant difference is the code that was put in the templates.

    In the "one_tag" template, I repeated this block 10 times:

    PHP Code:
    if (isset($_SERVER['PHP_SELF'])) {
        echo 
    '
            <div class="box">
                 The value of PHP_SELF is '
    htmlspecialchars($_SERVER['PHP_SELF']), '
            </div>
        '
    ;

    In the "many_tags" template, I repeated this block 10 times:

    PHP Code:
    <?php if (isset($_SERVER['PHP_SELF'])): ?>
        <div class="box">
            The value of PHP_SELF is <?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>
        </div>
    <?php endif ?>
    The idea was to use templates closer to what real templates might be like.

    I rechecked it just now, and using these templates with my tester above, neither one is consistently faster.

    Quote Originally Posted by deathshadow60 View Post
    Funny, I get a five to nine percent speed difference in favor of without... consistently.
    It apparently varies depending on what's in the template. Nonetheless, we're still talking about a fraction of a fraction of a fraction of a fraction of a fraction of a fraction of a millisecond -- literally. To say that one is 9% faster is purely academic. For all practical purposes, they're identical.

    And the real issue here is that you insinuated that anyone who uses the open/close tag syntax is stupid, when in fact it's a non-issue. <snip/>

    It's great that you ran some detailed tests just now. I can only hope that you'll continue to run tests before you post. And if you do that, then you may be able to identify a way that the community can genuinely improve their code, and you'll have evidence to back it up, which can lead to productive discussions.

  15. #40
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,044
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    I just ran deathshadow60's file. the first are his results png, the second mine.
    PhpVsNoPhp.pngconcat.png
    Windows 7 Home Premium
    Intel(R) Core(TM) i3-2130 CPU @ 3.40GHz
    6.00 GB
    64-bit

    Whether or not the amount of difference is substantial enough to justify changing coding habits is debatable, but it sure looks like a difference exists just the same.

  16. #41
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Hey look, Mitteneague ran his in Firefox. Don't you just LOVE bugzilla 915?


    That said, your string add + no <? ?> result is a bit shocking... I've never seen those report faster than using comma delimits before -- at the same time it's also funny how the same thing with the opening/closing PHP is inline with the results I'd expect.

    Though I've noticed some strange variances across hardware... Hardly surprising -- JHVH knows what affect different CPU and disk caches could have on this.

    Just curious, I've mostly tested this on Intels, your numbers are more inline with the one AMD I have -- what were the system specs you tested that on?

    The difference is there -- common sense says the difference should be there -- it's really no more or less characters either way... so why use the slower method at all?

    @Jeff -- You complain I'm saying it's stupid to choose the other way once you know better -- what would you call intentionally choosing a method that runs slower with a higher memory footprint that's no more or less work to implement?

    Oh, and I have run multiple tests in the past on this -- pretty much every time this comes up, so don't pull the routine of making up claims about me and my conclusions! Yes, I've run tests in the past, YES, that's how I came to this conclusion. People complain I'm insulting just by my very nature -- well what in the blue blazes would you call your posts making insinuations and accusations?

    Maybe you're the one who should be running tests and possibly understanding how to test BEFORE running your mouth?

  17. #42
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,246
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by deathshadow60 View Post
    @Jeff -- You complain I'm saying it's stupid to choose the other way once you know better -- what would you call intentionally choosing a method that runs slower with a higher memory footprint that's no more or less work to implement?
    The people who choose the open/close syntax would say it is better in templates because it improves readability when you're outputting largely HTML. And since the speeds, for all practical purposes, are identical, there's no downside.

    Quote Originally Posted by deathshadow60 View Post
    Oh, and I have run multiple tests in the past on this -- pretty much every time this comes up, so don't pull the routine of making up claims about me and my conclusions!
    So this whole time, you knew you were calling people stupid over just one one-millionth of one millisecond?! That actually seems worse...

  18. #43
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,000
    Mentioned
    101 Post(s)
    Tagged
    0 Thread(s)
    As this thread has now gone way off topic:

    Thread Closed.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator


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
  •