0

I am using c++ with QT 5.6. I have simple console application in 2 styles as follows:

//First style

qstring x = “Hi!”;

void func()
{
        QTextStream(stdout) << x;
}

int main()
{
        while (true)
        {
                func_one();
        }
}

//Second style

void func()
{
        QTextStream(stdout) << “Hi!”;
}

int main()
{
        while (true)
        {
                func();
        }
}

Which will stress out the cpu more and therefore have lesser performance there might not be a big difference but when we apply this to large scale such as a server where every 2 seconds a connection is made it makes a situation similar to the loop above and with multiple variables (but not the same variable and data) a little less resource usage can cause great performance improvements with lesser resource usage. So is using variables gives any performance improvements but I will be using the variable only once in my function though the function will be called repetitively or will using variables slows the program as it has to repetitively check the ram for where is the value of “x” stored and then retrieve the data?

Edit 1:

I will not be using the variable again in my code and we can consider that there is no compiler optimizations. @DrDonut the answer in the link you gave also doesn't answer is $array === (array) $array faster than is_array($array) i.e is it a micro-optimization and I am also asking is the second style a micro-optimization or does it harm the performance.

PSP
  • 143
  • 1
  • 2
  • 10
  • 3
    1) Compilers are cleverer than you might think - in the case above, especially if you use `qstring const x`, identical code may very well be produced. 2) Don't worry about performance now. 3) If you must, measure the performance before trying to second-guess what would be better. – Ken Y-N Jun 09 '16 at 04:50
  • It doesn't matter. – Jesper Juhl Jun 09 '16 at 04:52
  • In real life, you don't have the luxury of hard coding a string, you always need to use a variable for changing data. However, you could create a variable for `QTextStream(stdout)` to save it from being constructed each time. – David Ching Jun 09 '16 at 05:13
  • I know this answer is about PHP, but the principle is the same: http://stackoverflow.com/questions/3470990/is-micro-optimization-worth-the-time – DrDonut Jun 09 '16 at 06:07
  • The second version also has to repetitively check the ram for where the value is stored and then retrieve the data. – molbdnilo Jun 09 '16 at 06:20

1 Answers1

5

Your example is bad because of possible compiler optimizations and because it is not clear will you use this variable in different places or it is just a test code which will be thrown out.

But generally you are optimizing in a wrong way. There is no sense to optimize single variable or single function. You should not guess where your program will spend its time, you should first write your program in the way it works and looks OK.

After the program works, if you find its perfomance is bad you should search for bottlenecks - places where program spends a lot of time. They can be found with the help of profilers or in debugger, not by guessing.

When you found them, you need to optimize these critical places.

Read about premature optimization

demonplus
  • 5,613
  • 12
  • 49
  • 68