Random number generation speed

Quick question, has anyone done a benchmark on random number generation between javascript and php? I'm not talking about 4-5 random function calls but on a large scale, I'm more interested about speed rather than better random numbers.

Answers


As Alan pointed out, the performance of JavaScript's random numbers depends on the resources the client-side brings to the party. However, there are a few more things to consider:Since you're looking for random numbers, generated on a large scale, it is important to note that browsers may stop your JavaScript from reaching the end of a loop. Some browsers might assume the script contains a deadlock somewhere and ask the user to stop or debug the script. In addition to this, the numbers will only be generated as long as the client is on the page and has JavaScript enabled.

On PHP, there are two things to consider (apart from server hardware). If you're really planning on generating vast quantities of random numbers, the response to your client will be slowed down, too. Though PHP is faster then JavaScript (by far), it's no good to have your visitors wait too long for a response from the server. A possible solution might be to send the response out anyway and get the randomly generated numbers via ajax. Downside here is: you've lost the speed benefit of php.

In your situation, I'd keep the random generator server side. Perhaps look into other languages to put together a little C programme if you're really focused on speed and server load (PHP is resource expensive). But sticking to the tags, again: stick to php - after all, quite a lot of it is C anyways...


This will greatly depend on your hardware and environment. In Javascript, different browsers will run at different speeds for example.

The best bet is to write a loop in each and run it to generate a large number of random numbers and show you how long it took. That way you will know which runs better for your particular setup.


Need Your Help

Arrays not counting correctly

java arrays

I know I was just asking a question earlier facepalm

HREF Link ajax post request

php ajax post request href

To make shot, here is my problem. I have a script that sends Form POST request through ajax but my problem is I want to do it in A Href Tag. Below is my code for FORM POST Request: