📜  程序优化——C编程语言代码示例

📅  最后修改于: 2022-03-11 15:04:42.830000             🧑  作者: Mango

代码示例1
//Computational tasks can be performed in several different ways with 
//varying efficiency. 
//A more efficient version with equivalent functionality is known as a strength reduction. 
//For example, consider the following C code snippet whose intention is to 
//obtain the sum of all integers from 1 to N:
int i, sum = 0;
for (i = 1; i <= N; ++i) {
  sum += i;
}
printf("sum: %d\n", sum);
//This code can (assuming no arithmetic overflow) be rewritten using a mathematical formula like:
int sum = N * (1 + N) / 2;
printf("sum: %d\n", sum);