WordCount performance
bearophile
bearophileHUGS at lycos.com
Thu Mar 27 18:15:20 PDT 2008
Dave Wrote:
> The shootout benchmarks seem to indicate that in the general case DMD is
> pretty close to GCC except for floating point (again, in general).
Micro-benchmarks have the huge advantage that they allow you to find where a (micro) performance problem may be, the following is a stripped down benchmark from the Shootout:
D version:
import std.stdio, std.conv;
double fibo(double n) {
if (n < 2) return 1.0;
return fibo(n-2) + fibo(n-1);
}
void main(string[] args) {
int n = args.length == 2 ? toInt(args[1]) : 1;
printf("%.1f\n", fibo(n));
}
C version:
#include <stdio.h>
double fibo(double n) {
if (n < 2) return 1.0;
return fibo(n-2) + fibo(n-1);
}
int main(int argc, char ** argv) {
int n = argc == 2 ? atoi(argv[1]) : 1;
printf("%.1f\n", fibo(n));
return 0;
}
The difference (DMD Vs GCC) is visible.
In the case of GCC/DMC you can add the:
-fomit-frame
compilation flag to increase performance, and you can even replace this line:
if (n < 2.0) return 1.0;
With:
if (__builtin_expect(n < 2.0, 0)) return 1.0;
In such situation the runtime timings are (n = 37 on my very old PC) 2.36s Vs 3.11s DMD.
(I don't know how much difficult/work is to add an expect to D).
Bye,
bearophile
More information about the Digitalmars-d
mailing list