SlideShare ist ein Scribd-Unternehmen logo
1 von 63
Exploring DataStructures
SPL – Standard PHP Library
• SPL provides a standard set of interfaces for PHP5
• The aim of SPL is to implement some efficient data access interfaces
and classes for PHP
• Introduced with PHP 5.0.0
• Included as standard with PHP since version 5.3.0
• SPL DataStructures were added for version 5.3.0
SPL DataStructures
Dictionary DataStructures (Maps)
• Fixed Arrays
Linear DataStructures
• Doubly-Linked Lists
• Stacks
• Queues
Tree DataStructures
• Heaps
SPL DataStructures – Why use them?
• Can improve performance
• When the right structures are used in the right place
• Can reduce memory usage
• When the right structures are used in the right place
• Already implemented and tested in PHP core
• Saves work!
• Can be type-hinted in function/method definitions
SPL DataStructures
Dictionary DataStructures (Maps)
• Fixed Arrays
Linear DataStructures
Tree DataStructures
Fixed Arrays – SplFixedArray
• Predefined Size
• Enumerated indexes only, not Associative
• Indexed from 0, Increments by 1
• Is an object
• No hashing required for keys
• Implements
• Iterator
• ArrayAccess
• Countable
Fixed Arrays – Uses
• Returned Database resultsets, Record collections
• Hours of Day
• Days of Month/Year
• Hotel Rooms, Airline seats
As a 2-d fixed array of fixed array
Fixed Arrays – Big-O Complexity
• Insert an element O(1)
• Delete an element O(1)
• Lookup an element O(1)
• Resize a Fixed Array O(n)
Fixed Arrays
Standard Arrays SplFixedArray
Data Record 1
Key 12345
Data Record 2
Key 23456
Data Record 4
Key 34567
Data Record 3
Key 45678
[0]
[1]
[2]
[…]
[…]
[12]
[n-1]
Hash
Function
Key 12345
Key 23456
Key 45678
Key 34567
Data Record 1
Key 0
Data Record 2
Key 1
Data Record 3
Key 2
Data Record 4
Key 3
[0]
[1]
[2]
[…]
[…]
[12]
[n-1]
Key 0
Key 1
Key 2
Key 3
Fixed Arrays
$a = array();
for ($i = 0; $i < $size; ++$i) {
$a[$i] = $i;
}
// Random/Indexed access
for ($i = 0; $i < $size; ++$i) {
$r = $a[$i];
}
// Sequential access
foreach($a as $v) {
}
// Sequential access with keys
foreach($a as $k => $v) {
}
Initialise: 0.0000 s
Set 1,000,000 Entries: 0.1323 s
Random Read 1,000,000 Entries: 0.3311 s
Iterate values for 1,000,000 Entries: 0.0146 s
Iterate keys and values for 1,000,000 Entries: 0.0198 s
Total Time: 0.4979 s
Memory: 41,668.32 k
Fixed Arrays
$a = new SplFixedArray($size);
for ($i = 0; $i < $size; ++$i) {
$a[$i] = $i;
}
// Random/Indexed access
for ($i = 0; $i < $size; ++$i) {
$r = $a[$i];
}
// Sequential access
foreach($a as $v) {
}
// Sequential access with keys
foreach($a as $k => $v) {
}
Initialise: 0.0011 s
Set 1,000,000 Entries: 0.1061 s
Random Read 1,000,000 Entries: 0.3144 s
Iterate values for 1,000,000 Entries: 0.0394 s
Iterate keys and values for 1,000,000 Entries: 0.0476 s
Total Time: 0.5086 s
Memory: 17,889.43 k
Fixed Arrays
0.0000
0.0500
0.1000
0.1500
0.2000
0.2500
0.3000
0.3500
Initialise (s) Set Values (s) Sequential Read (s) Sequential Read
with Key (s)
Random Read (s) Pop (s)
Speed
SPL Fixed Array Standard PHP Array
0.00
5,000.00
10,000.00
15,000.00
20,000.00
25,000.00
30,000.00
35,000.00
40,000.00
45,000.00
Current Memory (k) Peak Memory (k)
Memory Usage
SPL Fixed Array Standard PHP Array
Fixed Arrays
• Faster to populate
• Lower memory usage
• Faster for random/indexed access than standard PHP arrays
• Slower for sequential access than standard PHP arrays
Fixed Arrays – Gotchas
• Can be extended, but at a cost in speed
• Standard array functions won’t work with SplFixedArray
e.g. array_walk(), sort(), array_pop(), array_intersect(), etc
• Avoid unsetting elements if possible
• Unlike standard PHP enumerated arrays, this leaves empty nodes that trigger
an Exception if accessed
SPL DataStructures
Dictionary DataStructures (Maps)
Linear DataStructures
• Doubly-Linked Lists
• Stacks
• Queues
Tree DataStructures
Doubly Linked Lists – SplDoublyLinkedList
Doubly Linked Lists
• Iterable Lists
• Top to Bottom
• Bottom to Top
• Unindexed
• Good for sequential access
• Not good for random/indexed access
• Implements
• Iterator
• ArrayAccess
• Countable
Doubly Linked Lists – Uses
• Stacks
• Queues
• Most-recently used lists
• Undo functionality
• Trees
• Memory Allocators
• Fast dynamic, iterable arrays
• iNode maps
• Video frame queues
Doubly Linked Lists – Big-O Complexity
• Insert an element by index O(1)
• Delete an element by index O(1)
• Lookup by index O(n)
• I have seen people saying that SplDoublyLinkedList behaves like a hash table
for lookups, which would make it O(1); but timing tests prove otherwise
• Access a node at the beginning of the list O(1)
• Access a node at the end of the list O(1)
Doubly Linked Lists
Head Tail
A B C D E
Doubly Linked Lists
$a = array();
for ($i = 0; $i < $size; ++$i) {
$a[$i] = $i;
}
// Random/Indexed access
for ($i = 0; $i < $size; ++$i) {
$r = $a[$i];
}
// Sequential access
for ($i = $size-1; $i >= 0; --$i) {
$r = array_pop($a);
}
Initialise: 0.0000 s
Set 100,000 Entries: 0.0585 s
Random Read 100,000 Entries: 0.0378 s
Pop 100,000 Entries: 0.1383 s
Total Time: 0.2346 s
Memory: 644.55 k
Peak Memory: 8457.91 k
Doubly Linked Lists
$a = new SplDoublyLinkedList();
for ($i = 0; $i < $size; ++$i) {
$a->push($i);
}
// Random/Indexed access
for ($i = 0; $i < $size; ++$i) {
$r = $a->offsetGet($i);
}
// Sequential access
for ($i = $size-1; $i >= 0; --$i) {
$a->pop();
}
Initialise: 0.0000 s
Set 100,000 Entries: 0.1514 s
Random Read 100,000 Entries: 22.7068 s
Pop 100,000 Entries: 0.1465 s
Total Time: 23.0047 s
Memory: 133.67 k
Peak Memory: 5603.09 k
Doubly Linked Lists
• Fast for sequential access
• Lower memory usage
• Traversable in both directions
Use setIteratorMode() to determine direction
• Size limited only by memory
• Slow for random/indexed access
• Insert into middle of list only available from PHP 5.5.0
SPL DataStructures
Dictionary DataStructures (Maps)
Linear DataStructures
• Doubly-Linked Lists
• Stacks
• Queues
Tree DataStructures
Stacks – SplStack
Stacks
• Implemented as a Doubly-Linked List
• LIFO
• Last-In
• First-Out
• Essential Operations
• push()
• pop()
• Optional Operations
• count()
• isEmpty()
• peek()
Stack – Uses
• Undo mechanism (e.g. In text editors)
• Backtracking (e.g. Finding a route through a maze or network)
• Call Handler (e.g. Defining return location for nested calls)
• Shunting Yard Algorithm (e.g. Converting Infix to Postfix notation)
• Evaluating a Postfix Expression
• Depth-First Search
Stacks – Big-O Complexity
• Push an element O(1)
• Pop an element O(1)
Stacks
class StandardArrayStack {
private $stack = array();
public function count() {
return count($this->stack);
}
public function push($data) {
$this->stack[] = $data;
}
public function pop() {
if (count($this->stack) > 0) {
return array_pop($this->stack);
}
return NULL;
}
function isEmpty() {
return count($this->stack) == 0;
}
}
Stacks
$a = new StandardArrayStack();
for ($i = 1; $i <= $size; ++$i) {
$a->push($i);
}
while (!$a->isEmpty()) {
$i = $a->pop();
}
PUSH 100,000 ENTRIES
Push Time: 0.5818 s
Current Memory: 8.75
POP 100,000 ENTRIES
Pop Time: 1.6657 s
Current Memory: 2.25
Total Time: 2.2488 s
Current Memory: 2.25
Peak Memory: 8.75
Stacks
class StandardArrayStack2 {
private $stack = array();
private $count = 0;
public function count() {
return $this->count;
}
public function push($data) {
++$this->count;
$this->stack[] = $data;
}
public function pop() {
if ($this->count > 0) {
--$this->count;
return array_pop($this->stack);
}
return NULL;
}
function isEmpty() {
return $this->count == 0;
}
}
Stacks
$a = new StandardArrayStack2();
for ($i = 1; $i <= $size; ++$i) {
$a->push($i);
}
while (!$a->isEmpty()) {
$i = $a->pop();
}
PUSH 100,000 ENTRIES
Push Time: 0.5699 s
Current Memory: 8.75
POP 100,000 ENTRIES
Pop Time: 1.1005 s
Current Memory: 1.75
Total Time: 1.6713 s
Current Memory: 1.75
Peak Memory: 8.75
Stacks
$a = new SplStack();
for ($i = 1; $i <= $size; ++$i) {
$a->push($i);
}
while (!$a->isEmpty()) {
$i = $a->pop();
}
PUSH 100,000 ENTRIES
Push Time: 0.4301 s
Current Memory: 5.50
POP 100,000 ENTRIES
Pop Time: 0.6413 s
Current Memory: 0.75
Total Time: 1.0723 s
Current Memory: 0.75
Peak Memory: 5.50
Stacks
0.0796 0.0782
0.0644
0.1244
0.0998
0.0693
8.75 8.75
5.50
0
1
2
3
4
5
6
7
8
9
10
0.0000
0.0200
0.0400
0.0600
0.0800
0.1000
0.1200
0.1400
StandardArrayStack StandardArrayStack2 SPLStack
Memory(MB)
Time(seconds)
Stack Timings
Push Time (s)
Pop Time (s)
Memory after Push (MB)
Stacks – Gotchas
• Peek (view an entry from the middle of the stack)
• StandardArrayStack
public function peek($n = 0) {
if ((count($this->stack) - $n) < 0) {
return NULL;
}
return $this->stack[count($this->stack) - $n - 1];
}
• StandardArrayStack2
public function peek($n = 0) {
if (($this->count - $n) < 0) {
return NULL;
}
return $this->stack[$this->count - $n - 1];
}
• SplStack
$r = $a->offsetGet($n);
Stacks – Gotchas
0.0075 0.0077 0.0064
0.0111
0.0078
0.1627
0.0124
0.0098
0.0066
1.00 1.00
0.75
0.00
0.20
0.40
0.60
0.80
1.00
1.20
0.0000
0.0200
0.0400
0.0600
0.0800
0.1000
0.1200
0.1400
0.1600
0.1800
StandardArrayStack StandardArrayStack2 SPLStack
Memory(MB)
Time(seconds)
Stack Timings
Push Time (s)
Peek Time (s)
Pop Time (s)
Memory after Push (MB)
Stacks – Gotchas
• Peek
When looking through the stack, SplStack has to follow each link in the “chain”
until it finds the nth entry
O(n)
SPL DataStructures
Dictionary DataStructures (Maps)
Linear DataStructures
• Doubly-Linked Lists
• Stacks
• Queues
Tree DataStructures
Queues – SplQueue
Queues
• Implemented as a Doubly-Linked List
• FIFO
• First-In
• First-Out
• Essential Operations
• enqueue()
• dequeue()
• Optional Operations
• count()
• isEmpty()
• peek()
Queues – Uses
• Job/print/message submissions
• Breadth-First Search
• Request handling (e.g. a Web server)
Queues – Big-O Complexity
• Enqueue an element O(1)
• Dequeue an element O(1)
Queues
class StandardArrayQueue2 {
private $queue = array();
private $count = 0;
public function count() {
return $this->count;
}
public function enqueue($data) {
++$this->count;
$this->queue[] = $data;
}
public function dequeue() {
if ($this->count > 0) {
--$this->count;
return array_shift($this->queue);
}
return NULL;
}
function isEmpty() {
return $this->count == 0;
}
}
Queues
$a = new StandardArrayQueue2();
for ($i = 1; $i <= $size; ++$i) {
$a->enqueue($i);
}
while (!$a->isEmpty()) {
$i = $a->dequeue();
}
ENQUEUE 100,000 ENTRIES
Enqueue Time: 0.6884
Current Memory: 8.75
DEQUEUE 100,000 ENTRIES
Dequeue Time: 335.8434
Current Memory: 1.75
Total Time: 336.5330
Current Memory: 1.75
Peak Memory: 8.75
Queues
$a = new SplQueue();
for ($i = 1; $i <= $size; ++$i) {
$a->enqueue($i);
}
while (!$a->isEmpty()) {
$i = $a->dequeue();
}
ENQUEUE 100,000 ENTRIES
Enqueue Time: 0.4087
Current Memory: 5.50
DEQUEUE 100,000 ENTRIES
Dequeue Time: 0.6148
Current Memory: 0.75
Total Time: 1.0249
Current Memory: 0.75
Peak Memory: 5.50
Queues
0.0075 0.0080 0.00640.0087 0.0070
0.1582
0.6284 0.6277
0.0066
1.00 1.00
0.75
0.00
0.20
0.40
0.60
0.80
1.00
1.20
0.0000
0.1000
0.2000
0.3000
0.4000
0.5000
0.6000
0.7000
StandardArrayQueue StandardArrayQueue2 SPLQueue
Memory(MB)
Time(seconds)
Queue Timings
Enqueue Time (s)
Peek Time (s)
Dequeue Time (s)
Memory after Enqueue (MB)
Queues – Gotchas
• Dequeue
In standard PHP enumerated arrays, shift() and unshift() are expensive
operations because they re-index the entire array
This problem does not apply to SplQueue
• Peek
When looking through the queue, SplQueue has to follow each link in the
“chain” until it finds the nth entry
SPL DataStructures
Dictionary DataStructures (Maps)
Linear DataStructures
Tree DataStructures
• Heaps
Heaps – SplHeap
 
