Thread overview | |||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 14, 2008 is D so slow? | ||||
---|---|---|---|---|
| ||||
Hello I wrote 2 almost identical test programs(matrix multiplication). One on C and another on D. And D prorgram was 15 times slower! Was it my mistake or not? Thank you p.s. code: void test (int n) { float[] xs = new float[n*n]; float[] ys = new float[n*n]; for(int i = n-1; i>=0; --i) { xs[i] = 1.0; } for(int i = n-1; i>=0; --i) { ys[i] = 2.0; } float[] zs = new float[n*n]; for (int i=0; i<n; ++i) { for (int j=0; j<n; ++j) { float s = 0.0; for (int k=0; k<n; ++k) { s = s + (xs[k + (i*n)] * ys[j + (k*n)]); } zs[j+ (i*n)] = s; } } delete xs; delete ys; delete zs; } |
June 14, 2008 Re: is D so slow? | ||||
---|---|---|---|---|
| ||||
Posted in reply to baleog | This is a frequently asked question.
What compilers are you comparing? They have different optimization backends. It could be that the C compiler or compiler flags you are using simply perform better than the comparable D compiler and flags.
-[Unknown]
baleog wrote:
> Hello
> I wrote 2 almost identical test programs(matrix multiplication). One on C and another on D. And D prorgram was 15 times slower! Was it my mistake or not?
> Thank you
>
> p.s. code:
> void test (int n) { float[] xs = new float[n*n];
> float[] ys = new float[n*n];
> for(int i = n-1; i>=0; --i) {
> xs[i] = 1.0;
> }
> for(int i = n-1; i>=0; --i) {
> ys[i] = 2.0;
> }
> float[] zs = new float[n*n];
> for (int i=0; i<n; ++i) {
> for (int j=0; j<n; ++j) {
> float s = 0.0;
> for (int k=0; k<n; ++k) {
> s = s + (xs[k + (i*n)] * ys[j + (k*n)]);
> }
> zs[j+ (i*n)] = s;
> }
> }
> delete xs;
> delete ys;
> delete zs;
> }
>
|
June 14, 2008 Re: is D so slow? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Unknown W. Brackets | Unknown W. Brackets Wrote: > What compilers are you comparing? gcc-4.0 with last releases of the gdc and dmd-2 > They have different optimization but gdc uses gcc backend and the test programs (C and D) differs by the couple of bytes.. i used std.gc.disable() too - nothing changed > backends. It could be that the C compiler or compiler flags you are using simply perform better than the comparable D compiler and flags. > > -[Unknown] > > > baleog wrote: > > Hello > > I wrote 2 almost identical test programs(matrix multiplication). One on C and another on D. And D prorgram was 15 times slower! > > Was it my mistake or not? > > Thank you > > > > p.s. code: > > void test (int n) { > > float[] xs = new float[n*n]; > > float[] ys = new float[n*n]; > > for(int i = n-1; i>=0; --i) { > > xs[i] = 1.0; > > } > > for(int i = n-1; i>=0; --i) { > > ys[i] = 2.0; > > } > > float[] zs = new float[n*n]; > > for (int i=0; i<n; ++i) { > > for (int j=0; j<n; ++j) { > > float s = 0.0; > > for (int k=0; k<n; ++k) { > > s = s + (xs[k + (i*n)] * ys[j + (k*n)]); > > } > > zs[j+ (i*n)] = s; > > } > > } > > delete xs; > > delete ys; > > delete zs; > > } > > |
June 15, 2008 Re: is D so slow? | ||||
---|---|---|---|---|
| ||||
Posted in reply to baleog | What about switches? Your program uses arrays; if you have array bounds checks enabled, that could easily account for the difference.
One way to see is dump the assembly (I think there's a utility called dumpobj included with dmd) and compare. Obviously, it's doing something differently - there's nothing instrinsically "slower" about the language for sure.
Also - keep in mind that gdc doesn't take advantage of all the optimizations that gcc is able to provide, at least at this time. A couple of bytes can go a long long way if not optimized right.
-[Unknown]
baleog wrote:
> Unknown W. Brackets Wrote:
>> What compilers are you comparing?
>
> gcc-4.0 with last releases of the gdc and dmd-2
>
>> They have different optimization
>
> but gdc uses gcc backend and the test programs (C and D) differs by the couple of bytes..
> i used std.gc.disable() too - nothing changed
>
>> backends. It could be that the C compiler or compiler flags you are using simply perform better than the comparable D compiler and flags.
>>
>> -[Unknown]
>>
>>
>> baleog wrote:
>>> Hello
>>> I wrote 2 almost identical test programs(matrix multiplication). One on C and another on D. And D prorgram was 15 times slower! Was it my mistake or not?
>>> Thank you
>>>
>>> p.s. code:
>>> void test (int n) { float[] xs = new float[n*n];
>>> float[] ys = new float[n*n];
>>> for(int i = n-1; i>=0; --i) {
>>> xs[i] = 1.0;
>>> }
>>> for(int i = n-1; i>=0; --i) {
>>> ys[i] = 2.0;
>>> }
>>> float[] zs = new float[n*n];
>>> for (int i=0; i<n; ++i) {
>>> for (int j=0; j<n; ++j) {
>>> float s = 0.0;
>>> for (int k=0; k<n; ++k) {
>>> s = s + (xs[k + (i*n)] * ys[j + (k*n)]);
>>> }
>>> zs[j+ (i*n)] = s;
>>> }
>>> }
>>> delete xs;
>>> delete ys;
>>> delete zs;
>>> }
>>>
>
|
June 15, 2008 Re: is D so slow? | ||||
---|---|---|---|---|
| ||||
Posted in reply to baleog | baleog wrote:
> Hello
> I wrote 2 almost identical test programs(matrix multiplication). One on C and another on D. And D prorgram was 15 times slower! Was it my mistake or not?
> Thank you
>
> p.s. code:
> void test (int n) { float[] xs = new float[n*n];
> float[] ys = new float[n*n];
> for(int i = n-1; i>=0; --i) {
> xs[i] = 1.0;
> }
> for(int i = n-1; i>=0; --i) {
> ys[i] = 2.0;
> }
> float[] zs = new float[n*n];
> for (int i=0; i<n; ++i) {
> for (int j=0; j<n; ++j) {
> float s = 0.0;
> for (int k=0; k<n; ++k) {
> s = s + (xs[k + (i*n)] * ys[j + (k*n)]);
> }
> zs[j+ (i*n)] = s;
> }
> }
> delete xs;
> delete ys;
> delete zs;
> }
>
What switches did you use to compile? Not much info you're giving ...
Tomas
|
June 15, 2008 Re: is D so slow? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomas Lindquist Olsen | Tomas Lindquist Olsen Wrote: > What switches did you use to compile? Not much info you're giving ... Ubuntu-6.06 dmd-2.0.14 - 40sec witth n=500 dmd -O -release -inline test.d gdc-0.24 - 32sec gdmd -O -release test.d and gcc-4.0.3 - 1.5sec gcc test.c so gcc without optimization runs 20 times faster than gdc but i can't find how to suppress array bound checking > > Tomas |
June 15, 2008 Re: is D so slow? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tomas Lindquist Olsen | Thank you for your replies! I used malloc instead of new and run time was about 1sec
p.s. i'm sorry for my terrible english
Tomas Lindquist Olsen Wrote:
> baleog wrote:
> > Hello
> > I wrote 2 almost identical test programs(matrix multiplication). One on C and another on D. And D prorgram was 15 times slower!
> > Was it my mistake or not?
> > Thank you
> >
> > p.s. code:
> > void test (int n) {
> > float[] xs = new float[n*n];
> > float[] ys = new float[n*n];
> > for(int i = n-1; i>=0; --i) {
> > xs[i] = 1.0;
> > }
> > for(int i = n-1; i>=0; --i) {
> > ys[i] = 2.0;
> > }
> > float[] zs = new float[n*n];
> > for (int i=0; i<n; ++i) {
> > for (int j=0; j<n; ++j) {
> > float s = 0.0;
> > for (int k=0; k<n; ++k) {
> > s = s + (xs[k + (i*n)] * ys[j + (k*n)]);
> > }
> > zs[j+ (i*n)] = s;
> > }
> > }
> > delete xs;
> > delete ys;
> > delete zs;
> > }
> >
>
> What switches did you use to compile? Not much info you're giving ...
>
> Tomas
|
June 15, 2008 Re: is D so slow? | ||||
---|---|---|---|---|
| ||||
Posted in reply to baleog | "baleog" <maccarka@yahoo.com> wrote in message news:g32umu$11kq$1@digitalmars.com... > Tomas Lindquist Olsen Wrote: > >> What switches did you use to compile? Not much info you're giving ... > > Ubuntu-6.06 > dmd-2.0.14 - 40sec witth n=500 > dmd -O -release -inline test.d > gdc-0.24 - 32sec > gdmd -O -release test.d > and gcc-4.0.3 - 1.5sec > gcc test.c > > so gcc without optimization runs 20 times faster than gdc but i can't find how to suppress array bound checking Array bounds checking is off as long as you specify -release. I don't know if your computer is just really, REALLY slow, but out of curiosity I tried running the D program on my computer. It completes in 1.2 seconds. Also, using malloc/free vs. new/delete shouldn't much matter in this program, because you make all of three allocations, all before any loops. The GC is never going to be called during the program. |
June 15, 2008 Re: is D so slow? | ||||
---|---|---|---|---|
| ||||
Posted in reply to baleog | baleog: I suggest you to show the complete C and the complete D code. > float[] xs = new float[n*n]; With a smarter use of gc.malloc you may avoid clearing items two times... (I presume the optimizer doesn't remove the first cleaning). You can use this from the d.extra module of my libs: import std.gc: gcmalloc = malloc, gcrealloc = realloc, hasNoPointers; T[] NewVoidGCArray(T)(int n) { assert(n > 0, "NewVoidCGArray: n must be > 0."); auto pt = cast(T*)gcmalloc(n * T.sizeof); hasNoPointers(pt); return pt[0 .. n]; } > for(int i = n-1; i>=0; --i) { > xs[i] = 1.0; > } D arrays know this shorter and probably faster syntax: xs[] = 1.0; Bye, bearophile |
Copyright © 1999-2021 by the D Language Foundation