This time around, I’m being proactive about programming caching into my code. From what I’ve read APC has the best performance in terms of object level caching, but Memcache is built with a distributed architecture allowing it to scale with relative ease. In any case, I’m using Memcache for object level caching, APC for opcode level caching and XDebug for performance profiling. I use http_load to test multiple fetches in parallel and to see the throughput of the server. I ran my code using 5 parallel fetches for a total of 1000 fetches on my development server. The improvement I found through APC (for opcode caching only) was drastic:
Without APC (comment out extension=apc.so):
root@fork:/home/dev/http_load# ./http_load -parallel 5 -fetches 1000 url_file
1000 fetches, 5 max parallel, 451000 bytes, in 58.1554 seconds
451 mean bytes/connection
17.1953 fetches/sec, 7755.09 bytes/sec
msecs/connect: 0.154203 mean, 47.333 max, 0.031 min
msecs/first-response: 290.431 mean, 13220.5 max, 57.739 min
HTTP response codes:
code 200 -- 1000
With APC (add extension=apc.so):
root@fork:/home/dev/http_load# ./http_load -parallel 5 -fetches 1000 url_file
1000 fetches, 5 max parallel, 451000 bytes, in 10.8297 seconds
451 mean bytes/connection
92.3386 fetches/sec, 41644.7 bytes/sec
msecs/connect: 0.107207 mean, 0.205 max, 0.031 min
msecs/first-response: 54.0108 mean, 6252.95 max, 10.541 min
HTTP response codes:
code 200 -- 1000
Fetches per second went from 17.2 to 92.3 and milliseconds per first response went from 290.4 to 54.0. Granted these results are somewhat anecdotal because their based on my application and my current dev-server setup. Any way you look at it though the improvement is very impressive.
If you don’t know the difference between using double quotes or single quotes when using Strings in PHP, you should. The PHP Manual page for Strings covers all this, but basically strings created with double quotes will be parsed by PHP. This example shows what I mean:
// they both output 'Yahoo Google'
$str = 'Google';
echo 'Yahoo' . $str;
echo "Yahoo $str";
At first glance you might think, who cares? The key note is deep in the manual page:
Parsing variables within strings uses more memory than string concatenation. When writing a PHP script in which memory usage is a concern, consider using the concatenation operator (.) rather than variable parsing.
Earlier today I was looking to sort an array of object by a given datetime member. After a couple quick tests I realized that PHP was capable of comparing MySQL DATETIME strings natively. I guess that makes sense, since all they really have to do is convert the string to a timestamp via strtotime. In any case, after a bit of searching I found a method similar to the one below in a comment posted on the PHP Sort manual page. I changed the method a bit to compare using a public class method, sort in descending order and return a new array. I hard coded the function name cause from what I know you can’t pass in the function name as a parameter (you can if it was a key).
function sortObject($data) {
for ($i = count($data) - 1; $i >= 0; $i--) {
$swapped = false;
for ($j = 0; $j < $i; $j++) {
if ( $data[$j]->getTime() < $data[$j + 1]->getTime() ) {
$tmp = $data[$j];
$data[$j] = $data[$j + 1];
$data[$j + 1] = $tmp;
$swapped = true;
}
}
if (!$swapped) {
return $data;
}
}
}
Here is an example of the above function sorting four datetime strings:
class DObject {
private $dt;
public function __construct($d) {
$this->dt = $d;
}
public function getTime() {
return $this->dt;
}
}
$date1 = '2009-08-05 03:24:57';
$date2 = '2006-08-05 23:44:56';
$date3 = '2007-08-05 20:14:56';
$date4 = '2005-02-10 12:24:06';
$toSort = array();
array_push($toSort, new DObject($date1));
array_push($toSort, new DObject($date2));
array_push($toSort, new DObject($date3));
array_push($toSort, new DObject($date4));
echo "<pre>";
print_r($toSort);
$result = sortObject($toSort);
print_r($result);
echo "</pre>";
This is sad. They say 20% of the sub-prime mortgages are in default, this is the story of one of those people.
Until this month, few Canadians knew what “subprime” meant. Fewer still could imagine why they should even care. Common in the United States, these loans are a rarity in Canada, accounting for roughly 5 per cent of all mortgages.
But these risky loans – made at inflated rates to borrowers with inadequate income and spotty credit histories – were all the rage south of the border. Last year, they accounted for 20 per cent of new mortgages, spawning a $1-trillion (U.S.) market.
The last hour has been pretty interesting. I’m having some people over tonight and I thought to go buy a bottle of Vodka for our enjoyment. I settled on a bottle of Belvedere, which I purchased from the local LCBO. Belvedere means ‘Beautiful to See’ and named after the Royal Palace in Warsaw. I had heard great things about this Vodka and decided to jump the gun and pour myself a glass. My first impressions were good, the Vodka is smooth and lives up to the hype. I had heard that Snapple (particularly Banana) was a great mix with Belvedere. Unfortunately Banana Snapple isn’t sold in Canada, so I decided to experiment with Lemon and Peach. I don’t think this drink (Lemon Snapple + Belvedere) has a name, but it should. While I was enjoying my new found drink, I thought to lookup some info about Snapple. Like most big brands, Snapple has been plagued with a bunch of rumours surrounding the company, it’s founders and it’s products. One that I found particularly interesting was the claim that Osama bin Laden owns Snapple. Obviously this is a bogus claim, but I can definitely see how and why the rumour started. From here I thought to myself, who owns this amazing juice that goes so well with Belvedere? Cadbury Schweppes. The company trades in New York through an ADR and has been beaten down pretty hard the last couple months. They’ve been trying to sell-off the Snapple brand, but it doesn’t look like it’s going to happen. The potential buyers appear to have trouble securing financing needed, because of the sub-prime meltdown and credit crunch. So there you go, an hour of randomness summed up with a glass of Lemon Snapple + Belvedere.
Yahoo! Sports has a new writer named Michael Silver who recently made his debut with a fantastic preview of ‘Morning Rush’. In the article he covered (among many things) a hot topic in the sports world: Michael Vick and his alleged involvement (and funding) of breeding Pit Bulls to fight to the death. The NFL and the Atlanta Falcons quickly suspended Vick from training camp when the news first surfaced and rumours have it that Vick will soon be suspended completely from the 07/08 season. I’m not going to get into the details of why Vick’s suspension and treatment by the public/activist groups is hypocritical, instead I suggest you simply read the article.
It’s so sad that the most sacred record in American sports history was broken by a steroid pushing junkie. Say what you want, but his shoe size went from size 10 to 13. Your shoe size (and head size) doesn’t grow after the age of 30. Furthermore, those aren’t steroids: That’s growth hormones. MLB could have solved this problem a years ago (after the lockout) but for some reason they didn’t.
Now I know: It’s hard to hit a baseball out of the park, steroids don’t help you with your reaction time, hand/eye coordination etc.. I know that. But I’ve seen Barry Bonds hit home runs off of broken bats. YES, I said a broken bat home run and yes, you need serious strength to make that happen. I wonder if Hank Aaron could hit a broken bat home run?
There’s a good discussion going on at the Yahoo! Sports MLB Expert blog.
Jimmy Kimmel’s monologue the next day was great:
I’ve never been a big fan of GPL. The license is too ambiguous and in certain cases may be open to interpretation. I was reading up on Sphinx and what it can do when it struck me: This license says nothing about using software as a service. A quick Google search confirmed my thought via a blog post by O’Reilly:
..that free software license requirements to release source code are all triggered by the act of distribution, and that web applications, which are not actually “distributed,” are therefore not bound by these licenses..
I came across a good writeup on linuxjournal.com talking about the OO features/capabilities in PHP 5. I guess the most important thing to note is that objects (i.e. objects of classes, not arrays) are now passed by reference. The writeup, which was taken from the book Object-Oriented PHP by Peter Lavin, explains their rational for doing this and how the entire scripting engine underlining PHP had to be re-written. All other types of variables are still passed by value (including arrays), but you can easily pass them by reference by adding an & to the function definition.
$tempArr = array();
$tempArr[0] = 1982;
$tempArr[1] = 2007;
echo "originally:";
printArray($tempArr);
tempFunc2($tempArr);
echo "after chang in function";
printArray($tempArr);
function tempFunc2(&$obj) {
$obj[0] = 1;
$obj[1] = 2;
}
function printArray($ta) {
echo "<pre>";
print_r($ta);
echo "</pre>";
}
The Greek island of Naxos is rich with a certain type of mineral called Emery. Before the decline of the industry, the town of Moutsouna was buzzing with boats docking to get their next shipment. Today Moutsouna is used as a small tourist destination with nice villas and cafes for people looking to get away from the busy city life. I spent an afternoon here while I was in Naxos and saw the ultimate BBQ near the water (were the boats used to dock). Give me that BBQ with that view and I’m happy.