Heaps
• Ordered Lists
• Random Input
• Ordered Output
• Implemented as a binary tree structure
• Essential Operations
• Insert
• Extract
• Ordering Rule
• Abstract that requires extending with the implementation of a compare()
algorithm
• compare() is reversed in comparison with usort compare callbacks
• Partially sorted on data entry
Heaps
• Implements
• Iterator
• Countable
Heaps – Uses
• Heap sort
• Selection algorithms (e.g. Max, Min, Median)
• Graph algorithms
• Prim’s Minimal Spanning Tree (connected weighted undirected graph)
• Dijkstra’s Shortest Path (network or traffic routing)
• Priority Queues
Heaps – Big-O Complexity
• Insert an element O(log n)
• Delete an element O(log n)
• Access root element O(1)
Heaps
class DistanceSplHeap extends SplHeap {
protected $longitude = 0.0;
protected $latitude = 0.0;
private $comparator;
public function __construct(Callable $comparator,
$longitude, $latitude) {
$this->longitude = $longitude;
$this->latitude = $latitude;
$this->comparator = $comparator;
}
protected function compare($a, $b) {
return call_user_func(
$this->comparator, $a, $b
);
}
public function insert($value) {
$value->distance =
$this->calculateDistance($value);
parent::insert($value);
}
}
$comparator = function($a, $b) {
if ($a->distance == $b->distance)
return 0;
return ($a->distance > $b->distance)
? -1
: 1;
};
// Latitude and Longitude for Barcelona
$speakersHeap = new DistanceSplHeap(
$comparator,
2.1833,
41.3833
);
Heaps
$file = new SplFileObject("speakers.csv");
$file->setFlags(
SplFileObject::DROP_NEW_LINE |
SplFileObject::SKIP_EMPTY
);
while (!$file->eof()) {
$speakerData = $file->fgetcsv();
$speaker = new StdClass;
$speaker->name = $speakerData[0];
$speaker->from = $speakerData[1];
$speaker->latitude = $speakerData[2];
$speaker->longitude = $speakerData[3];
$speakersHeap->insert($speaker);
}
"Anthony Ferrara","New Jersey, USA",40.0000,-74.5000
"Bastian Hofmann","Berlin, Germany",52.5167,13.3833
"Damien Seguy","The Hague, Netherlands",52.0833,4.3167
"Derick Rethans","London, UK",51.5072,-0.1275
"Juozas Kaziukėnas","New York, USA",40.7127,-74.0059
"Marcello Duarte","London, UK",51.5072,-0.1275
"Mark Baker","Wigan, UK",53.5448,-2.6318
"Mathias Verraes","Belgium",50.8500,4.3500
"Matthias Noback","Utrecht, Netherlands",52.0833,5.1167
"Nikita Popov","Berlin, Germany",52.5167,13.3833
"Paweł Jędrzejewski","Łódź, Poland",51.7833,19.4667
"Steve Maraspin","Udine, Italy",46.0667,13.2333
"Tudor Barbu","Barcelona, Catalonia",41.3833,2.1833
"Zeev Suraski","Israel",31.0000,35.0000
Heaps
echo 'There are ', $speakersHeap->count(),
' speakers at PHPBarcelona 2015',
PHP_EOL, PHP_EOL;
echo 'Distance that they have travelled to
reach Barcelona', PHP_EOL, PHP_EOL;
foreach($speakersHeap as $speaker) {
echo sprintf(
"%-22s from %-
24s has travelled %6.1f miles".PHP_EOL,
$speaker->name,
$speaker->from,
$speaker->distance
);
}
echo PHP_EOL;
There are 14 speakers at PHPBarcelona 2015
Distance that they have travelled to reach Barcelona
Tudor Barbu from Barcelona, Catalonia has travelled 0.0 miles
Steve Maraspin from Udine, Italy has travelled 638.8 miles
Mathias Verraes from Belgium has travelled 662.2 miles
Derick Rethans from London, UK has travelled 708.0 miles
Marcello Duarte from London, UK has travelled 708.0 miles
Damien Seguy from The Hague, Netherlands has travelled 746.0 miles
Matthias Noback from Utrecht, Netherlands has travelled 752.0 miles
Mark Baker from Wigan, UK has travelled 869.3 miles
Nikita Popov from Berlin, Germany has travelled 930.8 miles
Bastian Hofmann from Berlin, Germany has travelled 930.8 miles
Paweł Jędrzejewski from Łódź, Poland has travelled 1085.9 miles
Zeev Suraski from Israel has travelled 1951.0 miles
Juozas Kaziukėnas from New York, USA has travelled 3831.8 miles
Anthony Ferrara from New Jersey, USA has travelled 3877.9 miles
Heaps – Gotchas
• Compare method is reversed logic from a usort() callback
• Traversing the heap removes elements from the heap
SPL – Standard PHP Library
Other SPL Datastructures
• SplMaxHeap
• SplMinHeap
• SplPriorityQueue
• SplObjectStorage
SPL – Standard PHP Library
E-Book
Mastering the SPL Library
Joshua Thijssen
Available in PDF, ePub, Mobi
http://www.phparch.com/books/mastering-the-spl-library/
SPL – Standard PHP Library
E-Book
Iterating PHP Iterators
Cal Evans
Available in PDF, ePub, Mobi
https://leanpub.com/iteratingphpiterators
SPL DataStructures
?
Questions
Who am I?
Mark Baker
Design and Development Manager
InnovEd (Innovative Solutions for Education) Ltd
Coordinator and Developer of:
Open Source PHPOffice library
PHPExcel, PHPWord, PHPPowerPoint, PHPProject, PHPVisio
Minor contributor to PHP core
@Mark_Baker
https://github.com/MarkBaker
http://uk.linkedin.com/pub/mark-baker/b/572/171
SPL: The Undiscovered Library
– Exploring Datastructures
http://joind.in/talk/view/15874

