I have a script written in PHP that sends an API call to eBay. There are several (15-50) calls made every second. I did some benchmarking and noticed that PHP doesn’t execute these as fast as I’d like since it doesn’t run them in parallel.
My question: Is it a good idea to convert the script into Java, so as to give it multi-threaded capabilities? Will this even run each request in parallel like I think it would or am I misguided to believe so?
Any other suggestions are welcomed.
Thanks.
5
Answers
Check the Computer Language Benchmark Shootout Game
Java 1.71 PHP 63.19 so Java is about 37x faster.
Using Java, you certainly could use the Threads API to execute requests in parallel (probably using a
ThreadPoolExecutor
). That said, adding concurrency does not guarantee that the code will execute in less time overall.Do you know that the PHP code is even the bottleneck in execution? It could very well be that the eBay API throttles requests, so that you can only send a fixed number of requests per second, in which case it really doesn’t matter how fast your code is. Or, it could be the case that you have a really slow network, or really high latency — again, your time would be better spent fixing those problems first.
PHP has support for threading if you compile it in via PCNTL which could potentially save you more time then re-writing a lot of the large network libraries in Java.
First, rewriting your script in Java is not going to magically make it multithreaded, and unless you have experience with concurrent programming I would avoid this. Even then this doesn’t necessarily mean it will run faster than your PHP script.
Second, PHP can execute multiple http requests in parallel using cURL’s multi exec functions. See the docs for curl_multi_init and curl_multi_exec for how to use them.
How about using multiple invisible frames so the browser can process them in parallel and having the visible page feed requests to the frames and incorporate the responses client-side?