Lazy Variadic Functions

Wyverex wyverex.cypher at gmail.com
Fri Aug 8 08:56:28 PDT 2008


bearophile wrote:
> Can someone tell me what are some possible practical purposes of Lazy Variadic Functions? They are explained in the D docs (http://www.digitalmars.com/d/1.0/function.html ), but such docs sometimes sorely miss various practical usage examples... (I think such docs have to become a wiki that people can update, etc).
> 
> Bye,
> bearophile


I've used them in some multi threading applications..


import 	tango.io.Stdout,
	tango.core.ThreadPool,
	tango.time.StopWatch,
	tango.core.Thread;


private ThreadPool!() pool;

static this()
{
	setPool;
}

void setPool(uint size = 12)
{
	pool = new ThreadPool!()(size);
}

//Parallel block********************************************************
// Lazy number of inputs
void p_block(void delegate()[] blocks ...)
{
	foreach(blk; blocks)
		pool.assign(blk);
		
	while(pool.activeJobs || pool.pendingJobs)
		Thread.sleep(0);
}//*********************************************************************


void main()
{
	int a, b, c, d, e, f, g, h, i, j, k, l;

	Stdout("testing").newline;

	StopWatch w1;
	w1.start;
	
	p_block
	(
		{ for(a = 0; a < 10003201; ++a){} }
		,
		{ for(b = 0; b < 80213002; ++b){} }
		,
		{ for(c = 0; c < 561123423; ++c){} }
		,
		{ for(d = 0; d < 10003201; ++d){} }
		,
		{ for(e = 0; e < 80213002; ++e){} }
		,
		{ for(f = 0; f < 561123423; ++f){} }
		,
		{ for(g = 0; g < 10003201; ++g){} }
		,
		{ for(h = 0; h < 80213002; ++h){} }
		,
		{ for(i = 0; i < 561123423; ++i){} }
		,
		{ for(j = 0; j < 10003201; ++j){} }
		,
		{ for(k = 0; k < 80213002; ++k){} }
		,
		{ for(l = 0; l < 561123423; ++l){} }
	);

	auto t1 = w1.stop;


	Stdout.format("p_block = {} {} {}", a, b, c).newline;

	StopWatch w2;
	w2.start;

	{ for(a = 0; a < 10003201; ++a){} }
	{ for(b = 0; b < 80213002; ++b){} }
	{ for(c = 0; c < 561123423; ++c){} }
	{ for(d = 0; d < 10003201; ++d){} }
	{ for(e = 0; e < 80213002; ++e){} }
	{ for(f = 0; f < 561123423; ++f){} }
	{ for(g = 0; g < 10003201; ++g){} }
	{ for(h = 0; h < 80213002; ++h){} }
	{ for(i = 0; i < 561123423; ++i){} }
	{ for(j = 0; j < 10003201; ++j){} }
	{ for(k = 0; k < 80213002; ++k){} }
	{ for(l = 0; l < 561123423; ++l){} }

	auto t2 = w2.stop;

	Stdout.format("linear = {} {} {}", a, b, c).newline;
	
	Stdout.format("p_block time: {}  Linear time: {}  Delta: {}", t1, t2, 
t2-t1).newline;
}


More information about the Digitalmars-d-learn mailing list