Weitere ähnliche Inhalte

Was ist angesagt?

Bioinformatics p5-bioperl v2013-wim_vancriekinge
Bioinformatics p5-bioperl v2013-wim_vancriekingeBioinformatics p5-bioperl v2013-wim_vancriekinge
Bioinformatics p5-bioperl v2013-wim_vancriekinge
Prof. Wim Van Criekinge
 
Scala - den smarta kusinen
Scala - den smarta kusinenScala - den smarta kusinen
Scala - den smarta kusinen
Redpill Linpro
 
Solr Black Belt Pre-conference
Solr Black Belt Pre-conferenceSolr Black Belt Pre-conference
Solr Black Belt Pre-conference
Erik Hatcher
 

Was ist angesagt? (20)

Solr & Lucene @ Etsy by Gregg Donovan
Solr & Lucene @ Etsy by Gregg DonovanSolr & Lucene @ Etsy by Gregg Donovan
Solr & Lucene @ Etsy by Gregg Donovan
 
Solr 6 Feature Preview
Solr 6 Feature PreviewSolr 6 Feature Preview
Solr 6 Feature Preview
 
Manifests of Future Past
Manifests of Future PastManifests of Future Past
Manifests of Future Past
 
Groovy unleashed
Groovy unleashed Groovy unleashed
Groovy unleashed
 
