Профилирование программы из л.Р. №1 Программа
Модифицируем программу для автоматической работы:
#include <math.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
const int count = 500;
float x,er,ec;
int done;
float erf(float x) {
const sqrtpi = 1.7724538,
tol = 1.0E-4;
float x2,sum,sum1,term;
int k,i;
x2 = x*x;
sum=x;
term=x;
for(i=0; term<tol*sum; i++) {
sum1=sum;
term=2.0*term*x2/(1.0+2.0*i);
sum=term+sum1;
}
return (2*sum*exp(-x2)/sqrtpi);
}
float erfc( float x ) {
const sqrtpi = 1.7724538;
float terms = 12;
float x2,u,v,sum ;
int i;
x2=x*x;
v=1.0/(2.0*x2);
u=1.0+v*(terms+1.0);
for(i=terms; i>1; i--) {
sum=1.0+i*v/u;
u=sum;
}
return(exp(-x2)/(x*sum*sqrtpi));
}
void main() {
int k;
done=0;
printf("\n");
srand(11);
for(k=0; k<count; k++) {
srand(pow(k,2));
//printf("Arg? ");
//scanf("%f",&x);
x=(float)(rand()%12);
if( x < 0.0 ) { done = 1; }
else {
if (x==0.0 ) {
er=0.0;
ec=1.0;
}
else {
if( x<1.5) {
er=erf(x);
ec=1.0-er;
}
else {
ec=erfc(x);
er=1.0-ec;
}
}
printf("X= %f",x ," Erf= %f",er,", Erfc= %f",ec);
}
}
}
///------------------------------------------------------------------------
Профилирование
Выполняем те же действия, что и в предыдущих программах:
Результаты профилирования для модуля на C++:
TBS (Time Based Sampling)
Address Line Clockticks Source
1 #include <math.h>
2 #include <conio.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 const int count = 500;
7
8 " float x,er,ec;"
9 int done;
01:0030 10 float erf(float x) {
01:0048 11 " const sqrtpi = 1.7724538,"
01:004F 12 tol = 1.0E-4;
13
14 " float x2,sum,sum1,term, r1,r2;"
15 " int k,i;"
16
01:0056 17 x2 = x*x;
01:005F 18 sum=x;
01:0065 19 term=x;
01:006B 20 for(i=0; term<tol*sum; i++) {
01:008D 21 sum1=sum;
01:0093 22 term=2.0*term*x2/(1.0+2.0*i);
01:00AB 23 sum=term+sum1;
01:00B1 24 }
01:00B3 25 r2=2*sum*exp(-x2);
01:00D8 26 r1=(r2/sqrtpi);
27 return r1;
01:00E1 28 }
01:0130 29 1 float erfc( float x ) {
01:0148 30 const sqrtpi = 1.7724538;
01:014F 31 float terms = 12;
32 " float x2,u,v,sum,r1,r2 ;"
33 int i;
34
01:0156 35 x2=x*x;
01:015F 36 v=1.0/(2.0*x2);
01:016A 37 u=1.0+v*(terms+1.0);
01:017E 38 for(i=terms; i>1; i--) {
01:019A 39 sum=1.0+i*v/u;
01:01AC 40 u=sum;
01:01B2 41 }
01:01B4 42 r2=(x*sum*sqrtpi);
01:01C0 43 r1=(exp(-x2)/r2);
44 return r1;
01:01D9 45 }
46
01:0220 47 void main() {
48 int k;
01:0238 49 done=0;
01:0242 50 srand(11);
01:024C 51 for(k=0; k<count; k++) {
01:026D 52 " srand(pow(k,2));"
01:0293 53 x=(float)(rand()%12);
01:02AC 54 if( x < 0.0 ) { done = 1; }
01:02C3 55 else
56 {
01:02C8 57 if (x==0.0 )
58 {
01:02DB 59 er=0.0;
01:02E5 60 ec=1.0;
61 }
01:02EF 62 else
63 {
01:02F1 64 if( x<1.5)
65 {
01:0304 66 er=erf(x);
01:0319 67 ec=1.0-er;
68 }
01:0325 69 else
70 {
01:0327 71 ec=erfc(x);
01:033B 72 er=-(ec-1.0);
73 }
74 }
75 }
01:0349 76 }
01:034E 77 }
EBS (Event Based Sampling)
Address Line Clockticks Source
1 #include <math.h>
2 #include <conio.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 const int count = 500;
7
8 " float x,er,ec;"
9 int done;
01:0030 10 float erf(float x) {
01:0048 11 " const sqrtpi = 1.7724538,"
01:004F 12 tol = 1.0E-4;
13
14 " float x2,sum,sum1,term, r1,r2;"
15 " int k,i;"
16
01:0056 17 x2 = x*x;
01:005F 18 sum=x;
01:0065 19 term=x;
01:006B 20 for(i=0; term<tol*sum; i++) {
01:008D 21 sum1=sum;
01:0093 22 term=2.0*term*x2/(1.0+2.0*i);
01:00AB 23 sum=term+sum1;
01:00B1 24 }
01:00B3 25 r2=2*sum*exp(-x2);
01:00D8 26 r1=(r2/sqrtpi);
27 return r1;
01:00E1 28 }
01:0130 29 float erfc( float x ) {
01:0148 30 const sqrtpi = 1.7724538;
01:014F 31 float terms = 12;
32 " float x2,u,v,sum,r1,r2 ;"
33 int i;
34
01:0156 35 x2=x*x;
01:015F 36 v=1.0/(2.0*x2);
01:016A 37 u=1.0+v*(terms+1.0);
01:017E 38 1 for(i=terms; i>1; i--) {
01:019A 39 sum=1.0+i*v/u;
01:01AC 40 u=sum;
01:01B2 41 }
01:01B4 42 r2=(x*sum*sqrtpi);
01:01C0 43 r1=(exp(-x2)/r2);
44 return r1;
01:01D9 45 }
46
01:0220 47 void main() {
48 int k;
01:0238 49 done=0;
01:0242 50 srand(11);
01:024C 51 for(k=0; k<count; k++) {
01:026D 52 " srand(pow(k,2));"
01:0293 53 x=(float)(rand()%12);
01:02AC 54 if( x < 0.0 ) { done = 1; }
01:02C3 55 else
56 {
01:02C8 57 if (x==0.0 )
58 {
01:02DB 59 er=0.0;
01:02E5 60 ec=1.0;
61 }
01:02EF 62 else
63 {
01:02F1 64 if( x<1.5)
65 {
01:0304 66 er=erf(x);
01:0319 67 ec=1.0-er;
68 }
01:0325 69 else
70 {
01:0327 71 ec=erfc(x);
01:033B 72 er=-(ec-1.0);
73 }
74 }
75 }
01:0349 76 }
01:034E 77 }
Вывод:
Так как операторы ввода/вывода занимают большую часть времени выполнения программы, они были удалены, а ввод был заменен автоматической генерацией случайных значений.
Для анализа программы было введено зацикливание.
Профилирование исходной программы показывает, что в реализованном алгоритме практически отсутствуют узкие места.
Основное время уходит не вычисление основной функции в цикле (38).