I have to use a very simple data structure consisting only two double type data members (like complex numbers). I have written a class because it is practical to handle that object, and it is also usefull for me to overload all overloadable operators and define constructors and friend functions and so on. In my computation I have to use lots of objects from this class. How can I speed up? What is important to care with?

For example define as little member functions as I can? or it isnt important? for example define operator overloading as a friend and not as a member function? and try to avoidoverloading += which hs to be a membr function?

Or define as little friend functions as I can? or it isnt important?

Or declare as litte object in my programme as I can, change values o an existing object instead of creating a new one?

Creating two double number takes how much less time than creating a class with these two double and lots of functions? Or the functions are created only once during the programme?

To my knowledge the memory requirements for an object of the class requires memory for the member variables plus memory for a pointer (the this pointer). No memory is required for member functions or friend functions, etc of an object. Member functions are kept in the class declaration/definition. Ideally the class isn't declared more than once in program so ideally there is only one copy of each member function.

I wouldn't bother worrying too much about speed unless you can prove that your code is slow enough to justify optimizing.

Edited 7 Years Ago by Narue: n/a

I do not care about memory just the speed! But you say that functions do not slow down anything?

>I do not care about memory just the speed!
Memory usage can affect speed. If you use too much memory you can cross cache lines, or even cause thrashing. However, caring about something when there's no reason to care is stupid. We get people occasionally who want to do stuff like micro-optimize arithmetic when the program is I/O bound. No amount of tweaking instructions will have a noticeable effect when the program spends 99% of its time reading and writing files. Your case strikes me as very similar.

If you can't prove that your code is slow, and that changing how your classes are structured will drastically improve matters, I'm inclined to lump you with the rest of those goofballs who are optimization happy without a clue.

I was working on a reply similar to Narue's but she says it so much better I threw mine away.

But she's right.

Generally the rule is first, make it work, then if necessary make it work fast.

This isn't a license to do obviously stupid things, but if you've made a reasonable attempt at algorithm selection, you're not likely to have problems.

As an example that runs counter to what you're talking about here, I once sped up a section of code by adding more methods and data. I only did it after profiling the code and identifying an area where we were spending too much time. We examined what the code was actually doing and added a new mechanism that allowed us to use a MUCH more efficient algorithm in that section. The point being that the optimization was not made until the code was working and demonstrated to be slow.

My program does pure calculations, no IO.
I have already solved the problem once
(it is not a short calculation),
and now i would like to improve:
doing the same calculation but registering errors,
it has to be about twice longer, because twice more arithmetic,
but using classes (handling values and errors together in a struct)
increase the time more (more...) than twice! I've tested.

The class what I use is soething like that:

typedef double (*double_fp1)(double);
typedef double (*double_fp2)(double,double);
typedef double (*double_fp3)(double,double,double);

class real
{
  double value;
  double error;
public:
  real();
  real(double);
  real(double,double);
  operator double();
  void set_value(const double);
  void set_error(const double);
  double get_value();
  double get_error();
  void set(const double,const double);
  void get(double,double);
  real& operator+=(const int);
  real& operator-=(const int);
  real& operator*=(const int);
  real& operator/=(const int);
  real& operator+=(const double);
  real& operator-=(const double);
  real& operator*=(const double);
  real& operator/=(const double);
  real& operator+=(const real&);
  real& operator-=(const real&);
  real& operator*=(const real&);
  real& operator/=(const real&);
  friend real operator+(const real&);
  friend real operator-(const real&);
  friend real operator+(const real&, const real&);
  friend real operator-(const real&, const real&);
  friend real operator*(const real&, const real&);
  friend real operator/(const real&, const real&);
  friend real operator+(const real&, const double);
  friend real operator-(const real&, const double);
  friend real operator*(const real&, const double);
  friend real operator/(const real&, const double);
  friend real operator+(const double, const real&);
  friend real operator-(const double, const real&);
  friend real operator*(const double, const real&);
  friend real operator/(const double, const real&);
  friend real operator+(const real&, const int);
  friend real operator-(const real&, const int);
  friend real operator*(const real&, const int);
  friend real operator/(const real&, const int);
  friend real operator+(const int, const real&);
  friend real operator-(const int, const real&);
  friend real operator*(const int, const real&);
  friend real operator/(const int, const real&);
  friend bool operator<(const real&, const real&);
  friend bool operator>(const real&, const real&);
  friend bool operator<(const real&, const double);
  friend bool operator>(const real&, const double);
  friend bool operator<(const double, const real&);
  friend bool operator>(const double, const real&);
  friend bool operator<(const real&, const int);
  friend bool operator>(const real&, const int);
  friend bool operator<(const int, const real&);
  friend bool operator>(const int, const real&);
  friend bool operator==(const real&, const real&);
  friend bool operator!=(const real&, const real&);
  friend bool operator==(const real&, const double);
  friend bool operator!=(const real&, const double);
  friend bool operator==(const double, const real&);
  friend bool operator!=(const double, const real&);
  friend bool operator==(const real&, const int);
  friend bool operator!=(const real&, const int);
  friend bool operator==(const int, const real&);
  friend bool operator!=(const int, const real&);
  friend std::ostream& operator<<(std::ostream& stream, const real&);
  friend std::istream& operator>>(std::istream& stream, real&);
  friend real subs(double_fp1 f, const real&);
  friend real subs(double_fp2 f, const real&, const real&);
  friend real subs(double_fp3 f, const real&, const real&, const real&);
  friend real sqrt(const real&);
  friend real sin(const real&);
  friend real cos(const real&);
  friend real exp(const real&);
  friend real log(const real&);
  friend real fabs(const real&);
  friend real pow(const real&, const real&);  
};

Generally the rule is first, make it work, then if necessary make it work fast.

Not Generally, just usually :) I think
I choose c++ becuse Maple and Mathematica was stupid and slow to my problem. I have experience that my program will suffer speed problems, therefore I have to care the speed from the beginning!

If it is too slow you can not tested it sometimes, and sometimes a faster version based on totally different base!

About speed:
for example a double is 8 byte and a pointer is 4 byte on my system.
You say that passing a double by reference is faster.
But as I can imagine if we pass a doube to a function it can imidiately use it,
however if we pass the reference, after that the fuction has to find and read the data before using it. It takes time, or not?

Using c++ classes is going to be a little slower than C functions becuase c++ has a little more overhead to contend with. Also make sure your compiler is optimizing the code to use the computer's math coprocessor for floating point math instead of using an emulator library.

About speed:
for example a double is 8 byte and a pointer is 4 byte on my system.
You say that passing a double by reference is faster.
But as I can imagine if we pass a doube to a function it can imidiately use it,
however if we pass the reference, after that the fuction has to find and read the data before using it. It takes time, or not?

accessing a double passed by value has no speed advantage over a double passed by reference. The compiler generates code in both cases so that it has to load the address of the double before using it. When passed by value the value of the double is not kept in a register -- on Intel processors there are not enough registers to allow the program to do that.

This article has been dead for over six months. Start a new discussion instead.