Apache cassandra in 2016
Apache cassandra in 2016Apache cassandra in 2016
Apache cassandra in 2016
 
Solr vs. Elasticsearch, Case by Case: Presented by Alexandre Rafalovitch, UN
Solr vs. Elasticsearch,  Case by Case: Presented by Alexandre Rafalovitch, UNSolr vs. Elasticsearch,  Case by Case: Presented by Alexandre Rafalovitch, UN
Solr vs. Elasticsearch, Case by Case: Presented by Alexandre Rafalovitch, UN
 
Bioinformatics p5-bioperl v2013-wim_vancriekinge
Bioinformatics p5-bioperl v2013-wim_vancriekingeBioinformatics p5-bioperl v2013-wim_vancriekinge
Bioinformatics p5-bioperl v2013-wim_vancriekinge
 
Php Introduction nikul
Php Introduction nikulPhp Introduction nikul
Php Introduction nikul
 
Avro introduction
Avro introductionAvro introduction
Avro introduction
 
Bioinformatica p6-bioperl
Bioinformatica p6-bioperlBioinformatica p6-bioperl
Bioinformatica p6-bioperl
 
Drools5 Community Training HandsOn 1 Drools DRL Syntax
Drools5 Community Training HandsOn 1 Drools DRL SyntaxDrools5 Community Training HandsOn 1 Drools DRL Syntax
Drools5 Community Training HandsOn 1 Drools DRL Syntax
 
Scala - den smarta kusinen
Scala - den smarta kusinenScala - den smarta kusinen
Scala - den smarta kusinen
 
Introductionto fp with groovy
Introductionto fp with groovyIntroductionto fp with groovy
Introductionto fp with groovy
 
Rebuilding Solr 6 Examples - Layer by Layer: Presented by Alexandre Rafalovit...
Rebuilding Solr 6 Examples - Layer by Layer: Presented by Alexandre Rafalovit...Rebuilding Solr 6 Examples - Layer by Layer: Presented by Alexandre Rafalovit...
Rebuilding Solr 6 Examples - Layer by Layer: Presented by Alexandre Rafalovit...
 
Solr Black Belt Pre-conference
Solr Black Belt Pre-conferenceSolr Black Belt Pre-conference
Solr Black Belt Pre-conference
 
The Ring programming language version 1.6 book - Part 42 of 189
The Ring programming language version 1.6 book - Part 42 of 189The Ring programming language version 1.6 book - Part 42 of 189
The Ring programming language version 1.6 book - Part 42 of 189
 
Marc’s (bio)perl course
Marc’s (bio)perl courseMarc’s (bio)perl course
Marc’s (bio)perl course
 
Meetup slides
Meetup slidesMeetup slides
Meetup slides
 
Solr Troubleshooting - TreeMap approach
Solr Troubleshooting - TreeMap approachSolr Troubleshooting - TreeMap approach
Solr Troubleshooting - TreeMap approach
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 

Ähnlich wie SPL - The Undiscovered Library - PHPBarcelona 2015

What's New in Lucene/Solr Presented by Grant Ingersoll at SolrExchage DC
What's New  in Lucene/Solr Presented by Grant Ingersoll at SolrExchage DCWhat's New  in Lucene/Solr Presented by Grant Ingersoll at SolrExchage DC
What's New in Lucene/Solr Presented by Grant Ingersoll at SolrExchage DC
Lucidworks (Archived)
 

Ähnlich wie SPL - The Undiscovered Library - PHPBarcelona 2015 (20)

