Procedural PHP leads to slower apps

After reading OOP and Performance, Christopher Thompson rightly called my bluff (see the comments), triggering further discussion and forcing me to think harder.

In “OOP and Performance” I wasn’t trying to say anything absolute but rather describe a general hunch I’ve got. Although I singled out two personal examples, where the effect can, and was, written off as implementation detail, the hunch actually comes from general impressions and memories of trawling the source code of Open Source PHP apps, over the years, where they were written primarily with procedural code. And it’s not to say there aren’t any dog-slow OO apps out there – there most definately are.

Anyway, rather than waffle on further, I’m going to stick my neck out and say procedural PHP leads to slower apps (so I can get it chopped off ;)). The reason I’m feeling confident is that I think I’ve got a challenge which will help make things self-evident. Side note to any Perl / Python / whatever programmers – to an extent think this is specific to PHP, where a script itself does not define a separate variable scope and where classes are the only clean way to build abstractions.

So here goes. I’m going to define the input and output of a single script by providing the top and bottom. If you’ve got the time / interest,humour me and implement the section in the middle, once without using classes and a second time using classes / OO.

At the top of this script we have;


// This first dataset
$dataset1 = array();
for($i = 0; $i $dataset1[] = array('a','b','c');
}

// This second dataset
$dataset2 = array();
for($i = 0; $i $dataset2[] = array(
'x'=>array(1,2),
'y'=>array(3,4),
'z'=>array(5,6),
);
}

// This variable controls how the output is rendered
// Allowed values 'html','xml', 'plain'
$output_type = 'html';

(Your code goes in here)

…then at the bottom we have;


// output place in this variable
echo $result;

Of course there’s some rules and requirements…

The Requirements

– $dataset1 should be displayed to the user above $dataset2

– the script must be capable of rendering HTML, XML and plain text (whitespace formatted), depending the value of the output variable

– each dataset should be displayed with some kind of header (like a table caption) that tells the user what it is.

The Rules

– you may not include any external PHP scripts – it must be all in a single script

– you may not read or write to any external files (in particular you can’t use templates or code generation)

– placing function names in variables comes under the heading “hack”, as does using PHP’s create_function()

– nothing should be output until the final statement: echo $result;

– try to code as quickly as possible – don’t spend too much time thinking about the solutions.

– don’t do any benchmarking until you’ve finished

Despite the rules, I think this example is fairly representative of the type of code that makes up big parts of typical PHP apps.

In fact I’m actually expecting someone to come up with a procedural solution which benchmarks fastest overall, but not by much. At the same time I think this will illustrate why it’s more likely that procedural code ends up delivering relatively poor performance.

Replay

Category: programming Time: 2005-01-13 Views: 1
Tags:

Related post

  • PHP running too slow, always showing "504 Gateway Time-out" 2010-09-14

    PHP running too slow, always showing "504 Gateway Time-out" My server spec: Dual core ATOM 330 CPU 2GB RAM Use nginx with PHP in fastcgi use eaccelerator CPU 74.3%id RAM used: 350MB of 2GB I have lots of sites in my server, with cron running eve

  • Should I continue using procedural php code or move to a framework 2011-05-06

    I haven't done any serious web development stuff since last year because of family/time issues. I need to get back into it because it's my career choice and I'll be finished with my associates degree soon. I decided to revamp a previous website I hel

  • mvc pattern on procedural php 2013-01-24

    First off, I do not have anything against OO programming (I'd be mad if i believed so). But I was thinking on some sort of procedural MVC pattern with PHP; let me explain better. As we all know, variable scopes, unless they are taken from $_SESSION (

  • High load and iowait on MySQL database server leads to slow MySQL INSERTS 2013-01-31

    I have a MySQL database server runnuing on Ubuntu 12.10 with 64GB RAM, 2x3 TB SATA 6 Gb/s HDD 7200 rpm and software RAID 1 and an Intel Core i7-3930K. The database server is accessed over a local network with a dedicated 1 GBit ethernet card. The dat

  • Why is PHP - MSSQL very slow when the DB is not on the same machine as the PHP code 2013-04-05

    In the course of trying to move our website to Azure, we've found a problem with our PHP code talking to MSSQL. On the current dedicated host (hosting both the PHP and the DB), going to a page typically takes less than 2 seconds. I've also run the qu

  • How to enable exec function from php.ini at google app engine 2016-01-15

    How to enable exec function from php.ini at google app engine my php code echo 'test'; $item='example'; $tmp = exec("python test1.py $item"); echo $tmp; my python code import sys print sys.argv[1] exec function not working --------------Solution

  • Rewriting a Procedural PHP App - Should we use a framework? 2012-02-16

    We have an app that's currently only got about 120 users. The userbase will probably grow by 50% over the next 12 months - but we're going to have to separate storage etc. because we're looking at several TB of data. Later this year, we will move to

  • PHP Runs Very Slow on IIS7. Need Help optimizing our config 2012-04-06

    Am running a PHP based web app on our Windows 2008 cloud-based server. The app, which runs fine outside of our environment (e.g. a different IIS server), but is VERY slow in our environment. Based on googling this is a relatively common situation. I

  • PHP pages working slow from time to time 2012-06-26

    I have VPS with limit of 2GB of ram and 8 CPU cores. I have 5 sites on that VPS (one of them is just for testing, no visitors exept me). All 5 sites are image galleries, like wallpaper sites. Last week I noticed problem on one site (main domain, used

  • Calling a stored procedure from a third party app but it does not execute, but no errors are returned. What can I do to troubleshoot? 2016-01-19

    I have a SQL Server stored procedure being called remotely from a third party application. The stored procedure executes properly when called locally, but does not execute when called remotely. By locally I mean, if I right click and choose Execute;

  • Complex Mail Problem with Postfix, PHP and Gmail / Google Apps Enterprise 2011-06-02

    This question already has an answer here: Configure Postfix to use external MX servers for delivery of local mail if user is unknown 2 answers Okay, so I have read a lot around about the issues im having and the solutions people are offering only sol

  • Nginx and PHP FPM is slow 2011-07-26

    My server have 512 MB memory. nginx.conf setting is user www-data; worker_processes 2; pid /var/run/nginx.pid; events { worker_connections 768; # multi_accept on; } http { ## # Basic Settings ## sendfile on; #tcp_nopush on; #tcp_nodelay on; keepalive

  • Connecting to MySQL from PHP is extremely slow 2012-07-17

    I just did a fresh install of XAMPP. When first opening PHPMyAdmin I noticed it was extremely slow. It didn't make sense that on localhost it should take almost 5 seconds for every page to open. I made a small test case to shift the blame off PHPMyAd

  • My device is getting slow, apps start misbehaving/crashing. What can I do? 2012-12-14

    Everything was working fine for a long time. But suddenly the device is getting slower than usual (e.g. apps react slowly when starting an action -- like the RSS reader takes "ages" to open an article). Also, several apps start crashing: force-c

  • What do App Store reviewers say about greyed out buttons leading to in app purchases? 2013-02-22

    I'm wondering if there's anyone that have any experience from submitting an in app purchases (IAP) based iOS app to Apple with greyed out buttons that leads to the actual purchase? I've been searching but haven't found much, but it seems Apple is a l

  • SEO effects of intermix of WP blog, custom PHP site and FB app game 2013-05-08

    We're a melbourne tech company in the process of building a custom site in PHP. We plan to launch a "pre-launch" page which is also custom coded (CSS3 on twitter bootstrap framework + HTML5 front end and PHP back end). On that site will be a lin

  • Amazon AWS Service for multiple PHP, NoSQL (DynamoDB) Web Apps 2013-08-02

    I'm beginning the tranistion to Amazon Web Services (AWS) for my company, and I'm looking for some advice, tutorials, and/or anything else to help with the new setup. We host many (about 600) Real Estate websites. We have a bunch of agents who have s

  • How to have PHP and mod_wsgi python app on the same domain? 2013-11-07

    I am using apache with mod_wsgi (python3) on ubuntu 12.04. I have a python app (bottle) which is at www.mysite.com/ In my python app I have routes like www.mysite.com/abbb?q=blab I would like a path www.mysite.com/forum to resolve to a php app (simpl

  • nginx & php-fpm very slow on first access 2014-02-18

    I have a nginx 1.4.5 webserver with php-fpm 5.5.7 running on my server, but if my server stays idle for a while, accessing it is very slow. It takes about 10-20 seconds to get the response (first byte), after that its fast again. I dont know why it t

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.575 (s). 13 q(s)