Connecting Tech Pros Worldwide Help | Site Map

std::complex

  #1  
Old July 22nd, 2005, 10:53 PM
Marcin Kalicinski
Guest
 
Posts: n/a
Hi,

I found out that default constructor of std::complex class initializes the
value to (0,0). I wonder why is it so?

Because of this, the code below is about two times slower than it could be
if the default constructor left the values uninitialized.

void f()
{
using namespace std;
complex c[100];
for (int i = 0; i < 100; ++i) c[i] = complex(1, 0);
}

Values of built in types work perfectly without zero initialization, so why
make complex different and cause additional learning trouble?

Best regards,
Marcin


  #2  
Old July 22nd, 2005, 10:53 PM
John Harrison
Guest
 
Posts: n/a

re: std::complex



"Marcin Kalicinski" <kalita@poczta.onet.pl> wrote in message
news:cn9tt5$78q$1@korweta.task.gda.pl...[color=blue]
> Hi,
>
> I found out that default constructor of std::complex class initializes the
> value to (0,0). I wonder why is it so?
>
> Because of this, the code below is about two times slower than it could be
> if the default constructor left the values uninitialized.[/color]

How have you managed to time that, or is it just a guess?
[color=blue]
>
> void f()
> {
> using namespace std;
> complex c[100];
> for (int i = 0; i < 100; ++i) c[i] = complex(1, 0);
> }
>
> Values of built in types work perfectly without zero initialization, so
> why make complex different and cause additional learning trouble?[/color]

Most people would say that uninitialised variables are what causes learning
trouble, not to mention bugs.

How about rewriting your code like this?

#include <vector>
#include <complex>

void f()
{
using namespace std;
vector<complex> c(100, complex(1, 0));
}

john


  #3  
Old July 22nd, 2005, 10:54 PM
Marcin Kalicinski
Guest
 
Posts: n/a

re: std::complex


>> I found out that default constructor of std::complex class initializes[color=blue][color=green]
>> the value to (0,0). I wonder why is it so?
>> Because of this, the code below is about two times slower than it could
>> be if the default constructor left the values uninitialized.[/color][/color]
[color=blue]
>How have you managed to time that, or is it just a guess?[/color]

I have generated an assembly listing on MSVC .NET 2003 all optimizations
enabled. Here's the function:

#include <complex>
std::complex<float> f()
{
std::complex<float> c[100];
for (int i = 0; i < 100; ++i)
c[i] = std::complex<float>(1, 0);
return c[50];
}

Below is a relevant part of assembly listing for the function f. Note the
loop that is created by line #5 (the definition of the array):

; 5 : std::complex<float> c[100];

lea eax, DWORD PTR _c$[esp+800]
mov ecx, 100 ; 00000064H
xor edx, edx
$L11604:
mov DWORD PTR [eax], edx
mov DWORD PTR [eax+4], edx
add eax, 8
sub ecx, 1
jne SHORT $L11604

; 6 : for (int i = 0; i < 100; ++i)

xor eax, eax
mov ecx, 1065353216 ; 3f800000H
$L10133:

; 7 : c[i] = std::complex<float>(1, 0);

mov DWORD PTR _c$[esp+eax*8+800], ecx
mov DWORD PTR _c$[esp+eax*8+804], edx
add eax, 1
cmp eax, 100 ; 00000064H
jl SHORT $L10133


Best regards,
Marcin



Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
about std::complex<>'s real() and imag() huili80@gmail.com answers 7 June 28th, 2008 12:55 PM
std::complex<long double> division Fredy Halter answers 4 January 24th, 2007 02:25 PM
How to overload the new operator for std::complex<double>? PengYu.UT@gmail.com answers 2 November 4th, 2006 07:55 PM
Assignment to std::complex number zender@uci.edu answers 4 July 23rd, 2005 12:18 AM