MYSQL Query Anti-Patterns That Can Be Moved to Sphinx
MYSQL Query Anti-Patterns That Can Be Moved to SphinxMYSQL Query Anti-Patterns That Can Be Moved to Sphinx
MYSQL Query Anti-Patterns That Can Be Moved to Sphinx
 
Persistent Data Structures - partial::Conf
Persistent Data Structures - partial::ConfPersistent Data Structures - partial::Conf
Persistent Data Structures - partial::Conf
 
Mastering Python chapter3
Mastering Python chapter3Mastering Python chapter3
Mastering Python chapter3
 
Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...Processing data with Python, using standard library modules you (probably) ne...
Processing data with Python, using standard library modules you (probably) ne...
 
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, ItalyPHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
 
My lecture stack_queue_operation
My lecture stack_queue_operationMy lecture stack_queue_operation
My lecture stack_queue_operation
 
Looping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsLooping the Loop with SPL Iterators
Looping the Loop with SPL Iterators
 
Android Database
Android DatabaseAndroid Database
Android Database
 
SQL Server 2014 Memory Optimised Tables - Advanced
SQL Server 2014 Memory Optimised Tables - AdvancedSQL Server 2014 Memory Optimised Tables - Advanced
SQL Server 2014 Memory Optimised Tables - Advanced
 
Data Engineering with Solr and Spark
Data Engineering with Solr and SparkData Engineering with Solr and Spark
Data Engineering with Solr and Spark
 
L1 - Recap.pdf
L1 - Recap.pdfL1 - Recap.pdf
L1 - Recap.pdf
 
Understanding SQL Trace, TKPROF and Execution Plan for beginners
Understanding SQL Trace, TKPROF and Execution Plan for beginnersUnderstanding SQL Trace, TKPROF and Execution Plan for beginners
Understanding SQL Trace, TKPROF and Execution Plan for beginners
 
Java 103 intro to java data structures
Java 103   intro to java data structuresJava 103   intro to java data structures
Java 103 intro to java data structures
 
Leveraging Hadoop in your PostgreSQL Environment
Leveraging Hadoop in your PostgreSQL EnvironmentLeveraging Hadoop in your PostgreSQL Environment
Leveraging Hadoop in your PostgreSQL Environment
 
What's New in Lucene/Solr Presented by Grant Ingersoll at SolrExchage DC
What's New  in Lucene/Solr Presented by Grant Ingersoll at SolrExchage DCWhat's New  in Lucene/Solr Presented by Grant Ingersoll at SolrExchage DC
What's New in Lucene/Solr Presented by Grant Ingersoll at SolrExchage DC
 
An Introduction to Elastic Search.
An Introduction to Elastic Search.An Introduction to Elastic Search.
An Introduction to Elastic Search.
 
Data Science with Solr and Spark
Data Science with Solr and SparkData Science with Solr and Spark
Data Science with Solr and Spark
 
Scala Refactoring for Fun and Profit
Scala Refactoring for Fun and ProfitScala Refactoring for Fun and Profit
Scala Refactoring for Fun and Profit
 
Infinispan,Lucene,Hibername OGM
Infinispan,Lucene,Hibername OGMInfinispan,Lucene,Hibername OGM
Infinispan,Lucene,Hibername OGM
 
Swift, functional programming, and the future of Objective-C
Swift, functional programming, and the future of Objective-CSwift, functional programming, and the future of Objective-C
Swift, functional programming, and the future of Objective-C
 

Mehr von Mark Baker

Mehr von Mark Baker (20)

Looping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsLooping the Loop with SPL Iterators
Looping the Loop with SPL Iterators
 
Looping the Loop with SPL Iterators
Looping the Loop with SPL IteratorsLooping the Loop with SPL Iterators
Looping the Loop with SPL Iterators
 
Deploying Straight to Production
Deploying Straight to ProductionDeploying Straight to Production
Deploying Straight to Production
 
Deploying Straight to Production
Deploying Straight to ProductionDeploying Straight to Production
Deploying Straight to Production
 
Deploying Straight to Production
Deploying Straight to ProductionDeploying Straight to Production
Deploying Straight to Production
 
A Brief History of Elephpants
A Brief History of ElephpantsA Brief History of Elephpants
A Brief History of Elephpants
 
Aspects of love slideshare
Aspects of love slideshareAspects of love slideshare
Aspects of love slideshare
 
Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?
 
A Brief History of ElePHPants
A Brief History of ElePHPantsA Brief History of ElePHPants
A Brief History of ElePHPants
 
Coding Horrors
Coding HorrorsCoding Horrors
Coding Horrors
 
Anonymous classes2
Anonymous classes2Anonymous classes2
Anonymous classes2
 
Testing the Untestable
Testing the UntestableTesting the Untestable
Testing the Untestable
 
Anonymous Classes: Behind the Mask
Anonymous Classes: Behind the MaskAnonymous Classes: Behind the Mask
Anonymous Classes: Behind the Mask
 
Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?
 
Coding Horrors
Coding HorrorsCoding Horrors
Coding Horrors
 
Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?Does the SPL still have any relevance in the Brave New World of PHP7?
Does the SPL still have any relevance in the Brave New World of PHP7?
 
Giving birth to an ElePHPant
Giving birth to an ElePHPantGiving birth to an ElePHPant
Giving birth to an ElePHPant
 
A Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP GeneratorsA Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP Generators
 
A Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP GeneratorsA Functional Guide to Cat Herding with PHP Generators
A Functional Guide to Cat Herding with PHP Generators
 
Zephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensionsZephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensions
 

KĂźrzlich hochgeladen

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Bert Jan Schrijver
 

KĂźrzlich hochgeladen (20)

VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 

