Delegate perfomance (outrage was redherring)
Justin Johansson
no at spam.com
Fri Oct 16 00:21:39 PDT 2009
bearophile Wrote:
> Justin Johansson:
>
> > this turns out to be
> > a clear demonstration of the performance-enhancing power of D delegates over an
> > otherwise ingrained C++ thinking approach.
>
> I have changed your benchmark a little, you may want to look at its timings too (I have taken timings with it with DMD and LDC, and the results differ):
>
> version (Tango) {
> import tango.stdc.stdio: printf;
> import tango.stdc.time: CLOCKS_PER_SEC, clock;
> } else {
> import std.c.stdio: printf;
> import std.c.time: CLOCKS_PER_SEC, clock;
> }
>
> double myclock() {
> return clock() / cast(double)CLOCKS_PER_SEC;
> }
>
> abstract class Visitor {
> //interface Visitor { // try this too
> abstract int visit(int x);
> }
>
> final class MyVisitor: Visitor {
> int visit(int x) { return 0; }
> }
>
> struct IntRange {
> int stop;
>
> int forEachDeleg(int delegate(int x) apply) {
> for (int i; i < stop; i++)
> apply(i);
> return 0;
> }
>
> int forEachObj(Visitor v) {
> for (int i; i < stop; i++)
> v.visit(i);
> return 0;
> }
> }
>
> void testD(IntRange s, Visitor v) {
> auto start = myclock();
> s.forEachDeleg(&v.visit);
> auto stop = myclock();
> printf("Using D style delegate callback: %d ms\n", cast(int)((stop - start) * 1000));
> }
>
> void testCpp(IntRange s, Visitor v) {
> auto start = myclock();
> s.forEachObj(v);
> auto stop = myclock();
> printf("Using C++ style virtual callback: %d ms\n", cast(int)((stop - start) * 1000));
> }
>
> void main() {
> auto s = IntRange(400_000_000);
> Visitor v = new MyVisitor();
>
> for (int i; i < 5; i++) {
> testD(s, v);
> testCpp(s, v);
> printf("\n");
> }
> }
>
> (I suggest you to use the _ inside big number literals in D, they avoid few bugs).
>
> (Few days ago I think to have found that interfaces aren't implemented efficiently in LDC. Lindquist has answered he will improve the situation.)
>
> Bye,
> bearophile
Thanks muchly (also the _ tip)
Just ran your code with these results (D1/phobos/linux):
#! /opt/dmd1/linux/bin/rdmd -I/opt/dmd1/src/phobos -L-L/opt/dmd1/linux/lib -release -O
Also added -O switch this time though have no idea what level of optimization that does.
(btw. In this test code, the -release switch doesn't do anything does it
as that's just for conditional compilation?)
A. abstract class Visitor version :-
Using D style delegate callback: 2720 ms
Using C++ style virtual callback: 2249 ms
Using D style delegate callback: 2560 ms
Using C++ style virtual callback: 2259 ms
Using D style delegate callback: 2170 ms
Using C++ style virtual callback: 2259 ms
Using D style delegate callback: 2099 ms
Using C++ style virtual callback: 2259 ms
Using D style delegate callback: 2640 ms
Using C++ style virtual callback: 2250 ms
B. interface Visitor version :-
Using D style delegate callback: 2509 ms
Using C++ style virtual callback: 2500 ms
Using D style delegate callback: 2509 ms
Using C++ style virtual callback: 2500 ms
Using D style delegate callback: 2519 ms
Using C++ style virtual callback: 2510 ms
Using D style delegate callback: 2509 ms
Using C++ style virtual callback: 2500 ms
Using D style delegate callback: 2510 ms
Using C++ style virtual callback: 2500 ms
The results are not clear cut at all this time. So what's going on?
ciao
justin
More information about the Digitalmars-d
mailing list