www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 255] New: Odd performance difference w/ complex doubles.

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=255

           Summary: Odd performance difference w/ complex doubles.
           Product: D
           Version: 0.162
          Platform: PC
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: godaves yahoo.com


Version foo runs 5x slower depending on which scope the complex double
variables are declared in.

import std.stdio, std.string;
void main(char[][] args)
{
  char  bit_num = 0, byte_acc = 0;
  const int iter = 50;
  const double lim = 2.0 * 2.0;
version(foo)
{
  cdouble Z, C;
}

  int n = atoi(args[1]);

  writefln("P4\n%d %d",n,n);

  for(int y=0; y<n; y++)
    for(int x=0; x<n; x++)
    {
version(foo)
{}
else
{
       cdouble Z,C;
}
       Z = 0 + 0i;
       C = 2*cast(double)x/n - 1.5 + 2i*cast(double)y/n - 1i;

       for(int i = 0; i < iter && norm(Z) <= lim; i++)
         Z = Z*Z + C;

       byte_acc = (byte_acc << 1) | ((norm(Z) > lim) ? 0x00:0x01);

       bit_num++;
       if(bit_num == 8)
       {
           putc(byte_acc,stdout);
           bit_num = byte_acc = 0;
       }
       else if(x == n-1)
       {
           byte_acc  <<= (8-n%8);
           putc(byte_acc,stdout);
           bit_num = byte_acc = 0;
       }
    }
}

double norm(cdouble C)
{
    return C.re*C.re + C.im*C.im;
}


-- 
Jul 16 2006
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=255





------- Comment #1 from jpelcis gmail.com  2006-07-16 21:11 -------
I used the following code to time it:

long getCount () {
        asm {
                naked;
                rdtsc;
                ret;
        }
}

and had the first and last lines of main check the time.  I got a performance
difference, but it was under 10%.  What parameter were you passing and how were
you testing the time?


-- 
Jul 16 2006
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=255





------- Comment #2 from godaves yahoo.com  2006-07-16 22:53 -------
I timed it (the exact code posted) on Linux/P4 and Win32/AMD64 using an
external timer in both cases, and in both cases the difference was as large
over several runs, about 5x. I was using 1000 as n.

When I added your timer function the difference virtually disappeared (with it
running at the better time once your internal timer was added).

Sure looks like an alignment issue, which can be transitive and sensitive to
seemingly unrelated changes in the code elsewhere (which makes it all the more
frustrating). I've run into this with math.pow() too.


-- 
Jul 16 2006
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=255





------- Comment #3 from baryluk mpi.int.pl  2007-06-21 11:31 -------
Timings (n=1000, Athlon 1GHz) of code from comment #0 (zero changes)

dmd-1.015: 1.95 sec
dmd-1.015 -version=foo: 2.03 sec

dmd-1.015 -O -inline: 1.03 sec
dmd-1.015 -O -inline -version=foo: 1.60 sec

using bash builtin function time. optimised version without foo is better
becouse local variables can be properly alligned on the stack and access to
them can be optimised.


-- 
Jun 21 2007
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=255


Andrei Alexandrescu <andrei metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |andrei metalanguage.com
         AssignedTo|nobody puremagic.com        |bugzilla digitalmars.com


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 26 2010