Phil Wallach

I follow where my mind leads …

Phil Wallach header image 1

PHP profiling: comparisons

June 17th, 2009 · Programming

An application I created used a lot of MySQL ENUMS.  They are kind of useful, in that the value of an ENUM column does not need a lookup.  The downside is that it lead to a lot of string comparisons in the code, and this became a prime suspect when performance degraded.

However a colleague of mine suggested that it may be due to the cost of pass-by-value as opposed to pass-by reference.

So I wanted to sort it out and this simple test is the result. After all, the first rule of profiling is “measure, measure, measure” (OK, the first three rules). The following code compares string comparisons as pass-by-value, pass-by-reference and inline, as well as an integer (constant) comparison.

The profiling code:


        function compare_value($str)
        {
            if ($str == 'MekTek')
                ;
        }

        function compare_ref(&$str)
        {
            if ($str == 'MekTek')
                ;
        }

        $string = "MekTek";
        $integer = 0;

        $now = microtime(true);
        for ($i = 0; $i < 10000000; $i++)
            compare_value($string);
        echo("value: " . (microtime(true) - $now) . "\n");

        $now = microtime(true);
        for ($i = 0; $i < 10000000; $i++)
            compare_ref($string);
        echo("ref: " . (microtime(true) - $now) . "\n");

        $now = microtime(true);
        for ($i = 0; $i < 10000000; $i++)
            if ($str == 'MekTek')
                ;
        echo("str: " . (microtime(true) - $now) . "\n");

        $now = microtime(true);
        for ($i = 0; $i < 10000000; $i++)
            if ($integer == 0)
                ;
        echo("int: " . (microtime(true) - $now) . "\n");

The result:

    value: 5.2698040008545
      ref: 5.4901170730591
      str: 5.3154430389404
      int: 1.0382018089294

Isn’t that fascinating. I love it when profiling returns obvious results. The time taken to compare a string depends on the length of the string, regardless of other factors.

The message is clear. Do not compare strings in performance-critical code. Use constants instead.

→ No CommentsTags:

IMPERIAL GALAXY

December 10th, 2007 · Games

We’ll be launching the open beta test of IMPERIAL GALAXY, the science fiction massively multiplayer online game on Facebook, on Monday 17 December at 12:01am US Eastern time, which is 4:01pm AEDT. The www.imperialgalaxy.com website will also be live at that time.

I’ll be sending invitations to all my friends as soon as the Beta goes live, and it will be in the Applications directory and so on.

We’re very excited about IMPERIAL GALAXY. The background of the game is based on Garth Nix’s forthcoming novel A CONFUSION OF PRINCES (due out in 2009) and we’ve been playtesting it for the last three months. The game itself has been under development in its pre-Facebook incarnation for more than three years.

So a week from now, you can join the Imperial Navy and with your friends go forth to reclaim the lost Rozaxra Domain from Sad-Eye and Deader aliens, Mrouzh Rebels, Naknuk Secessionists, pirates, rebels and other star-scum. To your ships, my friends!

→ No CommentsTags:

OpenLaszlo: how you get there is more important than where you are going

August 29th, 2007 · OpenLaszlo

WHAT BROKE

This is the old version of the method.  It finds some specific views and scans their datapaths, to see if any entries match.  This version mostly worked fine, but occassionally it would fail, and finding the cause of the failure was proving difficult.


    &lt;method name="check_ok" args="from, to"&gt;
        &lt;![CDATA[
        for (var i = 1; true; i++)
        {
            var match = to.classroot.datapath.xpathQuery(
                'matches/match[' + i + ']/text()');
            if (match == null) break;
            for (var j = 1; true; j++)
            {
                var type = from.datapath.xpathQuery(
                    'types/type[' + j + ']/text()');
                if (type == null) break;
                if (match == type)
                    return true;
            }
        }
        return false;
        ]]&gt;
    &lt;/method&gt;

WHAT WORKED

I finally tracked down the problem; it occured whenever “to” was not replicated (i.e. there was only one).  So I assume that the replication manager was confusing my view navigation, as the number and type of views to navigate changed.

The solution is shown in the code below.  Basically I navigate the dataset directly, and avoid any view navigation.  Nothing can go wrong, right?


    &lt;method name="check_ok" args="from, to"&gt;
        &lt;![CDATA[
        var e  = to.datapath.dupePointer();
        e.selectParent(2);
        e.setXPath('matches');
        if (e.selectChild())
        {
            do
            {
                if (e.getNodeName() == 'match')
                {
                    var c  = from.datapath.dupePointer();
                    c.setXpath('types');
                    if (c.selectChild())
                    {
                        do
                        {
                            if (c.getNodeName() == 'type')
                            if (e.getNodeText() == c.getNodeText())
                                return true;
                        }
                            while (c.selectNext());
                    }
                }
            }
                while (e.selectNext());
        }
        return false;
        ]]&gt;
    &lt;/method&gt;

→ No CommentsTags:

OpenLaszlo: saving LzLoader

August 22nd, 2007 · OpenLaszlo

One thing that tripped me up early on is a know bug in the OpenLaszlo loader.  Basically, if a view is destroyed before the resource completes loading, then the loader chokes and nothing more is heard from it. 

Unsurprisingly, this is very very bad.

The bug is documented here:
    bug: destroy before resource is loaded leads to full load queue and hang

The fix is noted in the bug report.  Basically the following code is added to the application.

    &lt;script&gt;
        LzMakeLoad.unload = function ()
        {
            this.loader.unload( this.loader.mc );
        }
        LzMakeLoad.destroy = function (recur)
        {
            this.loader.unload( this.loader.mc );
            super.destroy( recur );
        }
    &lt;/script&gt;

→ No CommentsTags: