44
New SPL Features in PHP 5.3 Matthew Turland CodeWorks '09 Webcast Series June 26, 2009

New SPL Features in PHP 5.3 (TEK-X)

Embed Size (px)

DESCRIPTION

http://matthewturland.com/2010/05/20/new-spl-features-in-php-5-3/

Citation preview

Page 1: New SPL Features in PHP 5.3 (TEK-X)

New SPL Features in PHP 5.3

Matthew TurlandCodeWorks '09 Webcast Series

June 26, 2009

Page 2: New SPL Features in PHP 5.3 (TEK-X)

Salut! Comment sa-va?

● Senior Consultant at Blue Parabola

● Native of Duson, Louisiana

● Author and TE for php|architect Magazine

● Book coming soon from php|architect

● Contributor to Zend Framework project

Page 3: New SPL Features in PHP 5.3 (TEK-X)

A Long, Long Time Ago (Or Not)

● Alexander Stepanov

● Conceived of the

STL for C++

● Goals of SPL are

somewhat similar

Page 4: New SPL Features in PHP 5.3 (TEK-X)

Pre-5.3 SPL Features

● Classes: ArrayObject, SplFileInfo...

● Interfaces: ArrayAccess, Countable...

● Exceptions: BadFunctionCallException...

● Functions: spl_autoload_register...

In a Galaxy Not So Far Away

Page 5: New SPL Features in PHP 5.3 (TEK-X)

Oh yeah, and iterators.

Page 6: New SPL Features in PHP 5.3 (TEK-X)

So What's New?

In comparison to the STL:

● Iterators? Nope.

● Algorithms? Nope.

● Functors? Nope.

● Well, there's only one thing left then...

Page 7: New SPL Features in PHP 5.3 (TEK-X)

Containers

“A container is a class, a data structure, or an abstract data type whose instances are collections of other objects. They are used to store objects in an organized way following specific access rules.”

“Container (data structure)” - Wikipedia

Page 8: New SPL Features in PHP 5.3 (TEK-X)

We Don't NeedNo Stinkin' Containers!

array() 'string'

Page 9: New SPL Features in PHP 5.3 (TEK-X)

Sure We Do! Here's Why...

Scalability!!!

Page 10: New SPL Features in PHP 5.3 (TEK-X)

Arrays Are Great

● They're a general purpose container.

● This makes them flexible.

● Its underlying algorithm isn't always best.

Page 11: New SPL Features in PHP 5.3 (TEK-X)

Benchmarks

● Lies, Damned Lies, and Benchmarks - YMMV

● PHP 5.3.0RC4 compiled on Ubuntu 9.04

● Intel Core2Duo 1.83GHz, 4 GB DDR2-RAM

Page 12: New SPL Features in PHP 5.3 (TEK-X)

Benchmark Runner

#!/bin/bash# 20 = # executions to perform# 100 = # elements in the container# ./bench.sh test.php 20 100time=`/home/matt/Documents/Projects/php-5.3.0RC4/build/php_build/bin/php-cgi -q -T $2 $1 $3 2>&1 | tail -n 1 | cut -d " " -f 3`;avg=`echo "scale=6; $time / $2" | bc`;echo "$1 $avg";

Page 13: New SPL Features in PHP 5.3 (TEK-X)

The List

Page 14: New SPL Features in PHP 5.3 (TEK-X)

SplFixedArray

● Like an array, but with a fixed length.

● Only allows integers >= 0 for keys.

● Can be resized, but at a cost.

● Great for simple enumerated lists.

Page 15: New SPL Features in PHP 5.3 (TEK-X)

SplFixedArray Code

<?php$a = array();for ($i = 0; $i < $argv[1]; $i++) { $a[$i] = $i;}

<?php$a = new SplFixedArray($argv[1]);for ($i = 0; $i < $argv[1]; $i++) { $a[$i] = $i;}

Page 16: New SPL Features in PHP 5.3 (TEK-X)

