SlideShare a Scribd company logo
SPL Data Structures and their Complexity
                 Jurri¨n Stutterheim
                      e
                  September 17, 2011




1
1. Introduction




2
This presentation                                 §1




         Understand what data structures are
         How they are represented internally
         How “fast” each one is and why that is




3
Data structures                                                      §1




         Classes that offer the means to store and retrieve data,
         possibly in a particular order
         Implementation is (often) optimised for certain use cases
         array is PHP’s oldest and most frequently used data
         structure
         PHP 5.3 adds support for several others




4
Current SPL data structures    §1




         SplDoublyLinkedList
         SplStack
         SplQueue
         SplHeap
         SplMaxHeap
         SplMinHeap
         SplPriorityQueue
         SplFixedArray
         SplObjectStorage




5
Why care?                                                         §1




        Using the right data structure in the right place could
        improve performance
        Already implemented and tested: saves work
        Can add a type hint in a function definition
        Adds semantics to your code




6
Algorithmic complexity                                             §1




         We want to be able to talk about the performance of the
         data structure implementation
             Running speed (time complexity)
             Space consumption (space complexity)
         We describe complexity in terms of input size, which is
         machine and programming language independent




7
Example                                                          §1




      for ($i = 0; $i < $n; $i++) {
        for ($j = 0; $j < $n; $j++) {
          echo ’tick’;
        }
      }

     For some n, how many times is “tick” printed? I.e. what is the
     time complexity of this algorithm?




8
Example                                                          §1




      for ($i = 0; $i < $n; $i++) {
        for ($j = 0; $j < $n; $j++) {
          echo ’tick’;
        }
      }

     For some n, how many times is “tick” printed? I.e. what is the
     time complexity of this algorithm?
     n2 times




8
Talking about complexity                                                §1




         Pick a function to act as boundary for the algorithm’s
         complexity
         Worst-case
             Denoted O (big-Oh)
             “My algorithm will not be slower than this function”
         Best-case
             Denoted Ω (big-Omega)
             “My algorithm will at least be as slow as this function”
         If they are the same, we write Θ (big-Theta)
         In example: both cases are n2 , so the algorithm is in Θ(n2 )




9
Visualized   §1




10
Example 2                                             §1



      for ($i = 0; $i < $n; $i++) {
        if ($myBool) {
          for ($j = 0; $j < $n; $j++) {
            echo ’tick’;
          }
        }
      }

     What is the time complexity of this algorithm?




11
Example 2                                             §1



      for ($i = 0; $i < $n; $i++) {
        if ($myBool) {
          for ($j = 0; $j < $n; $j++) {
            echo ’tick’;
          }
        }
      }

     What is the time complexity of this algorithm?

         O(n2 )
         Ω(n) (if $myBool is false)
         No Θ!

11
We can be a bit sloppy                                              §1



      for ($i = 0; $i < $n; $i++) {
        if ($myBool) {
          for ($j = 0; $j < $n; $j++) {
            echo ’tick’;
          }
        }
      }


         We describe algorithmic behaviour as input size grows to
         infinity
             constant factors and smaller terms don’t matter too much
         E.g. 3n2 + 4n + 1 is in O(n2 )


12
Other functions                            §1




       for ($i = 0; $i < $n; $i++) {
         for ($j = 0; $j < $n; $j++) {
           echo ’tick’;
         }
       }

       for ($i = 0; $i < $n; $i++) {
         echo ’tock’;
       }

      This algorithm is still in Θ(n2 ).



13
Bounds                                                    §1




                               Figure: Order relations1




         1
14           Taken from Cormen et al. 2009
Complexity Comparison                                                                 §1

           3
      10
                                        Superexponential   Factorial   Exponential




                                                                          Quadratic
      102




                                                                              Linear
           1
      10




                                                                         Logarithmic
       100




                                                                                     101
     Constant: 1, logarithmic: lg n, linear: n, quadratic: n2 ,
     exponential: 2n , factorial: n!, super-exponential: nn

