Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!isis!csm9a!dhale From: dhale@csm9a.UUCP (Dave Hale) Newsgroups: comp.lang.c++ Subject: Efficiency of complex arithmetic in C++ Keywords: C++ complex Message-ID: <2223@csm9a.UUCP> Date: 31 Jul 90 17:42:28 GMT Organization: Colorado School of Mines Lines: 55 I am using AT&T C++ Release 2.1 for complex arithmetic, but I find the resulting code to be much slower than that from doing complex arithmetic in plain C, the hard way. A simple example is the code below, which fills an array of complex objects. The C++ way is much (more than 3 times) slower than the plain C way. I first observed this slowdown with more complicated, arithmetic expressions involving complex, but then tried progessively simpler expressions until I obtained the example provided below. The output (not shown below) of cfront indicates that the complex(float,float) constructor uses a temporary struct complex variable, instead of building the complex directly in the left-hand-side of the expression. Any suggestions on how to avoid such temporaries or, in general, on how to make complex arithmetic in AT&T C++ more efficient? -------------------------- cut here ------------------------------ // a very simple complex class complex { public: float r,i; // should be private, but may have to get dirty inline complex() {} inline complex(float re, float im=0.0) {r=re; i=im;} }; // a non-member function that works with complex variables void func(int n, float a, float b, complex *c) { for (int i=0; i