SplFixedArray Results

Elements SplFixedArray Array Ratio

10 371 µs 293 µs 1.266

100 501 µs 783 µs 0.640

1,000 899 µs 1,151 µs 0.781

10,000 8,229 µs 9,628 µs 0.855

100,000 49,481 µs 81,028 µs 0.610

Page 17: New SPL Features in PHP 5.3 (TEK-X)

SplFixedArray Graph

10 100 1000 10000 1000000

10000

20000

30000

40000

50000

60000

70000

80000

90000

SplFixedArrayArray

Elements

Tim

e (µ

s)

Page 18: New SPL Features in PHP 5.3 (TEK-X)

The Stack

Page 19: New SPL Features in PHP 5.3 (TEK-X)

SplStack

● Last In, First Out (LIFO)

● 2 Operations

● Push - [] for both

● Pop – array_pop() vs ->pop()

Page 20: New SPL Features in PHP 5.3 (TEK-X)

SplStack Code

<?php$a = array();for($i = 0; $i < $argv[1]; $i++) { $a[] = $i;}for($i = 0; $i < $argv[1]; $i++) { array_pop($a);}

Page 21: New SPL Features in PHP 5.3 (TEK-X)

SplStack Code (cont.)

<?php$a = new SplStack;for($i = 0; $i < $argv[1]; $i++) { $a[] = $i;}for($i = 0; $i < $argv[1]; $i++) { $a->pop();}

Page 22: New SPL Features in PHP 5.3 (TEK-X)

SplStack Results

Elements SplStack Array Ratio

10 394 µs 311 µs 1.267

100 595 µs 462 µs 1.288

1,000 2,417 µs 2,021 µs 1.196

10,000 15,525 µs 14,296 µs 1.086

100,000 135,854 µs 124,955 µs 1.087

Page 23: New SPL Features in PHP 5.3 (TEK-X)

SplStack Graph

10 100 1000 10000 1000000

20000

40000

60000

80000

100000

120000

140000

160000

SplStackArray

Elements

Tim

e (μ

s)

Page 24: New SPL Features in PHP 5.3 (TEK-X)

The Queue

Page 25: New SPL Features in PHP 5.3 (TEK-X)

SplQueue

● First In, First Out (FIFO)

● 2 Operations

● Enqueue - [] for both

● Dequeue – array_shift() vs ->dequeue()

Page 26: New SPL Features in PHP 5.3 (TEK-X)

SplQueue Code

<?php$a = array();for($i = 0; $i < $argv[1]; $i++) { $a[] = $i;}for($i = 0; $i < $argv[1]; $i++) { array_shift($a);}

Page 27: New SPL Features in PHP 5.3 (TEK-X)

SplQueue Code (cont.)

<?php$a = new SplQueue;for($i = 0; $i < $argv[1]; $i++) { $a[] = $i;}for($i = 0; $i < $argv[1]; $i++) { $a->dequeue();}

Page 28: New SPL Features in PHP 5.3 (TEK-X)

SplQueue Results

Elements SplQueue Array Ratio

10 390 µs 347 µs 1.124

100 657 µs 811 µs 0.810

1,000 2,918 µs 14,722 µs 0.198

10,000 17,322 µs 1,440,558 µs 0.012

100,000 137,136 µs 31,413,805 µs 0.004

Page 29: New SPL Features in PHP 5.3 (TEK-X)

10100

100010000

100000

0

5000000

10000000

15000000

20000000

25000000

30000000

35000000

SplQueueArray

Elements

Tim

e (μ

s)SplQueue Graph

Page 30: New SPL Features in PHP 5.3 (TEK-X)

The Heap

Page 31: New SPL Features in PHP 5.3 (TEK-X)

SplHeap, SplMinHeap, SplMaxHeap

● Highest / Lowest First Out

● 2 Operations

● Insert - [] and sort() vs ->insert()

● Remove – array_shift() vs ->extract()

