Creating fast pseudo-random data in PHP

I need to generate incompressible data (therefore pseudo-random) and tried using the code below. But this is only about 10 MB / s of data. I need about 100-200 MB / s. Do you have any tips?

if ($length > 8*1024*1024){ //Default max string length in php. while (($length - $bytesGenerated)>8*1024*1024){ $bytesGenerated = $bytesGenerated + (8*1024*1024); print(openssl_random_pseudo_bytes(8*1024*1024)); } } print(openssl_random_pseudo_bytes($length - $bytesGenerated)); 
+7
php random
source share
4 answers

if you are working under Linux, you can just read from / dev / urandom, its kernels are a fast pseudo-random generator.

on the other hand, you can use openssl rand and pipe, which is in php.

+5
source share

Do you need cryptographic pseudo random numbers? Otherwise, you can try to implement the Linear Feedback Shift Register.

+4
source share

It will be difficult in PHP, even with accelerators. I would write a C ++ module specifically for this function (but even in C ++ it is not very simple).

+2
source share

With maximum theoretical maximum performance on a 3GHz (x86) processor, you will have a budget of just under 4 processor instructions per random byte if you are trying to hit 200 MB / s. Actual performance will be significantly less. I would say that it will be extremely difficult in any language. You are well versed in speeds that typically use a dedicated hardware accelerator (i.e., you are trying to make 1.56 Gbps per second). Network or video applications have a significant amount of external equipment designed to handle this bandwidth. An extremely efficient C or assembly implementation may allow you to hit this limitation, but you really push the limits of what is possible using only general-purpose hardware.

I would consider either pre-generating the data (as already suggested) or using some kind of hardware crypto-calculator to hit everything that looks like these kinds of bandwidth. I found this list of crypto cooler hardware suppliers .

As a final thought, you actually meant 200 mega bytes per second, right? If you meant mega bits , then this problem is much more easily resolved.

+1
source share

All Articles