Cloud computing - PHP & MySQL
I have an algorithm written in PHP which communicates with MySQL. On standard shared hosting server it takes about ~5 seconds to calculate. My question is - how can I speed it up (of course without changing the algorithm itself), use dedicated/VPS, maybe some cloud computing or...?
What are your suggestions?
EDIT: It开发者_JS百科's Dijkstra algorithm for public transport. I have (only) 3500 unique stops.
In short - I have multiple arrays for recording active stops, previous stops, lineToStop, totalTime, travelingTime and so on. I loop for number of stops, get an id of a stop, build connections to other stops (based on the time on that active stop), loop again for size of stops() to find the one with the lowest value, mark it as active and continue again.
I realized it's optimizatio problem so - > Dijkstra algorithm optimization/caching
When you buy an airline ticket, the first question is not "where do you want to go?" - it is "where are you going from?" In other words, without much, much, MUCH more detail, we're just guessing.
The surest answer for "How can I speed it up?" in any case is this:
Measure it, find actual bottlenecks, remove bottlenecks, repeat until it runs well enough.
Without profiling, you could waste endless hours on optimizing some part of the code that doesn't have any significant performance impact (Note the emphasis on "actual bottlenecks" - many programmer-centuries were spent optimizing what someone imagined might be the bottleneck).
Example: speeding up a method by 1000% is pointless, if it's a method that gets called once at the start of your program, and the program spends 90% of its time waiting for disk I/O. Another example: making a disk array to help with I/O bottlenecks, when the program spends 90% of the time waiting for the SQL server which runs a complex, unindexed query. Those are not the only problems you could encounter, and they aren't even mutually exclusive - but you need to know what problems you have before you start solving one. "It is slow" is not a description of a problem, it is just a symptom (just like "a headache" can be a symptom of 9000 different medical conditions).
TL;DR: There is no silver bullet.
You'll first need to find where the bottleneck is. Use a profiler (xdebug) and see where in your PHP the slowness is. It may be that the majority of the time is waiting for connectivity to MySQL, or MySQL doing processing, or something else. If the slowness is actually in your algorithm code, then options are change your algorithm, which you don't want. Or throw more processing power at it. But I'd definitely find out where exactly is the slowness first before looking at throwing more processing at it.
"cloud computing" is not some magic that can boost your calculations. It's a technology and yo will need dramatically rewrite your algorithm to use it, and most probably you'll have to use no SQL database at all.
I'm think you have go much simpler way: by optimizing the algorithm and tuning your db server. I am sure that such time can be reduced by factor of 10 and even more than that, by using traditional optimization methods. Did you use any already, BTW?
You have a database query inside a loop. Have you tried getting ALL the rows once, and looping through the results in memory? If you're making 3500 queries on 1 page load, it might be faster.
Depending on the algorithm, and whether the bottleneck is in your php code or in the database, you could try using a php compiler, like for example facebooks hiphop: http://developers.facebook.com/blog/post/358/
Since you don't have a lot of traffic, but instead one algorithm that is run once, and that has to be faster, you probably need the php compiler (like the one I've linked), NOT an opcode cacher (like bcompiler or similair), since that only helps when running a lot of scripts.
All an opcode cached does is speed up the parsing of scripts, but in your case that happens only once per 5-second run of the script and is probably no the bottleneck. A compiler however, translates your php code to native assembly, which, even if not becoming as fast a c equivalent, is still averagely 2 times faster than runnning in PHP.
Again, this depends on your design. If the script just waits for the database most of the time of those 5 seconds, compiling won't help you much.
What kind of algorithm are we talking about here?
精彩评论