Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/caferrari/shuffle

Shuffle a huge amount of numbers
https://github.com/caferrari/shuffle

Last synced: about 2 months ago
JSON representation

Shuffle a huge amount of numbers

Awesome Lists containing this project

README

        

Caf\Shuffle [![Build Status](https://travis-ci.org/caferrari/Shuffle.png?branch=master)](https://travis-ci.org/caferrari/Shuffle)
===========

This lib was made with the objective to shuffle a huge amount of numbers

Performance
-----------

It's a lot slower than the combo range + shuffle functions but it does have a nice memory advantage:

10000000 (10 million) numbers:

range + shuffe: 1425.28 MB in 9 seconds
Caf\Shuffle: 38.59 MB in 24 seconds // Exported file: 40MB

100000000 (100 million) numbers:

range + shuffe: PHP Fatal error: Allowed memory size of 4294967296 bytes exhausted (tried to allocate 32 bytes)
Caf\Shuffle: 381.79Mb in 228 seconds // Exported file: 400MB

Advantages
----------

You can export the generated numbers in a nice binary file to be used later or in another place so it becomes more useful

$r = new Caf\Shuffle\Shuffle(10000000);
$r->initialize();
$r->shuffle();
$r->exportToFile('rand.bin');

It's very easy to reuse later:

$r = new Caf\Shuffle\Shuffle(10000000);
$r->loadFile('rand.bin');

foreach ($r->getIterator() as $number) {
echo $number . PHP_EOL
}