file rawRead and rawWrite in chunks example

Jay Norwood via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Aug 8 16:11:52 PDT 2015


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.

Nice execution time of 537 msec for this, which creates and reads 
back a file of about 160MB (20_000_000 doubles).


import std.algorithm;
import std.stdio;
import std.conv;
import std.math;
import std.range;
import std.file;
import std.datetime;
import std.array;

void main()
{

	auto fn = "numberList.db";
	auto f = File(fn,"wb");
	scope(exit) std.file.remove(fn);
	std.datetime.StopWatch sw;
	sw.start();
	
	foreach(elem; chunks(iota(10.5,20_000_010.5,1.0),1000000)){
		f.rawWrite(elem.array());
	}
	f.close();
	f = File(fn,"rb");

	const int n = 1000000;
	double dbv[] = new double[n];
	foreach(i; iota(10,20_000_000+10,n)){
		f.rawRead!(double)(dbv);
	}

	f.close();
	long tm = sw.peek().msecs;
	writeln("time msecs:", tm);

}


More information about the Digitalmars-d-learn mailing list