Nyla Posted November 27, 2014 Share Posted November 27, 2014 I have a 5000 item array of grocery UPC numbers and prices (here's a snippet): $myarray = array( '3540132662329' => '74.46', '8870132664186' => '63.24', '1090132664313' => '79.56', '6550132671425' => '46.92', '0020132685310' => '37.74', ); When someone inputs a UPC number, a price shows up: echo $myarray["6550132671425"]; // echoes 46.92 Is there anything FASTER than using an array? For example, converting the array into a string like this: $mystring = " 3540132662329-74.46, 8870132664186-63.24, "; ...and then using string functions like strpos, and substr to parse out the price? Or is it better to stick with arrays? Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted November 27, 2014 Share Posted November 27, 2014 Use a database and retrieve only what you need from it. Large arrays will never be good to work with. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted November 27, 2014 Share Posted November 27, 2014 Before you jump to weird optimization attempts, tell us something about the context and the actual problem. Why on earth do you have an array with 5,000 prices? Is this hard-coded into your application? Why don't you use a database? And do you actually have performance issues? Have you measured them and identified the concrete cause? Or is this all just speculation? Quote Link to comment Share on other sites More sharing options...
bsmither Posted November 27, 2014 Share Posted November 27, 2014 It is a curiosity to me to know if PHP would ever get confused and consider the keys as integers. Hopefully not, then maybe a ksort($myarray) might let PHP find the key faster. But I really do not know. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 27, 2014 Share Posted November 27, 2014 If you are that curious, why don't you time how long it takes to get a few hundred items from your array. Then convert the array to a string and time how long it takes doing it that way. $t1 = microtime(true); // perform the data extraction here $t2 = microtime(true); $timeTaken = $t2 - $t1; Quote Link to comment Share on other sites More sharing options...
Frank_b Posted November 28, 2014 Share Posted November 28, 2014 once that an array is loaded in your servers memory it will be as fast as any other variabele in your server's memory. But with PHP it will common be loaded again and again on every request to the server. For the best performance you should programm an apache extension that loads your array once on startup and keeps it into memory as long that the server is up and running. Database engines takes that job away for you and serve you a lot more behavior which you can use. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted November 28, 2014 Share Posted November 28, 2014 And considering that this is a php script that is not on the server, the response is always controlled by the connection speed and server response time (load?). Your concern about an array retrieval might be more pertinent if this was a JS problem rather than PHP Quote Link to comment Share on other sites More sharing options...
Nyla Posted November 28, 2014 Author Share Posted November 28, 2014 Thank you for all the replies! Okay, it just became apparant to me that I should have explained all the details, my mistake. So, here goes: 1.) My machine has a mySQL database on it (all 3000 UPC codes, and their corresponding prices). 2.) Another website is #1 on Google, and gets a BAZILLION queries per second. 3.) I provide MY page as an "API page." In other words, when THEIR machine gets a query (e.g. requesting a Price for a particular UPC code), THEIR machine queries MY machine. 4.) Rather than having THEIR machine cause MY machine to do a mySQL lookup a bazillion times a second, I thought it would be faster if my "API page" had a simple Array on it (the Array I described), to provide the result (the "price") for the UPC queries. Now, I'm wondering if I should just "get rid" of the entire array, and have it so when their machine queries MY machine, my machine does a mySQL lookup each time. When THEIR machine accesses my API page (with the big array on it), I always "assumed" that their machine loads my Array into its memory..... but I don't know for sure, I have no way of knowing, and I cannot control whether it does, or does not. My goal is to provide the quickest result possible. Important: When THEIR machine queries my machine, as well as a whole bunch of other websites like mine, whoever returns the result the FASTEST gets on the top of their result list. So, I have an incentive to make my machine provide a result faster! (If a competitor's machine takes 3 billionths of a second to produce a result, I want my machine to only take 2 billionths of a second). Quote Link to comment Share on other sites More sharing options...
bsmither Posted November 28, 2014 Share Posted November 28, 2014 I would think that this is only valid to explore the possibilities if all other aspects are identical. Such as the method of communication between their machine (T) and your machine (Y). (This reminds me of New York stock broker infrastructure where the distance of fiber-optic lines determine whether an interloper can leap ahead on a trade.) If all that Y does is answer API calls -- which you should be able to control what type of data is acceptable and what the response will be -- then maybe a XAMP stack is not your solution. Maybe something far more dedicated and specialized? Quote Link to comment 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.