Page 32: New SPL Features in PHP 5.3 (TEK-X)

SplMinHeap Code

<?php$a = array();for($i = 0; $i < $argv[1]; $i++) { $a[] = rand(1, $argv[1]); sort($a);}for($i = 0; $i < $argv[1]; $i++) { array_shift($a);}

Page 33: New SPL Features in PHP 5.3 (TEK-X)

SplMinHeap Code (cont.)

<?php$a = new SplMinHeap;for($i = 0; $i < $argv[1]; $i++) { $a->insert(rand(1, $argv[1]));}for($i = 0; $i < $argv[1]; $i++) { $a->extract();}

Page 34: New SPL Features in PHP 5.3 (TEK-X)

SplMinHeap Results

Elements SplMinHeap Array Ratio

10 516 µs 365 µs 1.414

100 847 µs 2,698 µs 0.314

1,000 4,629 µs 150,179 µs 0.031

10,000 26,459 µs 23,144,131 µs 0.001

100,000 371,613 µs 31,974,805 µs 0.012

Page 35: New SPL Features in PHP 5.3 (TEK-X)

SplMinHeap Graph

10100

100010000

100000

0

5000000

10000000

15000000

20000000

25000000

30000000

35000000

SplMinHeapArray

Elements

Tim

e (μ

s)

Page 36: New SPL Features in PHP 5.3 (TEK-X)

The Priority Queue

Page 37: New SPL Features in PHP 5.3 (TEK-X)

SplPriorityQueue

● Operates similarly to a heap

● In fact, uses a heap internally for storage

● Accepts a priority with the element value

● Element with highest priority comes out first

Page 38: New SPL Features in PHP 5.3 (TEK-X)

SplPriorityQueue Code

<?phpfunction priority_sort($a,$b) { return $a[1]-$b[1];}$a = array();$threshold = (int) $argv[1] * 0.1;for($i = 0; $i < $argv[1]; $i++) { $a[] = array($i, rand(1,10)); usort($a, 'priority_sort'); if ($i > $threshold) { array_shift($a); }}

Page 39: New SPL Features in PHP 5.3 (TEK-X)

SplPriorityQueue Code (cont.)

<?php$threshold = $argv[1] * 0.1;$a = new SplPriorityQueue;for($i = 0; $i < $argv[1]; $i++) { $a->insert($i, rand(1,10)); if ($i > $threshold) { $a->extract(); }}

Page 40: New SPL Features in PHP 5.3 (TEK-X)

SplPriorityQueue Results

Elements SplPriorityQueue Array Ratio

10 369 µs 450 µs 0.820

100 818 µs 4,583 µs 0.178

1,000 6,752 µs 346,094 µs 0.020

10,000 39,308 µs 30,710,530 µs 0.001

100,000 484,752 µs 30,587,806 µs 0.016

Page 41: New SPL Features in PHP 5.3 (TEK-X)

SplPriorityQueue Graph

10 100 1000 10000 1000000

5000000

10000000

15000000

20000000

25000000

30000000

35000000

SplPriorityQueueArray

Elements

Tim

e (μ

s)

Page 42: New SPL Features in PHP 5.3 (TEK-X)

By the way, thank this guy

Etienne Kneuss

Page 43: New SPL Features in PHP 5.3 (TEK-X)

Some Great SPL Resources

● http://php.net/spl

● http://colder.ch

● http://blueparabola.com/blog/spl-deserves-some-reiteration

● http://elizabethmariesmith.com/slides/spl_to_the_rescue.pdf

Page 44: New SPL Features in PHP 5.3 (TEK-X)

C'est tous!● http://ishouldbecoding.com

● http://www.blueparabola.com/blogs/matthew-turland

[email protected] or [email protected]

● Elazar on the Freenode IRC network

● Look for me in Dallas, Atlanta, Miami,

Washington, and New York City

at CodeWorks 2009!

● Watch for my book next quarter!Source: Christian Flickinger