473,473 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
Jul 22 '05 #1
2 3451

"Marcin Kalicinski" <ka****@poczta.onet.pl> wrote in message
news:cn**********@korweta.task.gda.pl...
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.
How have you managed to time that, or is it just a guess?

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?


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
Jul 22 '05 #2
>> 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.
How have you managed to time that, or is it just a guess?


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

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Marc Schellens | last post by:
Does anybody know an easy way to get the atan of a complex number in C++? thanks, marc
4
by: zender | last post by:
Hi, Is it possible to a value separately to the real or imaginary part of a complex number? Any help appreciated, Charlie The following code shows that .real() and .imag() are not lvalues:
9
by: Greg Buchholz | last post by:
/* While writing a C++ version of the Mandelbrot benchmark over at the "The Great Computer Language Shootout"... http://shootout.alioth.debian.org/gp4/benchmark.php?test=mandelbrot&lang=all ...
5
by: ma740988 | last post by:
Trying to determine how to get the max element from a complex sequence. Given: int main() { //typedef std::complex < double > C typedef std::vector < std::complex < double > > complex_vec;...
2
by: PengYu.UT | last post by:
Hi, In FFTW (http://www.fftw.org/), it defines the funciton fftw_malloc to allocate memory properly aligned. However, I only want to use new to allocate memory for std::complex<double>. Can...
4
by: Fredy Halter | last post by:
the following code is not working: std::complex<long doublex(1.,1.); std::complex<long doubleresult(0.,0.); result = 1./x; std::cout << "x = " << x << std::endl; std::cout << "r = " <<...
1
by: perroe | last post by:
Hi I have a array of complex numbers that are stored in a simple double array. This is done since the array is part of an wrapper for an external C library, and the imaginary part of the first...
5
by: jeremit0 | last post by:
I'm trying to sort a vector<complex<double and can't figure it out. I recognize the problem is that there isn't a default operator< for complex data types. I have written my own operator and can...
7
by: huili80 | last post by:
Should complex<T>::real() and imag() return a value or a refernce? What does the standard say about this? I just realized that MSVC2008's implementation returns a value, but in GCC reference is...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.