Jump to page: 1 24  
Page
Thread overview
Finding large difference b/w execution time of c++ and D codes for same problem
Feb 12, 2013
Sparsh Mittal
Feb 12, 2013
Sparsh Mittal
Feb 12, 2013
monarch_dodra
Feb 12, 2013
Dmitry Olshansky
Feb 12, 2013
Sparsh Mittal
Feb 12, 2013
Sparsh Mittal
Feb 12, 2013
Dmitry Olshansky
Feb 12, 2013
H. S. Teoh
Feb 12, 2013
Sparsh Mittal
Feb 12, 2013
FG
Feb 12, 2013
Sparsh Mittal
Feb 12, 2013
FG
Feb 13, 2013
Sparsh Mittal
Feb 13, 2013
Rob T
Feb 13, 2013
Marco Leise
Feb 13, 2013
FG
Feb 13, 2013
Marco Leise
Feb 13, 2013
Marco Leise
Feb 13, 2013
Marco Leise
Feb 13, 2013
FG
Feb 13, 2013
Marco Leise
Feb 13, 2013
Marco Leise
Feb 13, 2013
FG
Feb 13, 2013
Marco Leise
Feb 13, 2013
Marco Leise
Feb 13, 2013
jerro
Feb 14, 2013
Sparsh Mittal
February 12, 2013
I am writing Julia sets program in C++ and D; exactly same way as much as possible. On executing I find large difference in their execution time. Can you comment what wrong am I doing or is it expected?


//===============C++ code, compiled with -O3 ==============
#include <sys/time.h>
#include <iostream>
using namespace std;
const  int DIM= 4194304;

struct complexClass {
  float r;
  float i;
  complexClass( float a, float b )
  {
    r = a;
    i = b;
  }


  float squarePlusMag(complexClass another)
  {
    float r1 = r*r - i*i + another.r;
    float i1 = 2.0*i*r + another.i;

    r = r1;
    i = i1;

    return (r1*r1+ i1*i1);
  }
};


int juliaFunction( int x, int y )
{

  complexClass a (x,y);

   complexClass c(-0.8, 0.156);

  int i = 0;

  for (i=0; i<200; i++) {
   if( a.squarePlusMag(c) > 1000)
      return 0;
  }

  return 1;
}


void kernel(  ){
  for (int x=0; x<DIM; x++) {
    for (int y=0; y<DIM; y++) {
      int offset = x + y * DIM;
      int juliaValue = juliaFunction( x, y );
	//juliaValue will be used by some function.
    }
  }
}


int main()
{

  struct timeval start, end;
  gettimeofday(&start, NULL);
  kernel();
  gettimeofday(&end, NULL);
  float delta = ((end.tv_sec  - start.tv_sec) * 1000000u +
         end.tv_usec - start.tv_usec) / 1.e6;


  cout<<" C++ code with dimension " << DIM <<" Total time: "<< delta << "[sec]\n";
}






//=====================D++ code, compiled with -O -release -inline=========

#!/usr/bin/env rdmd
import std.stdio;
import std.datetime;
immutable int DIM= 4194304;


struct complexClass {
  float r;
  float i;

  float squarePlusMag(complexClass another)
  {
    float r1 = r*r - i*i + another.r;
    float i1 = 2.0*i*r + another.i;

    r = r1;
    i = i1;

    return (r1*r1+ i1*i1);
  }
};


int juliaFunction( int x, int y )
{

  complexClass c = complexClass(0.8, 0.156);
  complexClass a= complexClass(x, y);


  for (int i=0; i<200; i++) {

    if( a.squarePlusMag(c) > 1000)
      return 0;
  }
  return 1;
}


void kernel(  ){
  for (int x=0; x<DIM; x++) {
    for (int y=0; y<DIM; y++) {
      int offset = x + y * DIM;
      int juliaValue = juliaFunction( x, y );
      //juliaValue will be used by some function.	
    }
  }
}


void main()
{
  StopWatch sw;
  sw.start();
  kernel();
  sw.stop();
  writeln(" D code serial with dimension ", DIM ," Total time: ", (sw.peek().msecs/1000), "[sec]");
}

//================
I will appreciate any help.
February 12, 2013
I am finding C++ code is much faster than D code.
February 12, 2013
On Tuesday, 12 February 2013 at 20:39:36 UTC, Sparsh Mittal wrote:
> I am finding C++ code is much faster than D code.

dmd (AFAIK) is known to be slower. try LDC or GDC if speed is your major concern.
February 12, 2013
13-Feb-2013 00:39, Sparsh Mittal пишет:
> I am finding C++ code is much faster than D code.

Seems like DMD's floating point issue. The issue being that it always works with floats as full-width reals + rounding. Basically if nothing changed (and I doubt it changed) then  DMD with floating point code is about two (or more) times slower then GDC/LDC.

The cure is using GDC/LDC compiler as they are pretty stable and up to date on the front-end side these days.

-- 
Dmitry Olshansky
February 12, 2013
Pardon me, can you please point me to suitable reference or tell just command here. Searching on google, I could not find anything yet. Performance is my main concern.



February 12, 2013
OK. I found it.

February 12, 2013
On Wed, Feb 13, 2013 at 12:56:01AM +0400, Dmitry Olshansky wrote:
> 13-Feb-2013 00:39, Sparsh Mittal пишет:
> >I am finding C++ code is much faster than D code.
> 
> Seems like DMD's floating point issue. The issue being that it always works with floats as full-width reals + rounding. Basically if nothing changed (and I doubt it changed) then  DMD with floating point code is about two (or more) times slower then GDC/LDC.
> 
> The cure is using GDC/LDC compiler as they are pretty stable and up to date on the front-end side these days.
[...]

I did a few benchmarks somewhat recently where I compared the performance of code produced by GDC with DMD. Code produced by GDC consistently outperforms code produced by DMD by about 20-30% or so. This is across the board, with both floats, reals, and applications that don't do heavy arithmetic (just basic looping/recursion constructs).

I didn't investigate in detail the cause of this difference, but the last time I looked at the assembly code generated by both compilers, I noticed that GDC's optimizer is far more advanced than DMD's, esp. when it comes to loop-unrolling, strength reduction, inlining, etc.. For non-trivial code, GDC pretty much consistently produces superior code in general (not just in floating-point operations).

So if performance is a concern, I'd say definitely look into GDC or LDC instead of DMD.


T

-- 
Two wrongs don't make a right; but three rights do make a left...
February 12, 2013
13-Feb-2013 01:09, Sparsh Mittal пишет:
> Pardon me, can you please point me to suitable reference or tell just
> command here. Searching on google, I could not find anything yet.
> Performance is my main concern.
>
>
>
GDC, seems like its mostly "build from source" kind of thing.
Moved to gitbub:
https://github.com/D-Programming-GDC
(See also newsgroup digitalmars.d.D.gnu)

GDC binaries for Windows TDM-GCC toolchain are still available there:
https://bitbucket.org/goshawk/gdc/downloads

AFAIK it needs 4.6.1 version of TDM toolset.


LDC(2), recent release with binaries.

https://github.com/downloads/ldc-developers/ldc/ldc-0.10.0-src.tar.gz
https://github.com/downloads/ldc-developers/ldc/ldc2-0.10.0-linux-x86_64.tar.gz
https://github.com/downloads/ldc-developers/ldc/ldc2-0.10.0-linux-x86_64.tar.xz
https://github.com/downloads/ldc-developers/ldc/ldc2-0.10.0-linux-x86.tar.gz
https://github.com/downloads/ldc-developers/ldc/ldc2-0.10.0-linux-x86.tar.xz
https://github.com/downloads/ldc-developers/ldc/ldc2-0.10.0-osx-x86_64.tar.gz
https://github.com/downloads/ldc-developers/ldc/ldc2-0.10.0-osx-x86_64.tar.xz 


(See also announce on the newsgroup digitalmars.d.D.ldc)

Both compilers ship dmd-style compiler driver called gdmd or ldmd2.
Speed is mostly what you'd expect of GCC and LLVM respectively.

-- 
Dmitry Olshansky
February 12, 2013
Thanks for your insights. It was very helpful.


February 12, 2013
On 2013-02-12 21:39, Sparsh Mittal wrote:
> I am finding C++ code is much faster than D code.

I had a look, but first had to make juliaValue global, because g++ had optimized all the calculations away. :)  Also changed DIM to 32 * 1024.

13.2s -- g++ -O3
16.0s -- g++ -O2
15.9s -- gdc -O3
15.9s -- gdc -O2
16.2s -- dmd -O -release -inline    (v.2.060)

Winblows and DMD 32-bit, the rest 64-bit, but still, dmd was quite fast.
Interesting how gdc -O3 gave no extra boost vs. -O2.

« First   ‹ Prev
1 2 3 4