new spl features in php 5.3 (tek-x)

Post on 18-Nov-2014

13.497 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

New SPL Features in PHP 5.3

Matthew TurlandCodeWorks '09 Webcast Series

June 26, 2009

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

A Long, Long Time Ago (Or Not)

● Alexander Stepanov

● Conceived of the

STL for C++

● Goals of SPL are

somewhat similar

Pre-5.3 SPL Features

● Classes: ArrayObject, SplFileInfo...

● Interfaces: ArrayAccess, Countable...

● Exceptions: BadFunctionCallException...

● Functions: spl_autoload_register...

In a Galaxy Not So Far Away

Oh yeah, and iterators.

So What's New?

In comparison to the STL:

● Iterators? Nope.

● Algorithms? Nope.

● Functors? Nope.

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

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

We Don't NeedNo Stinkin' Containers!

array() 'string'

Sure We Do! Here's Why...

Scalability!!!

Arrays Are Great

● They're a general purpose container.

● This makes them flexible.

● Its underlying algorithm isn't always best.

Benchmarks

● Lies, Damned Lies, and Benchmarks - YMMV

● PHP 5.3.0RC4 compiled on Ubuntu 9.04

● Intel Core2Duo 1.83GHz, 4 GB DDR2-RAM

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";

The List

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.

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;}

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

SplFixedArray Graph

10 100 1000 10000 1000000

10000

20000

30000

40000

50000

60000

70000

80000

90000

SplFixedArrayArray

Elements

Tim

e (µ

s)

The Stack

SplStack

● Last In, First Out (LIFO)

● 2 Operations

● Push - [] for both

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

SplStack Code

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

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();}

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

SplStack Graph

10 100 1000 10000 1000000

20000

40000

60000

80000

100000

120000

140000

160000

SplStackArray

Elements

Tim

e (μ

s)

The Queue

SplQueue

● First In, First Out (FIFO)

● 2 Operations

● Enqueue - [] for both

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

SplQueue Code

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

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();}

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

10100

100010000

100000

0

5000000

10000000

15000000

20000000

25000000

30000000

35000000

SplQueueArray

Elements

Tim

e (μ

s)SplQueue Graph

The Heap

SplHeap, SplMinHeap, SplMaxHeap

● Highest / Lowest First Out

● 2 Operations

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

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

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);}

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();}

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

SplMinHeap Graph

10100

100010000

100000

0

5000000

10000000

15000000

20000000

25000000

30000000

35000000

SplMinHeapArray

Elements

Tim

e (μ

s)

The Priority Queue

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

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); }}

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(); }}

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

SplPriorityQueue Graph

10 100 1000 10000 1000000

5000000

10000000

15000000

20000000

25000000

30000000

35000000

SplPriorityQueueArray

Elements

Tim

e (μ

s)

By the way, thank this guy

Etienne Kneuss

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

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

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

● matt@ishouldbecoding.com or matthew@blueparabola.com

● 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

top related