Sei sulla pagina 1di 11

Primos

Jos Galaviz

int prime1(int n) {
int i;
for (i = 2; i < n; i++)
if (n % i == 0)
return 0;
return 1;
}
int prime2(int n) {
int i;
for (i = 2; i <= root(n);i++)
if (n % i == 0)
return 0;
return 1;
}

int prime3(int n) {
int i, bound;
bound = root(n);
for (i = 2; i <= bound; i++)
if (n % i == 0)
return 0;
return 1;
}
int prime4(int n) {
int i, bound;
if (n % 2 == 0)return (n == 2);
if (n % 3 == 0)return (n == 3);
if (n % 5 == 0)return (n == 5);
bound = root(n);
for (i = 7; i <= bound; i+=2)
if (n % i == 0)
return 0;
return 1;
}

int prime5(int n) {
int i;
if (n % 2 == 0)return (n == 2);
if (n % 3 == 0)return (n == 3);
if (n % 5 == 0)return (n == 5);
for (i = 7; i*i <= n; i+=2)
if (n % i == 0)
return 0;
return 1;
}
int cribaEratostenes(int n) {
unsigned long inicial;
int i, j;
esprimo[0] = esprimo[1] = 0;
for (i = 2; i <= n; esprimo[i++] = 1);
inicial = 0;
do {
for (i = inicial; !esprimo[i] && (i < TAM); i++);
inicial = i;
for (j = i + inicial; j <= n; j += i)
esprimo[j] = 0;
inicial++;
} while (inicial <= n);
return 1;
}

Compilacion

gcc -ansi -Wall primos.c -lm -o primos


Compilacion para profile

gcc -pg -ansi -Wall -O1 primos.c -lm -o primos

Proceso
gcc -pg -ansi -Wall -O1 primos.c -lm -o primos
./primos -1 2> lista
gprof -b primos gmon.out > perf-1.txt
./primos -2 2> lista
gprof -b primos gmon.out > perf-2.txt
./primos -3 2> lista
gprof -b primos gmon.out > perf-3.txt
./primos -4 2> lista
gprof -b primos gmon.out > perf-4.txt
./primos -5 2> lista
gprof -b primos gmon.out > perf-5.txt
./primos -6 2> lista
gprof -b primos gmon.out > perf-6.txt
o bien
gcc -pg -ansi -Wall -O1 primos.c -lm -o primos
./primos -1 2> lista
mv gmon.out gmon.sum
./primos -2 2> lista
gprof -s primos gmon.out gmon.sum
./primos -3 2> lista
gprof -s primos gmon.out gmon.sum
./primos -4 2> lista
gprof -s primos gmon.out gmon.sum
./primos -5 2> lista
gprof -s primos gmon.out gmon.sum
./primos -6 2> lista
gprof -s primos gmon.out gmon.sum
gprof -b primos gmon.sum > performance.txt

Tiempos
Primes: calling function prime1
Elapsed time: 12.140000 sec.
Primes: calling function prime2
Elapsed time: 0.190000 sec.
Primes: calling function prime3
Elapsed time: 0.090000 sec.
Primes: calling function prime4
Elapsed time: 0.050000 sec.
Primes: calling function prime5
Elapsed time: 0.050000 sec.
Primes: calling function prime6
Elapsed time: 0.030000 sec.
Flat profile:

Each sample counts as 0.01 seconds.


% cumulative self self total
time seconds seconds calls us/call us/call name
99.30 12.16 12.16 299999 40.55 40.55 prime1
0.33 12.20 0.04 299999 0.13 0.14 prime3
0.25 12.23 0.03 12988925 0.00 0.00 root
0.25 12.26 0.03 299999 0.10 0.20 prime2
0.16 12.28 0.02 299999 0.07 0.07 prime5
0.08 12.29 0.01 299999 0.03 0.03 prime4
0.08 12.30 0.01 main
0.00 12.30 0.00 1 0.00 0.00
cribaEratostenes

Call graph
granularity: each sample hit covers 2 byte(s) for 0.08% of 12.30
seconds
index % time self children called name
12.16 0.00 299999/299999 main [1]
[2] 98.9 12.16 0.00 299999 prime1 [2]
-----------------------------------------------
0.03 0.03 299999/299999 main [1]
[3] 0.5 0.03 0.03 299999 prime2 [3]
0.03 0.00 12608927/12988925 root [5]
-----------------------------------------------
0.04 0.00 299999/299999 main [1]
[4] 0.3 0.04 0.00 299999 prime3 [4]
0.00 0.00 299999/12988925 root [5]
-----------------------------------------------
0.01 0.00 299999/299999 main [1]
[7] 0.1 0.01 0.00 299999 prime4 [7]
0.00 0.00 79999/12988925 root [5]
Compilacion para coverage

gcc -lgcov -fprofile-arcs -ftest-coverage -ansi -Wall


primos.c -lm -o primos

Luego ejecutamos normalmente y luego:

jose@gwen:~/Programacion/Programas/Primos$ gcov primos.c


File 'primos.c'
Lines executed:38.46% of 78
Creating 'primos.c.gcov'

Y entonces en el archivo primos.c.gcov


299999: 163:int prime1(int n) {
-: 164: int i;
3712928174: 165: for (i = 2; i < n; i++)
3712902177: 166: if (n % i == 0)
274002: 167: return 0;
25997: 168: return 1;
-: 169:}
299999: 149:int prime2(int n) {
-: 150: int i;
12608927: 151: for (i = 2; i <= root(n); i++)
12582930: 152: if (n % i == 0)
274002: 153: return 0;
25997: 154: return 1;
-: 155:}

299999: 134:int prime3(int n) {


-: 135: int i, bound;
299999: 136: bound = root(n);
12608927: 137: for (i = 2; i <= bound; i++)
12582930: 138: if (n % i == 0)
274002: 139: return 0;
25997: 140: return 1;
-: 141:}
299999: 111:int prime4(int n) {
-: 112: int i, bound;
-: 113:
299999: 114: if (n % 2 == 0)
150000: 115: return (n == 2);
149999: 116: if (n % 3 == 0)
50000: 117: return (n == 3);
99999: 118: if (n % 5 == 0)
20000: 119: return (n == 5);
79999: 120: bound = root(n);
5985953: 121: for (i = 7; i <= bound; i+=2)
5959959: 122: if (n % i == 0)
54005: 123: return 0;
25994: 124: return 1;

299999: 87:int prime5(int n) {


-: 88: int i;
-: 89:
299999: 90: if (n % 2 == 0)
150000: 91: return (n == 2);
149999: 92: if (n % 3 == 0)
50000: 93: return (n == 3);
99999: 94: if (n % 5 == 0)
20000: 95: return (n == 5);
5985953: 96: for (i = 7; i*i <= n; i+=2)
5959959: 97: if (n % i == 0)
54005: 98: return 0;
25994: 99: return 1;
1: 61:int cribaEratostenes(int n) {
-: 62: unsigned long inicial;
-: 63: int i, j;
-: 64:
1: 65: esprimo[0] = esprimo[1] = 0;
1: 66: for (i = 2; i <= n; esprimo[i++] = 1);
1: 67: inicial = 0;
-: 68: do {
25998: 69: for (i = inicial; !esprimo[i] && (i<TAM);i++);
25998: 70: inicial = i;
827710: 71: for (j = i + inicial; j <= n; j += i)
801712: 72: esprimo[j] = 0;
25998: 73: inicial++;
25998: 74: } while (inicial <= n);
1: 75: return 1;
-: 76:}

12988925: 42:int root(int n) {


12988925: 43: return (int) sqrt((double) n);
-: 44:}

Potrebbero piacerti anche