5 ms·
As the author mentions, if you run into a use-case where you need to store 100000 integers in memory, then you should use one of the many alternative structures
by maratd 15y ago
As the author mentions, if you run into a use-case where you need to store 100000 integers in memory, then you should use one of the many alternative structures available. Some of them were explicitly designed to store integers in an efficient manner. Arrays weren't designed to store integers efficiently or anything else for that matter. They were designed to be fast and easy to use.
- mbell 15y agoI wouldn't call using 56 bytes to store 8 bytes of actual data efficient.
- dangrossman 15y agoThat'd be why he said "arrays weren't designed to store integers efficiently".
- ktr 15y agoFrom the comment: > then you should use one of the many alternative structures available. Some of them were explicitly designed to store integers in an efficient manner. From the article: > But if you do want to save memory you could consider using an SplFixedArray for large, static arrays. ... It basically does the same thing, but if you run it, you’ll notice that it uses “only” 5600640 bytes. That’s 56 bytes per element ... EDIT: formatting.
- maratd 15y agoThat is only one of the alternatives and in my opinion, not a very good one. I forget the exact details, but there is an extension by the guy who wrote igbinary that is specifically designed for this use-case.
- skeptical 15y agoConsidering PHP's scope and its limitations (lacks things like threads for example) I would say that is an extremely rare scenario. If you're storing 100.000 integers on a PHP data structure you're most likely doing it wrong.
- maratd 15y ago> Considering PHP's scope and its limitations (lacks things like threads for example) Please look up PHP-FPM. I use it in production, with great success. > If you're storing 100.000 integers on a PHP data structure you're most likely doing it wrong. What if I had a ton of price points and I needed to do statistical analysis? Well, those would be floats, but you get my point. PHP is capable of doing the work efficiently, you just need to put a little thought in it first or you'll very quickly trash your box.
- epochwolf 15y agoIsn't PHP-FPM a fastcgi process manager? I don't see any references to threading in it's documentation. I also recall the php documentation saying php is unsafe with multiple threads due to a large number of libraries that are not coded to be thread-safe.
- maratd 15y ago> Isn't PHP-FPM a fastcgi process manager It is. > I don't see any references to threading Wow. I always thought it was threaded. My mistake. It seems it launches multiple child processes. There is some form of memory sharing going on, though. Looks like it's some sort of a hybrid. Now I'm just thoroughly confused ...
- Gigablah 15y agoYou can take a look at php-pcntl. http://www.php.net/manual/en/book.pcntl.php http://www.php.net/manual/en/book.pcntl.php http://www.php.net/manual/en/function.pcntl-fork.php http://www.php.net/manual/en/function.pcntl-fork.php
- skeptical 15y agoHow would you evoke such code? Squeezing it into an http reply? Calling it from PHP CLI? If doing it on your webapp, don't! You're doing it wrong. Otherwise why would you need php specifically? I mean I guess you could, but what advantage does it poses comparing to other languages available in pretty much every *nix system nowadays (perl, python)? For the record, I have a nice collection of downvotes for defending PHP on point-and-laugh-at-PHP threads here at HN. I just don't see how the use case you're presnting is realistic.