Page 2 of 2
Re: Random 12 char codes
Posted: Thu Mar 13, 2008 1:27 pm
by onion2k
Building the list of available characters on every call to gen_code() is silly. Use an array at least, better yet use a static array (like I did).
Re: Random 12 char codes
Posted: Thu Mar 13, 2008 1:42 pm
by miro_igov
What is the meaning of static ? It could be built out of the function and used as global of course.
Any suggestion to make browser not saying Done with blank screen when i run it for 10,000,000 codes?
Re: Random 12 char codes
Posted: Thu Mar 13, 2008 3:13 pm
by Mordred
Most of the details were done right in the various attemps I've seen so far, but noone has managed to get them all in one place:
1. Generate all numbers in memory, write to database later.
2. Batch many numbers in one insert, don't go over the mysql query limit.
3. Don't use indexes on the table when you insert, generate them later when all data is inserted.
4. Static array with the possible values.
5. No function calls
6. Loop should be written like this:
Code: Select all
while (1) {
//I don't know offhand if mt_rand() or count($array) is faster. If count() is faster, omit mt_rand. Tweak the 10000 for better results.
if (mt_rand(0, 10000) == 0 && count($array)>=2000000) break; //I forget, was it 2 mln, or another value? Replace if needed
... //generate a $number, no function calls
$array[$number]=0; //no checking whatsoever!
}
//write to database, keep in mind that count($array) will not be exactly what you want, so truncate.
Re: Random 12 char codes
Posted: Thu Mar 13, 2008 3:45 pm
by onion2k
miro_igov wrote:What is the meaning of static ? It could be built out of the function and used as global of course.
Static makes PHP remember the variable between calls to the same function so it doesn't have to initialise it every time. It's mostly used for things like counters, but it's handy if you're using complex variables like arrays because it makes things a little quicker.
miro_igov wrote:Any suggestion to make browser not saying Done with blank screen when i run it for 10,000,000 codes?
Output something with echo then use flush() to push it to the browser. Don't do it on each iteration though, that'll be hellishly slow. Something like..
Code: Select all
if ($counter++%10000==0) { echo "."; flush(); }
Re: Random 12 char codes
Posted: Fri Mar 14, 2008 6:49 am
by miro_igov
Does that flush() require ob_start() or something similar? I just tried it and no result. The data is displayed when the script completes and if it takes so long it still displays blank browser and status Done.
Re: Random 12 char codes
Posted: Fri Mar 14, 2008 6:52 am
by onion2k
miro_igov wrote:Does that flush() require ob_start() or something similar? I just tried it and no result. The data is displayed when the script completes and if it takes so long it still displays blank browser and status Done.
If you've got output buffering switched on you'd need to use ob_flush() instead of (or as well as) flush().
Re: Random 12 char codes
Posted: Fri Mar 14, 2008 6:54 am
by miro_igov
No, output buffering is off, but flush() does not work.
Re: Random 12 char codes
Posted: Fri Mar 14, 2008 6:57 am
by onion2k
Hmm.. it works for me. If flushing the webserver buffer/PHP output buffer doesn't send the content to the browser then I imagine it's not going to be possible.
Re: Random 12 char codes
Posted: Fri Mar 14, 2008 7:03 am
by Mordred
Miro, let me guess: IE? I recall problems with it waiting for the whole script output sometimes. Try with a browser

(Opera, FF)
Re: Random 12 char codes
Posted: Fri Mar 14, 2008 7:07 am
by miro_igov
Mordred, bad guess, i am FF fan

FF does not output anything until the script ends, even with the flush() after every 10 000 iterations ($i%10000==0)
onion2k i noticed an issue with your code - you suggest $ac[rand(0,36)], rand may return something between 0 and 36 but we do not have 36th index in and array with 36 elements

so i believe it should be rand(0,35) or rand(0,$l-1).