From: John D'Errico on
Walter Roberson <roberson(a)hushmail.com> wrote in message <r9J8o.57334$Bh2.19415(a)newsfe04.iad>...
> John D'Errico wrote:
> > PauliePorsche <Bhopale73(a)gmail.com> wrote in message
> > <1653919816.96521.1281574388921.JavaMail.root(a)gallium.mathforum.org>...
> >> I have to run lengthy simulations which involves generating numbers
> >> randomly literally millions of times. I ended up running this
> >> simulation, figured out I'll need to tweak some algos, and re-run the
> >> simulation
> >>
> >> My question is it quicker to import a text file (from the previous
> >> simulation) and use the random numbers from that than it is to use the
> >> random no# simulator?
> >
> > Reading a text file is very slow in comparison to any
> > simple ops like generation of random numbers.
>
> On the other hand, it might plausibly be efficient to read in a binary
> file with the data. Although reading from disk is relatively slow,
> generally the more you read at one time the more efficient it gets (the
> fixed overheads get distributed over more data), and possibly "millions"
> of random numbers is enough to make it worth while.

My gut says no. But it might be wrong.

tic,R = rand(1000000,5);toc
Elapsed time is 0.076724 seconds.

save R.mat R
tic,load R,toc
Elapsed time is 0.288657 seconds.

Nope. Not wrong. Generate them on the fly,
using the same seed if this is your goal.

John
From: Walter Roberson on
John D'Errico wrote:
> Walter Roberson <roberson(a)hushmail.com> wrote in message

>> On the other hand, it might plausibly be efficient to read in a binary
>> file with the data. Although reading from disk is relatively slow,
>> generally the more you read at one time the more efficient it gets
>> (the fixed overheads get distributed over more data), and possibly
>> "millions" of random numbers is enough to make it worth while.

> My gut says no. But it might be wrong.
>
> tic,R = rand(1000000,5);toc
> Elapsed time is 0.076724 seconds.
>
> save R.mat R
> tic,load R,toc
> Elapsed time is 0.288657 seconds.
>
> Nope. Not wrong. Generate them on the fly,
> using the same seed if this is your goal.


>> fid = fopen('/tmp/random.dat','w'); fwrite(fid,rand(1000000,1),'*double');
fclose(fid)
ans =
0
>> !ls -l /tmp/random.dat
-rw-rw-r-- 1 roberson roberson 8000000 2010-08-12 13:46 /tmp/random.dat
>> fid=fopen('/tmp/random.dat','r');tic;r=fread(fid,'*double');toc,fclose(fid)
Elapsed time is 0.013720 seconds.

>> tic;rand(1000000,1);toc
Elapsed time is 0.022344 seconds.

Results might vary with speed of the computer and speed of the file system.