Code speed

bearophile bearophileHUGS at lycos.com
Tue Apr 13 17:45:55 PDT 2010


So far I've just given a light reading of the code. Notes:
- 1.0L in D is a real, not a double, it's 10-12-16 bytes long.
- Mt19937 is a Mersenne twister, keep in mind that it's really good but slow. If you need less quality, use a faster generator.
- Compile your code with -w, so it says you that you have missed an "override".
- ReputationAlgorithm can be an abstract class, I think. So you can write it as:
abstract class ReputationAlgorithm {
    this() {}
    this(ref Rating[] ratings,
         ref double[] reputationUser,
         ref double[] reputationObject);
}

- Both Yzlm and AvgArithmetic classes can be marked as final, and in the other classes there are other methods can be marked as final. As in Java in D class methods are virtual unless you mark them as final, but unlike the HotSpot of Java as far as I know no D compiler is currently able to inline virtual methods.
- Dmd has both a built-ihn profiler and code coverage, use them to find hot spots.
- Put a space after commas, generally.
- pow(x, 2) and sqrt(y) can be written as x ^^ 2 and y ^^ 0.5 (but you have to import std.math anyway, because of a bug).
- You can learn to use a little of contract programming, and move in a precondition the asserts, for example of the Yzlm constructor. Then you can add class invariants too, if you want.
- Switching the contents of dynamic arrays do work (it doesn't work well with stack-allocated arrays, the contents get copied).

I'll try to add more comments later.
Bye,
bearophile


More information about the Digitalmars-d-learn mailing list