Re: Range of random numbers

2012-04-25 Thread Christophe Travert
bearophile , dans le message (digitalmars.D.learn:35148), a écrit :
 Why don't you write a little benchmark to compare the performance 
 of the two versions?

Because I'm interested in the code's meaning for the human reader, not 
the performance.

I actually think : map!(_= uniform(a, b))(repeat(0)) is better, 
because it puts the call to uniform first, and the _ indicates that the 
rest is unimportant.


For you curiosity, this is the benchmark using:
gdc (GCC) 4.7.0 20120322 (gdc 0.31 - r805:0414cec152c7, using dmd 2.057)
and
gdc -O3 testRNG.d  ./a.out


import std.array, std.range, std.algorithm, std.random, std.stdio, 
std.datetime;

auto uniformRange1(T1, T2, T3)(T1 a, T2 b, T3 gen)
{
  return map!((x) {return x();} ) (repeat((){return uniform(a, b, gen);}));
}

auto uniformRange2(T1, T2, T3)(T1 a, T2 b, T3 gen)
{
  return map!((x) { return uniform(a, b, gen); })(repeat(0));
}

auto uniformRange3(T1, T2, T3)(T1 a, T2 b, T3 gen)
{
  return map!((x) { return uniform(a, b, gen); })(cycle([0]));
}

void f1() { auto ur = array(take(uniformRange1(0, 10, Xorshift(1)), 
1000)); }
void f2() { auto ur = array(take(uniformRange2(0, 10, Xorshift(1)), 
1000)); }
void f3() { auto ur = array(take(uniformRange3(0, 10, Xorshift(1)), 
1000)); }

void main() {
  auto b=benchmark!(f1, f2, f3)(1000);
  writeln(b[0].to!(seconds,double),s,  , b[1].to!(seconds,double),
s, , b[2].to!(seconds,double),s. );
  // outputs :
  // 0.040437s,  0.0393537s, 0.0486439s
}


f2 performs 23% better than f3, and 3% better than f1.

-- 
Christophe


Re: An issue with setting delegates via templates

2011-11-02 Thread Christophe Travert
Andrej Mitrovic , dans le message (digitalmars.D.learn:30315), a écrit :
 The new one: https://gist.github.com/1194497
 
 I'll investigate this further then.

OK, so there is indeed a filter to find a suitable delegate to load:

@trusted T connect(T)(T handler) if(isHandler!(T, Types));

But only the first Bar.x function is picked and given to isHandler!(T, 
int) to check it can be called. Bar.x(double) do not pass this filter.

I guess if the signature were:

T connect(void handler(Types));
T connect(bool handler(Types));
etc...

or maybe
T connect(T : void handler(Types)) (T handler);
T connect(T : bool handler(Types)) (T handler);
etc...

Then the compiler should be able to select the right Bar.x method. But 
with the isHandler filter,it is not able to find it. I don't see a way 
to improve isHandler to make it able to find the right Bar.x method, 
without modification of the compiler*, but I am not an expert here.

* the compiler should allow all overloads of a function given as a 
template argument to be tested against the filter to choose the right 
one, and complain if several overloads match the filter.

-- 
Christophe