Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1062745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:40:28+00:00 2026-05-16T18:40:28+00:00

Background: I have a large 2D array of integers that I need to load

  • 0

Background: I have a large 2D array of integers that I need to load into memory in PHP for each Apache request. I want it to take up less memory.

PHP stores ints in PHP_INT_SIZE bytes, which is 32 bits on most systems. All the integers are less than 2^16, which means they could be a short int (eg in C). Am I right in thinking that storing ints as short would take up half the RAM?

Ideally I’d like to be able to do:

$s = (short) 1234; // takes up 2 bytes instead of 4

More info:

  • The array takes up about 100mb of RAM and is generated by including a 30MB var_export() dump
  • The array is written in a cron process. Only the reading needs to be memory efficient (and quick)
  • The only operations I need to do on the integers are comparing all of them (<, >, ===) and then reading a few of them (similar to the Floyd-Warshall algorithm)
  • Reading each value from a DB is way too slow as there are a few hundred million reads per request

Some crazy ideas:

  • Use pack() / unpack() but that would still store the values as 32 bit ints when they were unpacked
  • Store the values as pixels in an image and use PHP’s GD library to read them (would this be slow)
  • Use shmop_read() and have the Apache processes share the array
  • Memcached might work but I have no experience with it and I guess it would be many times slower than a native PHP array
  • Learn C++ and write a PHP extension
  • Recompile PHP (or HipHop?) to use 2 bytes for ints
  • Use Igbinary (useful, but will have same problem as pack())
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:40:29+00:00Added an answer on May 16, 2026 at 6:40 pm

    I would not recommend last approach. 🙂

    For the quick solution, I would pack 2 your integers in 1 PHP integer using this:

    $big = $int1 + ($int2<<16);
    
    And uppack as:
    
    $int1 = $big & 65535;
    $int2 = ($big>>16) & 65535;
    

    Also, BIG thumbs up for using shared memory. This will make your APP way faster.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very large array of doubles that I am using a disk-based
Background: I have a kubuntu laptop right now that I can't use wirelessly, i.e.
Background: we have an application that generates reports from HTML (that may or may
Hey all I have a large html string like <a style=background: rgb(100, 101, 43)
Background: I have a little video playing app with a UI inspired by the
Background I have a massive db for a SharePoint site collection. It is 130GB
Background I have been asked by a client to create a picture of the
Background: I have a module which declares a number of instance methods module UsefulThings
Background I have made a Web Service in Visual Studio, and I'm trying to
Background I have an application written in native C++ over the course of several

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.