Connecting Tech Pros Worldwide Help | Site Map

std::complex

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 09:53 PM
Marcin Kalicinski
Guest
 
Posts: n/a
Default std::complex

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, 09:53 PM
John Harrison
Guest
 
Posts: n/a
Default 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, 09:54 PM
Marcin Kalicinski
Guest
 
Posts: n/a
Default 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



 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.