Speed test: PHP vs Perl vs Python vs Go vs C

August 30th, 2010

Recently I have needed to run a cron job every minute on a high load web server. As am currently most fluent in PHP I naturally wrote my script in PHP. However, as it's running every minute, I do want it to be as fast as possible. Seeing as the script was very simple, I thought that the most time required to run is in loading the PHP interpreter. Maybe i could use a compiled language instead? What about Google's new language Go?

Anyway, for my test I wrote a "hello, world" command line app in each language and ran the program 1000 times. Without any further ado, here are the results, from fastest to slowest:

C: 5.37 seconds

Go: 6.23 seconds

Perl: 7.97 seconds

Python: 20.64 seconds

PHP: 41.31 seconds

There you go! PHP is a bit slow for this kind of thing! But I'm not going to write all my scripts in Go or C now (what a hassle!). So, for me…. perl wins!

Ahh, good old perl, my faithful friend! I will never forget you!

Ubuntu for mother in law = yay!

November 10th, 2009

As I wrote in previous post, my personal experience trying to use ubuntu as a php development box was very brief!

However, i was impressed by the GUI side of things in Xubuntu (ubuntu but with the lightweight xfce desktop), and when my mother in law's (really!) laptop blew up – I took the opprtunity to spread the ubuntu/linux buzz up a generation! :)

My own windows desktop workstation (yes I use windows for my PHP programming – of course the PHP /apache side runs on linux!!) got a weird hardware problem, something to do with USB, and Windows kept rebooting itself, but linux boot cds worked fine… So I thought, oh well, if I cant really use this myself, why not chuck linux on it and give it away!

Mother-in-law literally just writes email and browses the web (maybe writing a few google docs) so i think linux gui is perfect for her….

So… after a fairly easy install…. viola! My crappy old desktop is reborn as a super simple NET PC!!! She loves it!

Hooray for linux, hooray for Xubuntu – rescuing obsolete hardware worldwide! :)

A brief flirtation with Ubuntu as development box

August 29th, 2009

Recently I've been using this awesome persistent version of Xubuntu on my USB pen drive. It's great for fixing computers, virus scanning, etc.

http://www.pendrivelinux.com/usb-xubuntu-904-persistent-install-windows/

It even detected my wireless adapter with no hassles – and the awesome thing is that all your settings are saved to disk!!

I was so happy that I thought I'd try xubuntu as a PHP/MySQL development box…

Installed great, looks good, BUT… OH NO .. mysql can't start anymore… some weird access problem!! argh!!!

I've never had anything like this with Debian before… WTF is going on?!

Anyway, back to Debian for development. Ahhhh…. good old trusty Debian. YAY!!!!

UTF-8 checklist

April 27th, 2009

Character encodings have been driving me insane lately….

- stuff pasted from word wont display properly

- funny square characters in the browser

- smartĀ  quotes from word don't display properly

So here' s a handy list of things i have been trying. Some things mgiht work for you, some might not. Let me know if you have anything to add!

Apache

AddDefaultCharset UTF-8

MySQL

On connect – SET NAMES 'utf8'

Convert: ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
or:

<?
$dbname = $argv[1];
if(!$dbname) {
print "no database specified\n";
exit;
}
$dbh = mysql_connect('localhost', 'username', 'password', 1);
mysql_select_db($dbname);
$sql = "SHOW TABLES";
$res = mysql_query($sql, $dbh);
while($row = mysql_fetch_assoc($res)) {
$sql2 = "ALTER TABLE $row['Tables_in_{$dbname}'] CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci";
print $sql2 . "\n";
}
?>

http://www.oreillynet.com/onlamp/blog/2006/01/turning_mysql_data_in_latin1_t.html

http://gentoo-wiki.com/TIP_Convert_latin1_to_UTF-8_in_MySQL

Also, once all of your databases are utf8 you could set these options in my.cnf:

[mysqld]
default-character-set=utf8
default-collation=utf8_unicode_ci

[client]
default-character-set=utf8

PHP/HTML

<meta http-equiv="content-type" content="text/html; charset=utf8" />
use htmlentities like this:
htmlentities($string, ENT_COMPAT, 'UTF-8')

Magpie RSS:
$rss = new MagpieRSS($rss_string, 'UTF-8', 'UTF-8', false);

How to setup hobbit, including MySQL graphs

August 1st, 2008

Detailed docs soon… at the moment just a handy link for myself with a few files…mysqlh.zip

Setting up xymon client
adduser –disabled-login xymon
cd /usr/src
wget http://transact.dl.sourceforge.net/sourceforge/hobbitmon/xymon-4.3.0-beta2.tar.gz
tar -zxvf xymon-4.3.0-beta2.tar.gz
cd xymon-4.3.0-beta2
./configure –client
(config=server)
(user=xymon)
/home/xymon
make
make install
su – xymon -c "/home/xymon/client/runclient.sh start"

Install MySQL/Apache Montioring on client

1. Create a mysql user 'monitoring'

2. Install ext scripts::

cd /home/xymon/client/ext
wget http://www.phpprogrammer.co.nz/wp-content/uploads/2009/05/ext.zip

unzip ext.zip
rm ext.zip

Add lines to /home/xymon/client/etc/clientlaunch.cfg

[mysql]
ENVFILE $HOBBITCLIENTHOME/etc/hobbitclient.cfg
CMD $HOBBITCLIENTHOME/ext/bb-mysql.pl
LOGFILE $HOBBITCLIENTHOME/logs/bb-mysql.log
INTERVAL 3m

[mysqlhq]
ENVFILE $HOBBITCLIENTHOME/etc/hobbitclient.cfg
CMD $HOBBITCLIENTHOME/ext/mysqlhq
LOGFILE $HOBBITCLIENTHOME/logs/mysqlhq.log
INTERVAL 3m

[mysqlht]
ENVFILE $HOBBITCLIENTHOME/etc/hobbitclient.cfg
CMD $HOBBITCLIENTHOME/ext/mysqlht
LOGFILE $HOBBITCLIENTHOME/logs/mysqlht.log
INTERVAL 3m

[mysqlhs]
ENVFILE $HOBBITCLIENTHOME/etc/hobbitclient.cfg
CMD $HOBBITCLIENTHOME/ext/mysqlhs
LOGFILE $HOBBITCLIENTHOME/logs/mysqlhs.log
INTERVAL 3m

[apachew]
ENVFILE $HOBBITCLIENTHOME/etc/hobbitclient.cfg
CMD $HOBBITCLIENTHOME/ext/apache-workers.sh
LOGFILE $HOBBITCLIENTHOME/logs/apache-workers.log
INTERVAL 3m

restart xymon:

su -c "/home/xymon/client/runclient.sh start" xymon

Easy Setup of a CVS server on Debian Etch

July 26th, 2008

I tore my hair out over this for quite a while yesterday. Hopefully this might help someone else (and as a reminder to my self if I ever need to do this again). Ideally I wanted the following scenario:

- developer account 'hamish' who can read/write to the cvs server
- an anonymous account 'anon' who can only read from the cvs server

I wanted the developer accounts to connect with ssh and anon via pserver, however…this seems like a mission! Perhaps this is why sourceforge (who do it this way) have a horrible delay between the developer and anonymous servers – maybe the 2 schemes wont cooperate nice and easily!?

Anyway, so I decided to use plain old pserver for both developer and anonymous account. This made my life a lot easier, and I'm not that paranoid about security :)

Anyway, here goes…
1. Install cvs and cvsd

apt-get install cvs cvsd

When configuring cvsd I was asked which repositories to serve. I typed '/cvs'.

This means my repository will be available at cvs.myserver.com/cvs

2. Initialise the repository

cvs -d /var/lib/cvsd/cvs init

add the following line in /var/lib/cvsd/cvs/CVSROOT/config:
LockDir=/tmp

3. Allow write access to repository

chown cvsd:cvsd /var/lib/cvsd/cvs

4. Allow history file to be written

chown cvsd:cvsd /var/lib/cvsd/cvs/CVSROOT/history

5. Add developer use account

cvsd-passwd /var/lib/cvsd/cvs hamish

6. Allow developer to write to repository

echo "hamish" > /var/lib/cvsd/cvs/CVSROOT/writers

7. Setup anonymous account

cvsd-passwd /var/lib/cvsd/cvs anon

That's it! Good luck!

Firefox 3 excitement….disappointment

November 22nd, 2007

I just read on slashdot – "ZDNet picks up on yesterday's Firefox 3 beta 1 review by comparing the memory usage of Firefox 2 against the latest beta. The results from one of the tests is quite interesting, after loading 12 pages and waiting 5 minutes, 2 used 103,180KB and 3 used 62,312KB. IE used 89,756KB.""

Oh how excited I was – the memory issues with FF 2 were why i went back to v1. Could I finally move on!?

The answer……….NO!!

The simple task of removing a link from my bookmarks toolbar sent FF3 into a spin…memory usage 720,000k and counting!! LOL!

What a joke.

Downgrade to Firefox 1.5 – Firefox 2 is a lemon!

November 9th, 2007

Ah, what a brilliant day. For months now I've been swearing about how bad Firefox 2 is – memory leaks, slow loading, crashing – it's been the bane of my coding life. Anyway, finally today I took the time to scour the net for good old Firefox 1.5 – you can't get it from the official firefox site. Anyway here it is:

http://filehippo.com/download_firefox/?2743

I can't believe how much faster it is! My productivity has gone through the roof!

Debian and New Zealand Daylight Savings SOLUTION

October 5th, 2007

Ok, I read the headlines – "Debian refuses to push out new NZ daylight savings info" or something like that. Everyone getting angry and annoyed. Sure enough, my Debian boxes (running etch) were an hour behind. The only info I could find was manually compiling new timezone info (which didn't really work).

ANYWAY – finally I found the quick and easy solution that I wish the alarmists had just said clearly in the first place:

Put this line in your /etc/apt/sources.list file:

deb http://volatile.debian.org/debian-volatile etch/volatile main contrib non-free

or for sarge:

deb http://volatile.debian.org/debian-volatile sarge/volatile main contrib non-free

then run the usual apt-get update ; apt-get upgrade and you're done!

Yay! Hopefully that might save someone a little time, hohoho no pun intended.

More about Debian volatile here:

http://www.debian.org/volatile/

Web contact forms, spam, and autoresponders

July 13th, 2007

I just realised I almost made a silly mistake :)

To cut down on the amount of spam you get, it's fairly common knowledge that putting your email address (even not machine readable) on your website is an open invitation for spammers to abuse your account. A common solution is to set up a web-based form which visitors fill out, and then gets forwarded to you (with a little bit of spam filtering along the way).

Recently I've been getting a lot of contacts that just say things like "hey man, i like your site!". This is clever way of trying to get your email address by 2 ways:

1. You reply to the email and say "thanks mate, i like my website too" (bad move, you just sent your email address direct to the spammer.)

2. You have a VACATION responder turned on which automatically sends the spammer your email address!

So, there are a couple of solutions:

1. dont set the From: header in the email from the website to be from the email address the user submits in the form (your autoreply will go straight to that address (the spammer's)).

2. Dont ever go on holiday, so you dont need an autoresponder! :)