file rawRead and rawWrite in chunks example
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Aug 8 17:50:14 PDT 2015
On 08/08/2015 04:11 PM, Jay Norwood wrote:
> I'm playing around with the range based operations and with raw file
> io. I couldn't figure out a way to get rid of the outer foreach loops.
When the body of the foreach loop performs something, then
std.algorithm.each can be useful:
import std.algorithm;
import std.stdio;
import std.range;
import std.datetime;
void main()
{
auto fn = "numberList.db";
std.datetime.StopWatch sw;
sw.start();
scope(exit) std.file.remove(fn);
{
auto f = File(fn,"wb");
iota(10.5, 20_000_010.5, 1.0)
.chunks(1000000)
.each!(a => f.rawWrite(a.array));
}
{
auto f = File(fn,"rb");
const int n = 1000000;
// NOTE: D-style syntax on the left-hand side
double[] dbv = new double[n];
// NOTE: No need to tell rawRead the type as double
iota(10, 20_000_000 + 10, n)
.each!(a => f.rawRead(dbv));
}
long tm = sw.peek().msecs;
writeln("time msecs:", tm);
}
Ali
More information about the Digitalmars-d-learn
mailing list