Jump to content

Recommended Posts

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?
Link to comment
https://forums.phpfreaks.com/topic/292760-faster-than-array-anything/
Share on other sites

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?

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;

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. 

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

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).

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.