New SPL Features in PHP 5.3 (TEK-X)

10,958 views

Published on

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

Published in: Technology
3 Comments
9 Likes
Statistics
Notes
  • That’s great... however, SplQueue is compared against array_shift which is known to be hideously slow as it renumbers the array. Keep a counter, grab the element at the counter and unset it and increment. Bench that, it will be a lot faster.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • This was a great, fast-paced, and visual overview of the new SPL classes. The WD-40 and Duct Tape image was inspired (and perfectly captures the array & string mentality). The performance graphs made a might compelling visual case. Thanks for making this available.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Great slideshow, thank you
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
10,958
On SlideShare
0
From Embeds
0
Number of Embeds
741
Actions
Shares
0
Downloads
176
Comments
3
Likes
9
Embeds 0
No embeds

No notes for slide

New SPL Features in PHP 5.3 (TEK-X)

  1. 1. New SPL Features in PHP 5.3 Matthew Turland CodeWorks '09 Webcast Series June 26, 2009
  2. 2. 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
  3. 3. A Long, Long Time Ago (Or Not) ● Alexander Stepanov ● Conceived of the STL for C++ ● Goals of SPL are somewhat similar
  4. 4. In a Galaxy Not So Far Away Pre-5.3 SPL Features ● Classes: ArrayObject, SplFileInfo... ● Interfaces: ArrayAccess, Countable... ● Exceptions: BadFunctionCallException... ● Functions: spl_autoload_register...
  5. 5. Oh yeah, and iterators.
  6. 6. So What's New? In comparison to the STL: ● Iterators? Nope. ● Algorithms? Nope. ● Functors? Nope. ● Well, there's only one thing left then...
  7. 7. 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
  8. 8. We Don't Need No Stinkin' Containers! array() 'string'
  9. 9. Sure We Do! Here's Why... Scalability!!!
  10. 10. Arrays Are Great ● They're a general purpose container. ● This makes them flexible. ● Its underlying algorithm isn't always best.
  11. 11. Benchmarks ● Lies, Damned Lies, and Benchmarks - YMMV ● PHP 5.3.0RC4 compiled on Ubuntu 9.04 ● Intel Core2Duo 1.83GHz, 4 GB DDR2-RAM
  12. 12. Benchmark Runner #!/bin/bash # 20 = # executions to perform # 100 = # elements in the container # ./bench.sh test.php 20 100 time=`/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";
  13. 13. The List
  14. 14. 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.
  15. 15. 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; }
  16. 16. 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
  17. 17. SplFixedArray Graph 90000 80000 70000 60000 Time (µs) 50000 40000 SplFixedArray 30000 Array 20000 10000 0 10 100 1000 10000 100000 Elements
  18. 18. The Stack
  19. 19. SplStack ● Last In, First Out (LIFO) ● 2 Operations ● Push - [] for both ● Pop – array_pop() vs ->pop()
  20. 20. SplStack Code <?php $a = array(); for($i = 0; $i < $argv[1]; $i++) { $a[] = $i; } for($i = 0; $i < $argv[1]; $i++) { array_pop($a); }
  21. 21. 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(); }
  22. 22. 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
  23. 23. SplStack Graph 160000 140000 120000 100000 Time (μs) 80000 SplStack 60000 Array 40000 20000 0 10 100 1000 10000 100000 Elements
  24. 24. The Queue
  25. 25. SplQueue ● First In, First Out (FIFO) ● 2 Operations ● Enqueue - [] for both ● Dequeue – array_shift() vs ->dequeue()
  26. 26. SplQueue Code <?php $a = array(); for($i = 0; $i < $argv[1]; $i++) { $a[] = $i; } for($i = 0; $i < $argv[1]; $i++) { array_shift($a); }
  27. 27. 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(); }
  28. 28. 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
  29. 29. SplQueue Graph 35000000 30000000 25000000 Time (μs) 20000000 15000000 SplQueue 10000000 Array 5000000 0 100 10000 10 1000 100000 Elements
  30. 30. The Heap
  31. 31. SplHeap, SplMinHeap, SplMaxHeap ● Highest / Lowest First Out ● 2 Operations ● Insert - [] and sort() vs ->insert() ● Remove – array_shift() vs ->extract()
  32. 32. 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); }
  33. 33. 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(); }
  34. 34. 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
  35. 35. SplMinHeap Graph 35000000 30000000 25000000 Time (μs) 20000000 SplMinHeap 15000000 Array 10000000 5000000 0 100 10000 10 1000 100000 Elements
  36. 36. The Priority Queue
  37. 37. 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
  38. 38. SplPriorityQueue Code <?php function 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); } }
  39. 39. 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(); } }
  40. 40. 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
  41. 41. SplPriorityQueue Graph 35000000 30000000 25000000 Time (μs) 20000000 SplPriorityQueue 15000000 Array 10000000 5000000 0 10 100 1000 10000 100000 Elements
  42. 42. By the way, thank this guy Etienne Kneuss
  43. 43. 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
  44. 44. 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

×