SPL - The Undiscovered Library - PHPBarcelona 2015

  • 2. SPL – Standard PHP Library • SPL provides a standard set of interfaces for PHP5 • The aim of SPL is to implement some efficient data access interfaces and classes for PHP • Introduced with PHP 5.0.0 • Included as standard with PHP since version 5.3.0 • SPL DataStructures were added for version 5.3.0
  • 3. SPL DataStructures Dictionary DataStructures (Maps) • Fixed Arrays Linear DataStructures • Doubly-Linked Lists • Stacks • Queues Tree DataStructures • Heaps
  • 4. SPL DataStructures – Why use them? • Can improve performance • When the right structures are used in the right place • Can reduce memory usage • When the right structures are used in the right place • Already implemented and tested in PHP core • Saves work! • Can be type-hinted in function/method definitions
  • 5. SPL DataStructures Dictionary DataStructures (Maps) • Fixed Arrays Linear DataStructures Tree DataStructures
  • 6. Fixed Arrays – SplFixedArray • Predefined Size • Enumerated indexes only, not Associative • Indexed from 0, Increments by 1 • Is an object • No hashing required for keys • Implements • Iterator • ArrayAccess • Countable
  • 7. Fixed Arrays – Uses • Returned Database resultsets, Record collections • Hours of Day • Days of Month/Year • Hotel Rooms, Airline seats As a 2-d fixed array of fixed array
  • 8. Fixed Arrays – Big-O Complexity • Insert an element O(1) • Delete an element O(1) • Lookup an element O(1) • Resize a Fixed Array O(n)
  • 9. Fixed Arrays Standard Arrays SplFixedArray Data Record 1 Key 12345 Data Record 2 Key 23456 Data Record 4 Key 34567 Data Record 3 Key 45678 [0] [1] [2] […] […] [12] [n-1] Hash Function Key 12345 Key 23456 Key 45678 Key 34567 Data Record 1 Key 0 Data Record 2 Key 1 Data Record 3 Key 2 Data Record 4 Key 3 [0] [1] [2] […] […] [12] [n-1] Key 0 Key 1 Key 2 Key 3
  • 10. Fixed Arrays $a = array(); for ($i = 0; $i < $size; ++$i) { $a[$i] = $i; } // Random/Indexed access for ($i = 0; $i < $size; ++$i) { $r = $a[$i]; } // Sequential access foreach($a as $v) { } // Sequential access with keys foreach($a as $k => $v) { } Initialise: 0.0000 s Set 1,000,000 Entries: 0.1323 s Random Read 1,000,000 Entries: 0.3311 s Iterate values for 1,000,000 Entries: 0.0146 s Iterate keys and values for 1,000,000 Entries: 0.0198 s Total Time: 0.4979 s Memory: 41,668.32 k
  • 11. Fixed Arrays $a = new SplFixedArray($size); for ($i = 0; $i < $size; ++$i) { $a[$i] = $i; } // Random/Indexed access for ($i = 0; $i < $size; ++$i) { $r = $a[$i]; } // Sequential access foreach($a as $v) { } // Sequential access with keys foreach($a as $k => $v) { } Initialise: 0.0011 s Set 1,000,000 Entries: 0.1061 s Random Read 1,000,000 Entries: 0.3144 s Iterate values for 1,000,000 Entries: 0.0394 s Iterate keys and values for 1,000,000 Entries: 0.0476 s Total Time: 0.5086 s Memory: 17,889.43 k
  • 12. Fixed Arrays 0.0000 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 Initialise (s) Set Values (s) Sequential Read (s) Sequential Read with Key (s) Random Read (s) Pop (s) Speed SPL Fixed Array Standard PHP Array 0.00 5,000.00 10,000.00 15,000.00 20,000.00 25,000.00 30,000.00 35,000.00 40,000.00 45,000.00 Current Memory (k) Peak Memory (k) Memory Usage SPL Fixed Array Standard PHP Array
  • 13. Fixed Arrays • Faster to populate • Lower memory usage • Faster for random/indexed access than standard PHP arrays • Slower for sequential access than standard PHP arrays
  • 14. Fixed Arrays – Gotchas • Can be extended, but at a cost in speed • Standard array functions won’t work with SplFixedArray e.g. array_walk(), sort(), array_pop(), array_intersect(), etc • Avoid unsetting elements if possible • Unlike standard PHP enumerated arrays, this leaves empty nodes that trigger an Exception if accessed
  • 15. SPL DataStructures Dictionary DataStructures (Maps) Linear DataStructures • Doubly-Linked Lists • Stacks • Queues Tree DataStructures
  • 16. Doubly Linked Lists – SplDoublyLinkedList
  • 17. Doubly Linked Lists • Iterable Lists • Top to Bottom • Bottom to Top • Unindexed • Good for sequential access • Not good for random/indexed access • Implements • Iterator • ArrayAccess • Countable
  • 18. Doubly Linked Lists – Uses • Stacks • Queues • Most-recently used lists • Undo functionality • Trees • Memory Allocators • Fast dynamic, iterable arrays • iNode maps • Video frame queues
  • 19. Doubly Linked Lists – Big-O Complexity • Insert an element by index O(1) • Delete an element by index O(1) • Lookup by index O(n) • I have seen people saying that SplDoublyLinkedList behaves like a hash table for lookups, which would make it O(1); but timing tests prove otherwise • Access a node at the beginning of the list O(1) • Access a node at the end of the list O(1)
  • 20. Doubly Linked Lists Head Tail A B C D E
  • 21. Doubly Linked Lists $a = array(); for ($i = 0; $i < $size; ++$i) { $a[$i] = $i; } // Random/Indexed access for ($i = 0; $i < $size; ++$i) { $r = $a[$i]; } // Sequential access for ($i = $size-1; $i >= 0; --$i) { $r = array_pop($a); } Initialise: 0.0000 s Set 100,000 Entries: 0.0585 s Random Read 100,000 Entries: 0.0378 s Pop 100,000 Entries: 0.1383 s Total Time: 0.2346 s Memory: 644.55 k Peak Memory: 8457.91 k
  • 22. Doubly Linked Lists $a = new SplDoublyLinkedList(); for ($i = 0; $i < $size; ++$i) { $a->push($i); } // Random/Indexed access for ($i = 0; $i < $size; ++$i) { $r = $a->offsetGet($i); } // Sequential access for ($i = $size-1; $i >= 0; --$i) { $a->pop(); } Initialise: 0.0000 s Set 100,000 Entries: 0.1514 s Random Read 100,000 Entries: 22.7068 s Pop 100,000 Entries: 0.1465 s Total Time: 23.0047 s Memory: 133.67 k Peak Memory: 5603.09 k
  • 23. Doubly Linked Lists • Fast for sequential access • Lower memory usage • Traversable in both directions Use setIteratorMode() to determine direction • Size limited only by memory • Slow for random/indexed access • Insert into middle of list only available from PHP 5.5.0
  • 24. SPL DataStructures Dictionary DataStructures (Maps) Linear DataStructures • Doubly-Linked Lists • Stacks • Queues Tree DataStructures
  • 26. Stacks • Implemented as a Doubly-Linked List • LIFO • Last-In • First-Out • Essential Operations • push() • pop() • Optional Operations • count() • isEmpty() • peek()
  • 27. Stack – Uses • Undo mechanism (e.g. In text editors) • Backtracking (e.g. Finding a route through a maze or network) • Call Handler (e.g. Defining return location for nested calls) • Shunting Yard Algorithm (e.g. Converting Infix to Postfix notation) • Evaluating a Postfix Expression • Depth-First Search
  • 28. Stacks – Big-O Complexity • Push an element O(1) • Pop an element O(1)
  • 29. Stacks class StandardArrayStack { private $stack = array(); public function count() { return count($this->stack); } public function push($data) { $this->stack[] = $data; } public function pop() { if (count($this->stack) > 0) { return array_pop($this->stack); } return NULL; } function isEmpty() { return count($this->stack) == 0; } }
  • 30. Stacks $a = new StandardArrayStack(); for ($i = 1; $i <= $size; ++$i) { $a->push($i); } while (!$a->isEmpty()) { $i = $a->pop(); } PUSH 100,000 ENTRIES Push Time: 0.5818 s Current Memory: 8.75 POP 100,000 ENTRIES Pop Time: 1.6657 s Current Memory: 2.25 Total Time: 2.2488 s Current Memory: 2.25 Peak Memory: 8.75
  • 31. Stacks class StandardArrayStack2 { private $stack = array(); private $count = 0; public function count() { return $this->count; } public function push($data) { ++$this->count; $this->stack[] = $data; } public function pop() { if ($this->count > 0) { --$this->count; return array_pop($this->stack); } return NULL; } function isEmpty() { return $this->count == 0; } }
  • 32. Stacks $a = new StandardArrayStack2(); for ($i = 1; $i <= $size; ++$i) { $a->push($i); } while (!$a->isEmpty()) { $i = $a->pop(); } PUSH 100,000 ENTRIES Push Time: 0.5699 s Current Memory: 8.75 POP 100,000 ENTRIES Pop Time: 1.1005 s Current Memory: 1.75 Total Time: 1.6713 s Current Memory: 1.75 Peak Memory: 8.75
  • 33. Stacks $a = new SplStack(); for ($i = 1; $i <= $size; ++$i) { $a->push($i); } while (!$a->isEmpty()) { $i = $a->pop(); } PUSH 100,000 ENTRIES Push Time: 0.4301 s Current Memory: 5.50 POP 100,000 ENTRIES Pop Time: 0.6413 s Current Memory: 0.75 Total Time: 1.0723 s Current Memory: 0.75 Peak Memory: 5.50
  • 34. Stacks 0.0796 0.0782 0.0644 0.1244 0.0998 0.0693 8.75 8.75 5.50 0 1 2 3 4 5 6 7 8 9 10 0.0000 0.0200 0.0400 0.0600 0.0800 0.1000 0.1200 0.1400 StandardArrayStack StandardArrayStack2 SPLStack Memory(MB) Time(seconds) Stack Timings Push Time (s) Pop Time (s) Memory after Push (MB)
  • 35. Stacks – Gotchas • Peek (view an entry from the middle of the stack) • StandardArrayStack public function peek($n = 0) { if ((count($this->stack) - $n) < 0) { return NULL; } return $this->stack[count($this->stack) - $n - 1]; } • StandardArrayStack2 public function peek($n = 0) { if (($this->count - $n) < 0) { return NULL; } return $this->stack[$this->count - $n - 1]; } • SplStack $r = $a->offsetGet($n);
  • 36. Stacks – Gotchas 0.0075 0.0077 0.0064 0.0111 0.0078 0.1627 0.0124 0.0098 0.0066 1.00 1.00 0.75 0.00 0.20 0.40 0.60 0.80 1.00 1.20 0.0000 0.0200 0.0400 0.0600 0.0800 0.1000 0.1200 0.1400 0.1600 0.1800 StandardArrayStack StandardArrayStack2 SPLStack Memory(MB) Time(seconds) Stack Timings Push Time (s) Peek Time (s) Pop Time (s) Memory after Push (MB)
  • 37. Stacks – Gotchas • Peek When looking through the stack, SplStack has to follow each link in the “chain” until it finds the nth entry O(n)
  • 38. SPL DataStructures Dictionary DataStructures (Maps) Linear DataStructures • Doubly-Linked Lists • Stacks • Queues Tree DataStructures
  • 40. Queues • Implemented as a Doubly-Linked List • FIFO • First-In • First-Out • Essential Operations • enqueue() • dequeue() • Optional Operations • count() • isEmpty() • peek()
  • 41. Queues – Uses • Job/print/message submissions • Breadth-First Search • Request handling (e.g. a Web server)
  • 42. Queues – Big-O Complexity • Enqueue an element O(1) • Dequeue an element O(1)
  • 43. Queues class StandardArrayQueue2 { private $queue = array(); private $count = 0; public function count() { return $this->count; } public function enqueue($data) { ++$this->count; $this->queue[] = $data; } public function dequeue() { if ($this->count > 0) { --$this->count; return array_shift($this->queue); } return NULL; } function isEmpty() { return $this->count == 0; } }
  • 44. Queues $a = new StandardArrayQueue2(); for ($i = 1; $i <= $size; ++$i) { $a->enqueue($i); } while (!$a->isEmpty()) { $i = $a->dequeue(); } ENQUEUE 100,000 ENTRIES Enqueue Time: 0.6884 Current Memory: 8.75 DEQUEUE 100,000 ENTRIES Dequeue Time: 335.8434 Current Memory: 1.75 Total Time: 336.5330 Current Memory: 1.75 Peak Memory: 8.75
  • 45. Queues $a = new SplQueue(); for ($i = 1; $i <= $size; ++$i) { $a->enqueue($i); } while (!$a->isEmpty()) { $i = $a->dequeue(); } ENQUEUE 100,000 ENTRIES Enqueue Time: 0.4087 Current Memory: 5.50 DEQUEUE 100,000 ENTRIES Dequeue Time: 0.6148 Current Memory: 0.75 Total Time: 1.0249 Current Memory: 0.75 Peak Memory: 5.50
  • 46. Queues 0.0075 0.0080 0.00640.0087 0.0070 0.1582 0.6284 0.6277 0.0066 1.00 1.00 0.75 0.00 0.20 0.40 0.60 0.80 1.00 1.20 0.0000 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 StandardArrayQueue StandardArrayQueue2 SPLQueue Memory(MB) Time(seconds) Queue Timings Enqueue Time (s) Peek Time (s) Dequeue Time (s) Memory after Enqueue (MB)
  • 47. Queues – Gotchas • Dequeue In standard PHP enumerated arrays, shift() and unshift() are expensive operations because they re-index the entire array This problem does not apply to SplQueue • Peek When looking through the queue, SplQueue has to follow each link in the “chain” until it finds the nth entry
  • 48. SPL DataStructures Dictionary DataStructures (Maps) Linear DataStructures Tree DataStructures • Heaps
  • 50. Heaps • Ordered Lists • Random Input • Ordered Output • Implemented as a binary tree structure • Essential Operations • Insert • Extract • Ordering Rule • Abstract that requires extending with the implementation of a compare() algorithm • compare() is reversed in comparison with usort compare callbacks • Partially sorted on data entry
  • 52. Heaps – Uses • Heap sort • Selection algorithms (e.g. Max, Min, Median) • Graph algorithms • Prim’s Minimal Spanning Tree (connected weighted undirected graph) • Dijkstra’s Shortest Path (network or traffic routing) • Priority Queues
  • 53. Heaps – Big-O Complexity • Insert an element O(log n) • Delete an element O(log n) • Access root element O(1)
  • 54. Heaps class DistanceSplHeap extends SplHeap { protected $longitude = 0.0; protected $latitude = 0.0; private $comparator; public function __construct(Callable $comparator, $longitude, $latitude) { $this->longitude = $longitude; $this->latitude = $latitude; $this->comparator = $comparator; } protected function compare($a, $b) { return call_user_func( $this->comparator, $a, $b ); } public function insert($value) { $value->distance = $this->calculateDistance($value); parent::insert($value); } } $comparator = function($a, $b) { if ($a->distance == $b->distance) return 0; return ($a->distance > $b->distance) ? -1 : 1; }; // Latitude and Longitude for Barcelona $speakersHeap = new DistanceSplHeap( $comparator, 2.1833, 41.3833 );
  • 55. Heaps $file = new SplFileObject("speakers.csv"); $file->setFlags( SplFileObject::DROP_NEW_LINE | SplFileObject::SKIP_EMPTY ); while (!$file->eof()) { $speakerData = $file->fgetcsv(); $speaker = new StdClass; $speaker->name = $speakerData[0]; $speaker->from = $speakerData[1]; $speaker->latitude = $speakerData[2]; $speaker->longitude = $speakerData[3]; $speakersHeap->insert($speaker); } "Anthony Ferrara","New Jersey, USA",40.0000,-74.5000 "Bastian Hofmann","Berlin, Germany",52.5167,13.3833 "Damien Seguy","The Hague, Netherlands",52.0833,4.3167 "Derick Rethans","London, UK",51.5072,-0.1275 "Juozas Kaziukėnas","New York, USA",40.7127,-74.0059 "Marcello Duarte","London, UK",51.5072,-0.1275 "Mark Baker","Wigan, UK",53.5448,-2.6318 "Mathias Verraes","Belgium",50.8500,4.3500 "Matthias Noback","Utrecht, Netherlands",52.0833,5.1167 "Nikita Popov","Berlin, Germany",52.5167,13.3833 "Paweł Jędrzejewski","ŁódĹş, Poland",51.7833,19.4667 "Steve Maraspin","Udine, Italy",46.0667,13.2333 "Tudor Barbu","Barcelona, Catalonia",41.3833,2.1833 "Zeev Suraski","Israel",31.0000,35.0000
  • 56. Heaps echo 'There are ', $speakersHeap->count(), ' speakers at PHPBarcelona 2015', PHP_EOL, PHP_EOL; echo 'Distance that they have travelled to reach Barcelona', PHP_EOL, PHP_EOL; foreach($speakersHeap as $speaker) { echo sprintf( "%-22s from %- 24s has travelled %6.1f miles".PHP_EOL, $speaker->name, $speaker->from, $speaker->distance ); } echo PHP_EOL; There are 14 speakers at PHPBarcelona 2015 Distance that they have travelled to reach Barcelona Tudor Barbu from Barcelona, Catalonia has travelled 0.0 miles Steve Maraspin from Udine, Italy has travelled 638.8 miles Mathias Verraes from Belgium has travelled 662.2 miles Derick Rethans from London, UK has travelled 708.0 miles Marcello Duarte from London, UK has travelled 708.0 miles Damien Seguy from The Hague, Netherlands has travelled 746.0 miles Matthias Noback from Utrecht, Netherlands has travelled 752.0 miles Mark Baker from Wigan, UK has travelled 869.3 miles Nikita Popov from Berlin, Germany has travelled 930.8 miles Bastian Hofmann from Berlin, Germany has travelled 930.8 miles Paweł Jędrzejewski from ŁódĹş, Poland has travelled 1085.9 miles Zeev Suraski from Israel has travelled 1951.0 miles Juozas Kaziukėnas from New York, USA has travelled 3831.8 miles Anthony Ferrara from New Jersey, USA has travelled 3877.9 miles
  • 57. Heaps – Gotchas • Compare method is reversed logic from a usort() callback • Traversing the heap removes elements from the heap
  • 58. SPL – Standard PHP Library Other SPL Datastructures • SplMaxHeap • SplMinHeap • SplPriorityQueue • SplObjectStorage
  • 59. SPL – Standard PHP Library E-Book Mastering the SPL Library Joshua Thijssen Available in PDF, ePub, Mobi http://www.phparch.com/books/mastering-the-spl-library/
  • 60. SPL – Standard PHP Library E-Book Iterating PHP Iterators Cal Evans Available in PDF, ePub, Mobi https://leanpub.com/iteratingphpiterators
  • 62. Who am I? Mark Baker Design and Development Manager InnovEd (Innovative Solutions for Education) Ltd Coordinator and Developer of: Open Source PHPOffice library PHPExcel, PHPWord, PHPPowerPoint, PHPProject, PHPVisio Minor contributor to PHP core @Mark_Baker https://github.com/MarkBaker http://uk.linkedin.com/pub/mark-baker/b/572/171
  • 63. SPL: The Undiscovered Library – Exploring Datastructures http://joind.in/talk/view/15874