简体   繁体   English

变量会影响性能吗?

[英]Do variables affect performance?

I am using c++ with QT 5.6. 我在QT 5.6中使用c ++。 I have simple console application in 2 styles as follows: 我有2种样式的简单控制台应用程序,如下所示:

//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. 这会给CPU带来更多压力,因此性能会降低,但可能不会有太大的区别,但是当我们将其应用到大规模服务器(例如每2秒建立一次连接的服务器)时,其情况类似于上述循环,并且多个变量(但不是相同的变量和数据),资源使用量少一点可以减少资源使用量,从而大大提高性能。 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? 因此,使用变量可以提高性能,但是我将仅在函数中使用一次变量,尽管该函数将被重复调用,或者使用变量会使程序变慢,因为它必须重复检查ram的“ x”值在哪里。然后存储并检索数据?

Edit 1: 编辑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) ie is it a micro-optimization and I am also asking is the second style a micro-optimization or does it harm the performance. @DrDonut您给出的链接中的答案也没有答案是$array === (array) $arrayis_array($array)快,即它是微优化,我也想问是第二种样式是micro -优化或是否损害性能。

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 了解过早的优化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM