Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance 打印简单数学_Performance - Fatal编程技术网

Performance 打印简单数学

Performance 打印简单数学,performance,Performance,我的老师告诉我,打印一个等于答案的变量比打印答案本身更有效、更快。这是真的吗?为什么? int num1 = 5; int num2 = 1; int answer; answer = num1 * num2; System.out.println(num1 * num2); System.out.println(answer); 假设在此之后,答案变量不再使用。我知道差异是可以忽略的,但我不认为有一个新的变量来作为答案有什么意义。在计算机科学中,程序的效率取决于两个因素 时间复杂性

我的老师告诉我,打印一个等于答案的变量比打印答案本身更有效、更快。这是真的吗?为什么?

int num1 = 5;
int num2 = 1;
int answer;

answer = num1 * num2;


System.out.println(num1 * num2);
System.out.println(answer);

假设在此之后,答案变量不再使用。我知道差异是可以忽略的,但我不认为有一个新的变量来作为答案有什么意义。

在计算机科学中,程序的效率取决于两个因素

  • 时间复杂性
  • 空间复杂性
如果在程序中多次使用类似的操作,则最好将结果存储在变量中。(赋值操作比其他操作需要更少的时间和空间)

如果只发生一次,请使用操作


如果你想详细了解计算机科学中算法设计和分析的概念搜索,程序的效率取决于两个因素

  • 时间复杂性
  • 空间复杂性
如果在程序中多次使用类似的操作,则最好将结果存储在变量中。(赋值操作比其他操作需要更少的时间和空间)

如果只发生一次,请使用操作

如果您想详细了解这个概念,请搜索算法的设计和分析