{"id":17302,"date":"2012-12-11T12:07:02","date_gmt":"2012-12-11T10:07:02","guid":{"rendered":"https:\/\/mamchenkov.net\/wordpress\/?p=17302"},"modified":"2012-12-11T12:07:02","modified_gmt":"2012-12-11T10:07:02","slug":"spl-standard-php-library","status":"publish","type":"post","link":"https:\/\/mamchenkov.net\/wordpress\/2012\/12\/11\/spl-standard-php-library\/","title":{"rendered":"SPL &#8211; Standard PHP Library"},"content":{"rendered":"<!-- google_ad_section_start -->\n<p>I&#8217;ve been looking at <a href=\"http:\/\/php.net\/manual\/en\/book.spl.php\">SPL<\/a> for some time now. \u00a0On one hand, it&#8217;s a new addition to PHP core (since version 5.3), so I know how to work without it. \u00a0On the other hand, it provides standardized solutions for common problems, and that should be enough reason to start using it. \u00a0However, today I came across <a href=\"http:\/\/nikic.github.com\/2011\/12\/12\/How-big-are-PHP-arrays-really-Hint-BIG.html\">an interesting article<\/a> that provides even more reason to use SPL.<\/p>\n<blockquote><p>In this post I want to investigate the memory usage of PHP arrays (and values in general) using the following script as an example, which creates 100000 unique integer array elements and measures the resulting memory usage:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$startMemory = memory_get_usage();\r\n$array = range(1, 100000);\r\necho memory_get_usage() - $startMemory, ' bytes';\r\n<\/pre>\n<p>How much would you expect it to be? Simple, one integer is 8 bytes (on a 64 bit unix machine and using the long type) and you got 100000 integers, so you obviously will need 800000 bytes. That\u2019s something like 0.76 MBs.<\/p>\n<p>Now try and run the above code. You can do it online if you want. This gives me 14649024 bytes. Yes, you heard right, that\u2019s 13.97 MB &#8211; eightteen times more than we estimated.<\/p>\n<p>[&#8230;]<\/p>\n<p>But if you do want to save memory you could consider using an SplFixedArray for large, static arrays.<\/p>\n<p>Have a look a this modified script:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$startMemory = memory_get_usage();\r\n$array = new SplFixedArray(100000);\r\nfor ($i = 0; $i &lt; 100000; ++$i) {\r\n    $array&#x5B;$i] = $i;\r\n}\r\necho memory_get_usage() - $startMemory, ' bytes';\r\n<\/pre>\n<p>It basically does the same thing, but if you run it, you\u2019ll notice that it uses \u201conly\u201d 5600640 bytes. That\u2019s 56 bytes per element and thus much less than the 144 bytes per element a normal array uses. This is because a fixed array doesn\u2019t need the bucket structure: So it only requires one zval (48 bytes) and one pointer (8 bytes) for each element, giving us the observed 56 bytes.<\/p><\/blockquote>\n<p>For years I&#8217;ve been suffering from PHP&#8217;s memory hunger. I&#8217;ve had to optimize the code around smaller memory footprints, unset variables, and do all sorts of other messy things, that I normally wouldn&#8217;t have in a high-level programming language like PHP. With SPL, it seems, there is more help on the horizon.<\/p>\n<!-- google_ad_section_end -->\n","protected":false},"excerpt":{"rendered":"<!-- google_ad_section_start -->\n<p>I&#8217;ve been looking at SPL for some time now. \u00a0On one hand, it&#8217;s a new addition to PHP core (since version 5.3), so I know how to work without it. \u00a0On the other hand, it provides standardized solutions for common problems, and that should be enough reason to start using it. \u00a0However, today I came &hellip; <a href=\"https:\/\/mamchenkov.net\/wordpress\/2012\/12\/11\/spl-standard-php-library\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">SPL &#8211; Standard PHP Library<\/span><\/a><\/p>\n<!-- google_ad_section_end -->\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_links_to":"","_links_to_target":""},"categories":[1,18,62,1334],"tags":[3136,246,1057,38],"keyring_services":[],"class_list":["post-17302","post","type-post","status-publish","format-standard","hentry","category-general","category-programming","category-technology","category-web-work","tag-design-patterns","tag-optimization","tag-performance","tag-php"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":12284,"url":"https:\/\/mamchenkov.net\/wordpress\/2010\/03\/20\/day-in-brief\/","url_meta":{"origin":17302,"position":0},"title":"Day in brief","author":"Leonid Mamchenkov","date":"March 20, 2010","format":false,"excerpt":"I've heard about Standard PHP Library (SPL) a billion times. Always wanted to use it for something. Today I finally did. Awesome! # I favorited a YouTube video -- F**king Matt Damon & Ben Affleck! TOO FUNNY!! http:\/\/youtu.be\/5l9BcreGd-M?a # I favorited a YouTube video -- Letterman - George Clooney's Boyfriend\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":14892,"url":"https:\/\/mamchenkov.net\/wordpress\/2011\/05\/17\/partial-string-replacement-with-fixed-length-in-php\/","url_meta":{"origin":17302,"position":1},"title":"Partial string replacement with fixed length in PHP","author":"Leonid Mamchenkov","date":"May 17, 2011","format":false,"excerpt":"Today I came across one of those problems that look simple and straight forward at first, but then somehow consume a good part of an hour to solve. \u00a0I wanted to replace a part of the string (in the middle), with given character, without changing the length of the string\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27612,"url":"https:\/\/mamchenkov.net\/wordpress\/2017\/05\/23\/php-moving-from-array-to-class\/","url_meta":{"origin":17302,"position":2},"title":"PHP : Moving from array to class","author":"Leonid Mamchenkov","date":"May 23, 2017","format":false,"excerpt":"\"Moving from array to class\" is yet another thought-provoking take on the difference between arrays and classes in the modern versions of PHP. \u00a0The benefits of moving from arrays to classes seem to be not only in the code readability and maintainability, but quite clearly in performance and resource utilization\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":12148,"url":"https:\/\/mamchenkov.net\/wordpress\/2010\/02\/17\/cakephp-building-factories-with-models-and-behaviors\/","url_meta":{"origin":17302,"position":3},"title":"CakePHP : Building factories with models and behaviors","author":"Leonid Mamchenkov","date":"February 17, 2010","format":false,"excerpt":"CakePHP is a wonderful framework. \u00a0 Recently I proved it to myself once again (not that I need much of that proof anyway). \u00a0The problem that we had at work was a whole lot of code in once place and no obvious way of how to break that code into\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27408,"url":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/11\/validating-json-against-schema-in-php\/","url_meta":{"origin":17302,"position":4},"title":"Validating JSON against schema in PHP","author":"Leonid Mamchenkov","date":"March 11, 2017","format":false,"excerpt":"GitHub was rather slow yesterday, which affected the speed of installing composer dependencies (since most of them are hosted on GitHub anyway). \u00a0Staring at a slowly scrolling list of installed dependencies, I noticed something interesting. [code light=\"true\"] ... - Installing seld\/jsonlint (1.6.0) - Installing justinrainbow\/json-schema (5.1.0) ... [\/code] Of course,\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27320,"url":"https:\/\/mamchenkov.net\/wordpress\/2017\/02\/10\/php-assertions\/","url_meta":{"origin":17302,"position":5},"title":"PHP assertions","author":"Leonid Mamchenkov","date":"February 10, 2017","format":false,"excerpt":"When I hear the word \"assertion\", the first thing that comes to my mind is PHPUnit assertions. \u00a0Sure, I write assertions in my unit tests. \u00a0But is that the only application? \u00a0Today I decided to figure it out, or, at least, learn more about the subject. It turns out that\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/17302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/comments?post=17302"}],"version-history":[{"count":0,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/17302\/revisions"}],"wp:attachment":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/media?parent=17302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/categories?post=17302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/tags?post=17302"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/keyring_services?post=17302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}