SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I'm working on a script and I need some advise on the best methods for either including a file of variables, or reading variables from a file.

    The file contaning these variable is a basic flat file. The variable are all held in one large associative array in the following manner:

    $Variable[sitenamea] = 25;
    $Variable[sitenameb] = 33;
    $Variable[sitenamec] = 67;
    ........etc, etc, etc.

    The array is likely to be pretty large, and possibly containing over 1,000 variables - which brings me to my problem.

    The variables will be used in approximately 10 seperate function calls, and only approx 10 of the variables in the array will be used - the rest will be residual.

    For best performance, am I better of including the variables file once, then using them in each function (which would mean about 1000 small unused variables taking up memory) or am I better off having each function opening and then closing the variables file and just reading the required variable (which could mean 10 open and closes).

    Would 1,000 small varibles take up so much memory it would be noticable - or is it a drop in the ocean?

    Are there any other options?

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Lets say each sitename is 60 chars long and the value (25, 26, etc) is an int.
    I don't know about linux, but on win32 that would require:

    (60(2) + 2) bytes per record

    1000 * 122 bytes = 122,000 bytes

    122,000 bytes / 1024 = approx 120 kB

    So your array will probably require around 100kB of memory or less. I wouldn't think
    that would be a problem to keep it in memory during the life of the script.

    <edit>

    You should pass the array by reference to avoid copying it in memory.
    So in those functions where it gets received as a parameter append
    a "&" to the variable name:

    eg;

    function doThis( &$myArray )

    If you are not familiar with references start here:
    http://www.php.net/manual/en/language.references.php
    Last edited by freakysid; Mar 15, 2001 at 08:26.

  3. #3
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help - I was not sure if the PHP memory worked like that. From what you say, the memory used is purely a reflection of the file size that would be storing the variables - is this correct?

    To minimise things further would changing :

    $Variable[sitenamea] = 25;
    $Variable[sitenameb] = 33;
    $Variable[sitenamec] = 67;

    to

    $V[sitenamea] = 25;
    $V[sitenameb] = 33;
    $V[sitenamec] = 67;

    or even better to

    $V = array ( "sitenamea" => 25, "sitenameb" => 33,
    "sitenamec" => 67, etc etc);

    Would it make a difference to the memory required to store the variables array.

    Thanks for the advise

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by ChilliBoy
    From what you say, the memory used is purely a reflection of the file size that would be storing the variables - is this correct?
    More or less.
    To minimise things further would changing :

    $Variable[sitenamea] = 25;
    $Variable[sitenameb] = 33;
    $Variable[sitenamec] = 67;

    to

    $V[sitenamea] = 25;
    $V[sitenameb] = 33;
    $V[sitenamec] = 67;

    or even better to

    $V = array ( "sitenamea" => 25, "sitenameb" => 33,
    "sitenamec" => 67, etc etc);

    Would it make a difference to the memory required to store the variables array.

    Thanks for the advise
    I think you mean an associateive array like

    $Variable["sitenamea"] = 25;

    Note the quote marks. In this case, I can't see that there would be any difference between the three examples you give.

  5. #5
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the advise - any thoughts on my below post "function include files"?

    Cheers


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •