I thought I'd do some quick testing on php to see which method(s) of coding work best..
Platform
- PHP Version 5.2.4
Linux (Ubuntu 6.06 LTS)
Dual Core AMD Opteron(tm) Processor 175 (2194.87 MHz) w/ 2GB Ram
- CLI & Apache 2.0.55 (PHP Via Apache 2.0 Handler)
Code: Select all
sha1(microtime(true).rand(1,2300))Test 1 - inline:
Code: Select all
for($b=0;$b<1000000;$b++) {
sha1(microtime(true).rand(1,2300));
}4.69 : 4.65 : 4.65 : 4.66 : 4.65
Via CLI:
4.73 : 4.70 : 4.72 : 4.74 : 4.72
Test 2 - function:
Code: Select all
function t() {
sha1(microtime(true).rand(1,2300));
}
for($b=0;$b<1000000;$b++) {
t();
}5.16 : 5.12 : 5.12 : 5.12 : 5.12
Via CLI:
5.20 : 5.30 : 5.22 : 5.24 : 5.23
Test 3 - OO Method 1:
Code: Select all
class test {
public function t() {
sha1(microtime(true).rand(1,2300));
}
}
$o = new test;
for($b=0;$b<1000000;$b++) {
$o->t();
}5.44 : 5.40 : 5.39 : 5.41 : 5.38
Via CLI:
5.47 : 5.39 : 5.34 : 5.38 : 5.36
Test 4 - OO Method 2:
Code: Select all
class test {
public function __construct() {
$this->c();
}
public function c() {
for($i=0;$i<1000000;$i++) {
sha1(microtime(true).rand(1,2300));
}
}
}
$o = new test;4.65 : 4.61 : 4.62 : 4.63 : 4.61
Via CLI:
4.67 : 4.70 : 4.73 : 4.76 : 4.75
=========================================================
Sub Conclusion
Suprisingly Apache was marginally faster in all tests carried out, the 2 fastest (and almost identical) methods were:
- Test 1 - Inline
Test 4 - OO Method 2
=========================================================
Will Multithreading via the CLI speed things up?
To get some results I repeated tests 1 & 4 via the cli, however this time multithreaded (5 worker threads + launcher thread) - the results are as follows:
Test 1 - inline
4.70 : 4.71 : 4.71 : 4.72 : 4.70
Test 2 - OO Method 2
4.73 : 4.71 : 4.73 : 4.74 : 4.72
Rather unsuprisingly multithreading was no faster.. why?
The test function used is very processor intensive, so no matter which method we use, the computer has to do the same amount of work, thus we can't speed it up without upgrading the hardware. The question remains then, when do we multithread our applications, The answer lies in the following tests.
Multithreaded vs Non Threaded
The Test:
50x file_get_contents() on a manual page at uk.php.net [ single thread vs multithreaded (5 worker threads + launcher thread) ]
Single Thread:
52.55 : 53.33 : 52.86
Multithreaded:
16.42 : 16.49 : 16.58
=========================================================
Conclusion
Personally I've gathered that as I'd assumed previously, most applications can be fully optimised and run as quick as can be by sticking to standard PHP5 OO practises; The only time this can be improved is when we need to work with third party datasources or applications that run slower than our machine can handle, under these circumstances it's safe to say that multithreading will vastly speed things up.
=========================================================
One Final Lesson
You can speed up your applications very simply by making the smallest of changes; use single quotes instead of double quotes around all your strings. Check this out:
The Test:
Double Quotes:
Code: Select all
for($i=0;$i<5000000;$i++) {
$testdata = "a normal $i string";
}Code: Select all
for($i=0;$i<5000000;$i++) {
$testdata = 'a normal '.$i.' string';
}Double Quotes:
5.43 : 5.57 : 5.53 : 5.57 : 5.43
Single Quotes:
4.49 : 4.51 : 4.52 : 4.51 : 4.49
Shaved 1 second (20%) off my very simply script simply by using single quotes!
(why? php parses everything in double quotes for variables, cut that parsing out and save a tonne of processing time)
Cheers,
nath