15
In numbers                            §1



     Approximate growth for n = 50:

               1 1
             lg n 5.64
               n 50
              n2 2500
              n3 12500
              2n 1125899906842620
              n! 3.04 ∗ 1064
              nn 8.88 ∗ 1084



16
Some more notes on complexity                                     §1




        Constant time is written 1, but goes for any constant c
        Polynomial time contains all functions in nc for some
        constant c
        Everything in this presentation will be in polynomial time




17
2. SPL Data Structures




18
Credit where credit is due                                         §2




      The first three pictures in this section are from Wikipedia




19
SplDoublyLinkedList                                                  §2



                 12                99                37


         Superclass of SplStack and SplQueue
         SplDoublyLinkedList is not truly a doubly linked list; it
         behaves like a hashtable




20
SplDoublyLinkedList                                                  §2



                 12                99                37


         Superclass of SplStack and SplQueue
         SplDoublyLinkedList is not truly a doubly linked list; it
         behaves like a hashtable
         Usual doubly linked list time complexity
             Append/prepend to available node in Θ(1)
             Lookup by scanning in O(n)
             Access to beginning/end in Θ(1)




20
SplDoublyLinkedList                                                  §2



                 12                99                37


         Superclass of SplStack and SplQueue
         SplDoublyLinkedList is not truly a doubly linked list; it
         behaves like a hashtable
         Usual doubly linked list time complexity
             Append/prepend to available node in Θ(1)
             Lookup by scanning in O(n)
             Access to beginning/end in Θ(1)
         SplDoublyLinkedList time complexity
             Insert/delete by index in Θ(1)
             Lookup by index in Θ(1)
             Access to beginning/end in Θ(1)

20
SplStack                                                         §2


           Subclass of SplDoublyLinkedList; adds no new operations
           Last-in, first-out (LIFO)
           Pop/push value from/on the top of the stack in Θ(1)




                           Push          Pop




21
SplQueue                                                           §2

           Subclass of SplDoublyLinkedList; adds enqueue/dequeue
           operations
           First-in, first-out (FIFO)
           Read/dequeue element from front in Θ(1)
           Enqueue element to the end in Θ(1)



                         Enqueue




                                       Dequeue


22
Short excursion: trees                                        §2


                                             100




                                        19         36




                               17       3          25   1




                           2        7




         Consists of nodes (vertices) and directed edges
         Each node always has in-degree 1
             Except the root: always in-degree 0
         Previous property implies there are no cycles
         Binary tree: each node has at most two child-nodes
23
SplHeap, SplMaxHeap and SplMinHeap                                §2


                                            100




                                       19         36




                              17       3          25   1




                          2        7




        A heap is a tree with the heap property : for all A and B, if
        B is a child node of A, then
            val(A)    val(B) for a max-heap: SplMaxHeap
            val(A)    val(B) for a min-heap: SplMinHeap
        Where val(A) denotes the value of node A
24
Heaps contd.                                §2




        SplHeap is an abstract superclass
        Implemented as binary tree
        Access to root element in Θ(1)
        Insertion/deletion in O(lg n)




25
SplPriorityQueue                                                   §2




         Variant of SplMaxHeap: for all A and B, if B is a child
         node of A, then prio(A) prio(B)
         Where prio(A) denotes the priority of node A




26
SplFixedArray                                           §2




         Fixed-size array with numerical indices only
         Efficient OO array implementation
             No hashing required for keys
             Can make assumptions about array size
         Lookup, insertion, deletion in Θ(1) time
         Resize in Θ(n)




27
SplObjectStorage                                    §2




         Storage container for objects
         Insertion, deletion in Θ(1)
         Verification of presence in Θ(1)
         Missing: set operations
             Union, intersection, difference, etc.




28
3. Concluding




29
Missing in PHP                                                     §3




        Set data structure
        Map/hashtable data structure
            Does SplDoublyLinkedList satisfy this use case?
            If yes: split it in two separate structures and make
            SplDoublyLinkedList a true doubly linked list
        Immutable data structures
            Allows us to more easily emulate “pure” functions
            Less bugs in your code due to lack of mutable state




