Skip to content

Instantly share code, notes, and snippets.

@srs81 srs81/write_speed.php
Last active Dec 10, 2015

Embed
What would you like to do?
Test the speed of writing 10,000 random strings 100 times each (1,000,000 lines total) to disk. Run as "time php write_speed.php"
<?php
// Length of each line
$length = 40;
// Open up a file
$file = fopen("test.txt", "w");
// Generate 10,000 strings
for ($iii=0; $iii<=10000; $iii++) {
// Generate the random string
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$size = strlen( $chars );
$str = "";
for( $i = 0; $i < $length; $i++ ) {
$str .= $chars[ rand( 0, $size - 1 ) ];
}
// Write this string to file 100 times
for ($ii=0; $ii<=100; $ii++) {
fwrite($file, $str . "\n");
}
// Loop to get to next random string
}
// Close the file
fclose($file);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.