Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

Program optimization

//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
", sum);
//This code can (assuming no arithmetic overflow) be rewritten using a mathematical formula like:
int sum = N * (1 + N) / 2;
printf("sum: %d
", sum);
Source by en.wikipedia.org #
 
PREVIOUS NEXT
Tagged: #Program #optimization
ADD COMMENT
Topic
Name
9+7 =