30
Closing remarks                                §3




         Use the SPL data structures!
         Choose them with care
         Reason about your code’s complexity




31
Questions         §3




     Questions?




32

More Related Content

PDF
Intro To Erlang
PDF
Logic programming a ruby perspective
PPT
9 cm604.12
PDF
Why we cannot ignore Functional Programming
PPTX
Meta Object Protocols
PDF
FP in Java - Project Lambda and beyond
PPT
9 cm604.12
PDF
Lazy java
Intro To Erlang
Logic programming a ruby perspective
9 cm604.12
Why we cannot ignore Functional Programming
Meta Object Protocols
FP in Java - Project Lambda and beyond
9 cm604.12
Lazy java

What's hot (20)

PPTX
Java 7, 8 & 9 - Moving the language forward
PPT
Knowledge engg using & in fol
PDF
The TclQuadcode Compiler
PDF
Laziness, trampolines, monoids and other functional amenities: this is not yo...
PPTX
Structure and interpretation of computer programs modularity, objects, and ...
PDF
Comparing different concurrency models on the JVM
PDF
From object oriented to functional domain modeling
PDF
If You Think You Can Stay Away from Functional Programming, You Are Wrong
PPTX
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
PDF
OOP and FP - Become a Better Programmer
PPTX
Java fundamentals
PDF
Monadic Java
PDF
Object Design - Part 1
PPT
Computer Programming- Lecture 4
PDF
recursion2
PPT
Computer Programming- Lecture 9
PDF
Java q ref 2018
PPT
Lecture 12: Classes and Files
PDF
Pydiomatic
PPTX
Pj01 4-operators and control flow
Java 7, 8 & 9 - Moving the language forward
Knowledge engg using & in fol
The TclQuadcode Compiler
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Structure and interpretation of computer programs modularity, objects, and ...
Comparing different concurrency models on the JVM
From object oriented to functional domain modeling
If You Think You Can Stay Away from Functional Programming, You Are Wrong
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
OOP and FP - Become a Better Programmer
Java fundamentals
Monadic Java
Object Design - Part 1
Computer Programming- Lecture 4
recursion2
Computer Programming- Lecture 9
Java q ref 2018
Lecture 12: Classes and Files
Pydiomatic
Pj01 4-operators and control flow
Ad

Similar to Pf congres20110917 data-structures (20)

PDF
lec4_annotated.pdf ml csci 567 vatsal sharan
PPT
19 algorithms-and-complexity-110627100203-phpapp02
PPTX
Complexity analysis - The Big O Notation
PDF
Introduction to Erlang
PDF
Fork Join (BeJUG 2012)
PPTX
Insertion sort and shell sort
PDF
Functional Concepts for OOP Developers
PDF
CODEsign 2015
DOCX
Basic Computer Engineering Unit II as per RGPV Syllabus
PPTX
AA_Unit 1_part-I.pptx
PDF
GPUVerify - Implementation
PDF
Sienna 2 analysis
PDF
6_12_13Asymptotic analysiskfnhlkjsbfbkjs.pdf
PPT
Stacksqueueslists
PPT
Stacks queues lists
PPT
Stacks queues lists
PPT
Stacks queues lists
PPT
Stack squeues lists
PPT
Stacks queues lists
ODP
Clojure
lec4_annotated.pdf ml csci 567 vatsal sharan
19 algorithms-and-complexity-110627100203-phpapp02
Complexity analysis - The Big O Notation
Introduction to Erlang
Fork Join (BeJUG 2012)
Insertion sort and shell sort
Functional Concepts for OOP Developers
CODEsign 2015
Basic Computer Engineering Unit II as per RGPV Syllabus
AA_Unit 1_part-I.pptx
GPUVerify - Implementation
Sienna 2 analysis
6_12_13Asymptotic analysiskfnhlkjsbfbkjs.pdf
Stacksqueueslists
Stacks queues lists
Stacks queues lists
Stacks queues lists
Stack squeues lists
Stacks queues lists
Clojure
Ad

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Cloud computing and distributed systems.
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
cuic standard and advanced reporting.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Electronic commerce courselecture one. Pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
Unlocking AI with Model Context Protocol (MCP)
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Programs and apps: productivity, graphics, security and other tools
Cloud computing and distributed systems.
Dropbox Q2 2025 Financial Results & Investor Presentation
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
cuic standard and advanced reporting.pdf
Network Security Unit 5.pdf for BCA BBA.
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Encapsulation_ Review paper, used for researhc scholars
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Digital-Transformation-Roadmap-for-Companies.pptx
Understanding_Digital_Forensics_Presentation.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Reach Out and Touch Someone: Haptics and Empathic Computing
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Electronic commerce courselecture one. Pdf
NewMind AI Weekly Chronicles - August'25 Week I

