I have a large array of almost 1200 indices. Each one is a string, and they vary widely in length. During my script's execution most of them are not needed, but there are a few times when most of them will get used. I'm trying to find out if I need to break up this array and only load certain parts when I need them... which will mean a bit of code restructuring.

So my question is two part. At what point do I start to concern myself with the memory requirements of my scripts, and how do I calculate that? Is there possibly a simple formula that I can use to calculate the size of an array, even if it's not an exact answer? Like 1 byte per character in the string, plus one for each character in the key (I use associative keys)?

    right as far as i know, when you declare a variable, it sets aside a certain amount of memory, pending on the data type. how much a string sets aside, i dont know.

    if you have an array of 10 strings you will have 10*string memory size, regardless of what is in it.

    if you are concerned with memory management, i suggest using pointers. create an array of pointers, pointing to type string. then only call the items in the array which are !=NULL.

      heh no pointers in php thankfully.
      but the size of a php string is its length plus 1 byte. since arrays are hashes you should count the length of the key, plus the string length+1 of each value in the array.

      depending on your os you can view how much memory a script uses while its running. also you can time your script and see how long it takes to initialize the array.
      www.php.net/microtime has a good sample of a function to time how long it takes to run code.

        cant use pointers?? rofl i could have swore i saw a script using them...would have thought it would have pointers.

          php doesnt have pointers, there is no need for them since all the memory management is handled by the zend engine that runs php. strings are strings to a php programmer, they dont see it as a pointer to a memory location containing a null terminated string.
          closest thing is [man]references[/man].

            orite, cool. i really need to get a good PHP FAq or something lol, and get to understand its workings better.

              www.php.net will pretty much have everything you would want to know. there are certain areas that are more valuabe than others, mostly chapters 3,4,5, a few areas in the function reference, like strings, arrays, and filesystem functions, then a few sections from the appendix has good stuff.

                right thanks mate this will come in useful. 🙂

                  Thanks drew, that's what I thought. It's the same as MySQL.

                  The array is compiled of many different language files for all the modules in a CMS I'm building. Since the modules need to have access to values and "words" in any other module at any given time, I need to load all the language files and have them ready to be accessed. So this array happens to be ~75K in size with 9 modules, but I expect that to grow as I continue to develop more features. The thing that I need to keep reminding myself of is that it's a CMS, not a website. It will only be used by a client as often as a few times a day (most likely a few times a week), so it doesn't need to be extremely efficient with the resources... but I do want to try my best.

                  I do have my own internal timer for debugging the code. If I try to separate the language files into two types: "global" for shared language values, and "local" for language values that would be used by that module alone... then I will increase the speed of my script by 10% (a few milliseconds), but it would mean many different changes elsewhere that I am unwilling to make.

                    Write a Reply...