viralplatipuss Posted July 14, 2011 Share Posted July 14, 2011 So I'm trying to keep my code as optimised as possible (as any good coder should). But I was hoping someone could tell me what's more optimised in this situation: So basically I have some essentially static data, which is just a whole bunch of variables I may need to reference in my php script. so lets say I have the file include.php which contains something like this: <?php $a = array(); $a['random'] = 10; $a['variables'] = 20; $a['are'] = 40; $a['here'] = 70; $b = array(); $b['random'] = 50; $b['variables'] = 60; $b['are'] = 70; $b['here'] = 80; ?> And I have a the main file that's executed called main.php, sometimes I might need all the data from $a, sometimes from $b, sometimes both. Now I could just include the file so both data sets are accessible to me: <?php require_once('include.php'); $my_var = $a['random'] * $something_else; ?> But what if I split those data sets into their own files, called include_a.php, and include_b.php so include_a would just have: <?php $a = array(); $a['random'] = 10; $a['variables'] = 20; $a['are'] = 40; $a['here'] = 70; ?> then in my main file, depending on what I need I could dynamically load in JUST the data I need: <?php $my_required_data_set = 'a'; //from some external factor like a POST var require_once('include_'.$my_required_data_set.'.php'); $my_var = $a['random'] * $something_else; ?> Obviously the action of requiring a file takes resources, but say I had quite a bit of data, around 10 variables for each array, and say, 100 odd arrays all in that file. Would this overall be more optimised? One other quick question, would the same principle apply to functions? If I had a big php file full of functions, like so: <?php function randomFunction1() { echo "I'm so random, lolz."; } function randomFunction2() { echo "I'm REALLY random, lolz."; } //.etc ?> Would it be better to stash them in one big functions file, or separate them into smaller files and only call the ones I wish to use? I'm hoping a PHP expert can help me out on this. Thanks in advance. --- Dom Quote Link to comment https://forums.phpfreaks.com/topic/242020-is-it-more-optimised-to-split-only-essential-code-between-files/ Share on other sites More sharing options...
requinix Posted July 14, 2011 Share Posted July 14, 2011 It's generally faster to load too much data in fewer files than exactly the right data in multiple files. Unless you're talking about many megabytes of memory for these arrays, one file will be fine. Ditto for functions. Focus more on segregating them into logical pieces if necessary: database stuff here, user stuff there, and so on. Going down into per-variable or per-function levels is just too much of a hassle. Quote Link to comment https://forums.phpfreaks.com/topic/242020-is-it-more-optimised-to-split-only-essential-code-between-files/#findComment-1242871 Share on other sites More sharing options...
viralplatipuss Posted July 14, 2011 Author Share Posted July 14, 2011 Thanks requinix, It was also not so much the worry about the size of the file itself, but more the fact all the arrays/variables in that file will still be read and put into memory un-necessarily. I guess I could at least use a single file, but break up the array creation into functions and just call the function for the data set. Quote Link to comment https://forums.phpfreaks.com/topic/242020-is-it-more-optimised-to-split-only-essential-code-between-files/#findComment-1242874 Share on other sites More sharing options...
Psycho Posted July 14, 2011 Share Posted July 14, 2011 Focus more on segregating them into logical pieces if necessary I agree with that. The performance benefits you would gain/lose here are negligible if we're not talking huge amounts of data. So, do what makes logic sense for the overall structure and maintainability. Although , in your example above if you have ~100 arrays all with the same indexes it seems that storing the values in a database would make more sense. Quote Link to comment https://forums.phpfreaks.com/topic/242020-is-it-more-optimised-to-split-only-essential-code-between-files/#findComment-1242877 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.