Toby Ewing wrote:
If you want to add more interfaces, I'd agree if they're nontrivial enough -- but in contrast e.g. `randrEx1 (range)' is equivalent to (and in fact does the same as) `range * Random' when `Random' calls `Default_RandReal' internally, so I don't think it justifies another RTS function.
I agree in the specific case of randrEx1 - it is trivial. But it was included to fill out the complete suite of random doubles in: [0..1) [0..r) [0..1] [0..r] (0..1] (0..r] (0..1) (0..r) which may look like overkill, but there are times for each.
<0..r> = r * <0..1> (for <> in { [], () } ;-)
(0..1] = 1 - [0..1)
[0..1] and (0..1) may be useful (though, still, they're one-liners, just as the existing [0..1) ).
At a quick glance, also `randNorm' seems problematic since if `bsecond' is set, it ignores its arguments.
This can look confusing at first glance, but it's actually a rather nice algorithm.
I'm not doubting the algorithm, just an implementation detail (see my comment above). It seems fixable with moderate effort.
The code that I started with, the C++ abyss from which I rescued some civilized Pascal, claims to be faster than what I wrote. It uses the C and C++ directives "inline" and "register", and some arcane C constructions that are better avoided in clear code.
Yes, GPC provides them as attributes, but with `-O3' for `inline' and `-O' for `register', it also does it automatically, so they're recommended only in very special cases.
I found that the speedup resulting from going from -O2 to -O3 was quite significant. Presumably that is due to the inlining. I'm not familiar with the -O attribute: can you please say more about how and where to use it?
Basically, without `-O', the generated code is very bad. You can do without it if compile times are important and/or runtime is neglectible. Otherwise at least `-O' is recommended. (In particular, timings without `-O' are pointless.)
`-O2' generally improves the generated code significantly, but also compile-time quite significantly. Many packages use this as the default.
The only difference in `-O3' (unless that's changed in newer backends) is automatic inling. `-O2' should work just as well if you declare the critical routines with `attribute (inline)' (as I said, I don't recommend this in general, but for certain cases, such as here, I'd accept it).
Frank