UNID 05-200 Chapter 5 Optimizing Program Performance Guobao Jiang(蒋国宝) 11210240049@fudan.edu.cn loveaborn@foxmail.com
Chapter 5 Optimizing Program Performance Guobao Jiang (蒋国宝) 11210240049@fudan.edu.cn loveaborn@foxmail.com
UN/ Problem 5. 1(P381) The following problem illustrates the way memory aliasing(存储器别名使用) can cause unexpected program behavior. Consider the following procedure to swap two values void swap (int *xp, int *yp) Xp 六 ★ Ⅺp+"yp; 六 X+V* yp=xp -ypi 大 x+y-y=X Ⅺ=x-yp y-×=y f this procedure is called with xp equal to yp what effect will it have 2021/10/29
Problem 5.1 (P381) • The following problem illustrates the way memory aliasing (存储器别名使用) can cause unexpected program behavior. Consider the following procedure to swap two values: void swap(int *xp, int *yp) { *xp = *xp + *yp; /* x+y */ *yp = *xp - *yp; /* x+y-y = x */ *xp = *xp - *yp; /* x+y-x = y */ } If this procedure is called with xp equal to yp, what effect will it have ? 2021/10/29 2
UN/ Problem 5.2(P384) Later in this chapter we will take a single function and generate many different variants that preserve the function s behavior but with different performance characteristics. For three of these variants we found that the run times(in clock cycles)can be approximated by y the following functions: Version 1 60+ 35n · Version2136+4n · Version3157+1.25n For what values of n would each version be the fastest of the three? Remember that n wil always be an integer 2021/10/29
Problem 5.2 (P384) • Later in this chapter we will take a single function and generate many different variants that preserve the function’s behavior, but with different performance characteristics. For three of these variants, we found that the run times (in clock cycles) can be approximated by the following functions: • Version 1 60 + 35n • Version 2 136 + 4n • Version 3 157 + 1.25n For what values of n would each version be the fastest of the three ? Remember that n will always be an integer. 2021/10/29 3
UN/ H Problem 5.3(P391 Consider the following functions int min(int x, int y) returnx<y? x: y] int max(int x, int return x<y? y: x void incr(int*xp, int vxp+=v;] nt square(in×) return×*×;} The following three code fragments call these functions 2021/10/29
Problem 5.3 (P391) • Consider the following functions: int min(int x, int y) {return x < y ? x:y;} int max(int x, int y){return x < y ? y:x;} void incr(int *xp, int v){ *xp += v;} int square(int x){return x*x;} • The following three code fragments call these functions: 2021/10/29 4
UN/ ty Problem 5.3+(P391) A. for(i= min(x, y); k<max(, y); incr(&i, 1)) t+= square(i) B for(i= max(x, y)-1; i>=min(x, y): incr (&i, 1) t+= square(i) C. int low min(x,y) int high max(x, y) for (i= low; i< high; incr(&i, 1)) t+= square) 2021/10/29
Problem 5.3+ (P391) A. for(i = min(x, y); i<max(x, y); incr(&i,1)) t += square(i); B. for(i = max(x, y)-1;i>=min(x, y);incr(&i,-1)) t += square(i); C. int low = min(x, y); int high = max(x, y); for (i = low; i < high; incr(&i, 1)) t+= square(i); 2021/10/29 5