Pf congres20110917 data-structures

  • 1. SPL Data Structures and their Complexity Jurri¨n Stutterheim e September 17, 2011 1
  • 3. This presentation §1 Understand what data structures are How they are represented internally How “fast” each one is and why that is 3
  • 4. Data structures §1 Classes that offer the means to store and retrieve data, possibly in a particular order Implementation is (often) optimised for certain use cases array is PHP’s oldest and most frequently used data structure PHP 5.3 adds support for several others 4
  • 5. Current SPL data structures §1 SplDoublyLinkedList SplStack SplQueue SplHeap SplMaxHeap SplMinHeap SplPriorityQueue SplFixedArray SplObjectStorage 5
  • 6. Why care? §1 Using the right data structure in the right place could improve performance Already implemented and tested: saves work Can add a type hint in a function definition Adds semantics to your code 6
  • 7. Algorithmic complexity §1 We want to be able to talk about the performance of the data structure implementation Running speed (time complexity) Space consumption (space complexity) We describe complexity in terms of input size, which is machine and programming language independent 7
  • 8. Example §1 for ($i = 0; $i < $n; $i++) { for ($j = 0; $j < $n; $j++) { echo ’tick’; } } For some n, how many times is “tick” printed? I.e. what is the time complexity of this algorithm? 8
  • 9. Example §1 for ($i = 0; $i < $n; $i++) { for ($j = 0; $j < $n; $j++) { echo ’tick’; } } For some n, how many times is “tick” printed? I.e. what is the time complexity of this algorithm? n2 times 8
  • 10. Talking about complexity §1 Pick a function to act as boundary for the algorithm’s complexity Worst-case Denoted O (big-Oh) “My algorithm will not be slower than this function” Best-case Denoted Ω (big-Omega) “My algorithm will at least be as slow as this function” If they are the same, we write Θ (big-Theta) In example: both cases are n2 , so the algorithm is in Θ(n2 ) 9
  • 11. Visualized §1 10
  • 12. Example 2 §1 for ($i = 0; $i < $n; $i++) { if ($myBool) { for ($j = 0; $j < $n; $j++) { echo ’tick’; } } } What is the time complexity of this algorithm? 11
  • 13. Example 2 §1 for ($i = 0; $i < $n; $i++) { if ($myBool) { for ($j = 0; $j < $n; $j++) { echo ’tick’; } } } What is the time complexity of this algorithm? O(n2 ) Ω(n) (if $myBool is false) No Θ! 11
  • 14. We can be a bit sloppy §1 for ($i = 0; $i < $n; $i++) { if ($myBool) { for ($j = 0; $j < $n; $j++) { echo ’tick’; } } } We describe algorithmic behaviour as input size grows to infinity constant factors and smaller terms don’t matter too much E.g. 3n2 + 4n + 1 is in O(n2 ) 12
  • 15. Other functions §1 for ($i = 0; $i < $n; $i++) { for ($j = 0; $j < $n; $j++) { echo ’tick’; } } for ($i = 0; $i < $n; $i++) { echo ’tock’; } This algorithm is still in Θ(n2 ). 13
  • 16. Bounds §1 Figure: Order relations1 1 14 Taken from Cormen et al. 2009
  • 17. Complexity Comparison §1 3 10 Superexponential Factorial Exponential Quadratic 102 Linear 1 10 Logarithmic 100 101 Constant: 1, logarithmic: lg n, linear: n, quadratic: n2 , exponential: 2n , factorial: n!, super-exponential: nn 15
  • 18. In numbers §1 Approximate growth for n = 50: 1 1 lg n 5.64 n 50 n2 2500 n3 12500 2n 1125899906842620 n! 3.04 ∗ 1064 nn 8.88 ∗ 1084 16
  • 19. Some more notes on complexity §1 Constant time is written 1, but goes for any constant c Polynomial time contains all functions in nc for some constant c Everything in this presentation will be in polynomial time 17
  • 20. 2. SPL Data Structures 18
  • 21. Credit where credit is due §2 The first three pictures in this section are from Wikipedia 19
  • 22. SplDoublyLinkedList §2 12 99 37 Superclass of SplStack and SplQueue SplDoublyLinkedList is not truly a doubly linked list; it behaves like a hashtable 20
  • 23. SplDoublyLinkedList §2 12 99 37 Superclass of SplStack and SplQueue SplDoublyLinkedList is not truly a doubly linked list; it behaves like a hashtable Usual doubly linked list time complexity Append/prepend to available node in Θ(1) Lookup by scanning in O(n) Access to beginning/end in Θ(1) 20
  • 24. SplDoublyLinkedList §2 12 99 37 Superclass of SplStack and SplQueue SplDoublyLinkedList is not truly a doubly linked list; it behaves like a hashtable Usual doubly linked list time complexity Append/prepend to available node in Θ(1) Lookup by scanning in O(n) Access to beginning/end in Θ(1) SplDoublyLinkedList time complexity Insert/delete by index in Θ(1) Lookup by index in Θ(1) Access to beginning/end in Θ(1) 20
  • 25. SplStack §2 Subclass of SplDoublyLinkedList; adds no new operations Last-in, first-out (LIFO) Pop/push value from/on the top of the stack in Θ(1) Push Pop 21
  • 26. SplQueue §2 Subclass of SplDoublyLinkedList; adds enqueue/dequeue operations First-in, first-out (FIFO) Read/dequeue element from front in Θ(1) Enqueue element to the end in Θ(1) Enqueue Dequeue 22
  • 27. Short excursion: trees §2 100 19 36 17 3 25 1 2 7 Consists of nodes (vertices) and directed edges Each node always has in-degree 1 Except the root: always in-degree 0 Previous property implies there are no cycles Binary tree: each node has at most two child-nodes 23
  • 28. SplHeap, SplMaxHeap and SplMinHeap §2 100 19 36 17 3 25 1 2 7 A heap is a tree with the heap property : for all A and B, if B is a child node of A, then val(A) val(B) for a max-heap: SplMaxHeap val(A) val(B) for a min-heap: SplMinHeap Where val(A) denotes the value of node A 24
  • 29. Heaps contd. §2 SplHeap is an abstract superclass Implemented as binary tree Access to root element in Θ(1) Insertion/deletion in O(lg n) 25
  • 30. SplPriorityQueue §2 Variant of SplMaxHeap: for all A and B, if B is a child node of A, then prio(A) prio(B) Where prio(A) denotes the priority of node A 26
  • 31. SplFixedArray §2 Fixed-size array with numerical indices only Efficient OO array implementation No hashing required for keys Can make assumptions about array size Lookup, insertion, deletion in Θ(1) time Resize in Θ(n) 27
  • 32. SplObjectStorage §2 Storage container for objects Insertion, deletion in Θ(1) Verification of presence in Θ(1) Missing: set operations Union, intersection, difference, etc. 28
  • 34. Missing in PHP §3 Set data structure Map/hashtable data structure Does SplDoublyLinkedList satisfy this use case? If yes: split it in two separate structures and make SplDoublyLinkedList a true doubly linked list Immutable data structures Allows us to more easily emulate “pure” functions Less bugs in your code due to lack of mutable state 30
  • 35. Closing remarks §3 Use the SPL data structures! Choose them with care Reason about your code’s complexity 31
  • 36. Questions §3 Questions? 32