Skip to content

Instantly share code, notes, and snippets.

@srs81
Last active December 10, 2015 15:38
Show Gist options
  • Save srs81/4455806 to your computer and use it in GitHub Desktop.
Save srs81/4455806 to your computer and use it in GitHub Desktop.
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