Connecting Tech Pros Worldwide Forums | Help | Site Map

std::complex

Marcin Kalicinski
Guest
 
Posts: n/a
#1: Jul 22 '05
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



John Harrison
Guest
 
Posts: n/a
#2: Jul 22 '05

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


Marcin Kalicinski
Guest
 
Posts: n/a
#3: Jul 22 '05

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