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:

Virtual Worlds News » Former Outback Online Head Announces Imperial Galaxy for Facebook

December 20th, 2007 · Games

Former Outback Online Head Announces Imperial Galaxy for Facebook

→ No CommentsTags:

Australians launch MMOG on Facebook : The Metaverse Journal – Australia’s Virtual World News Service

December 18th, 2007 · Games

Australians launch MMOG on Facebook : The Metaverse Journal – Australia’s Virtual World News Service

→ 1 CommentTags:

Talking Squid » Imperial Galaxy, the first MMORG for Facebook

December 18th, 2007 · Games

On Talking Squid:

Imperial Galaxy, the first MMORG for Facebook

→ No CommentsTags: