Created
April 29, 2019 19:19
-
-
Save OldLipe/61f3dd9c2ff3d1bb6d4db936aa9af3ac to your computer and use it in GitHub Desktop.
Exemplo de troca de loop em c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/time.h> | |
double mysecond(void) | |
{ | |
struct timeval tVal; | |
gettimeofday(&tVal,NULL); | |
return (double)tVal.tv_sec + 1.0e-6 * (double)tVal.tv_usec; | |
} | |
/*By adding this name, most Fortran program will be able to use | |
mysecond() as a double precision function */ | |
double mysecond_(void) | |
{ | |
return mysecond(); | |
} | |
//https://programacaodescomplicada.wordpress.com/2012/11/09/aula-64-alocacao-dinamica-pt-6-alocacao-de-matrizes/ | |
double** alocarMatriz(int Linhas,int Colunas){ //Recebe a quantidade de Linhas e Colunas como Parâmetro | |
int i,j; //Variáveis Auxiliares | |
double **m = (double**)malloc(Linhas * sizeof(double*)); //Aloca um Vetor de Ponteiros | |
for (i = 0; i < Linhas; i++){ //Percorre as linhas do Vetor de Ponteiros | |
m[i] = (double*) malloc(Colunas * sizeof(double)); //Aloca um Vetor de Inteiros para cada posição do Vetor de Ponteiros. | |
for (j = 0; j < Colunas; j++){ //Percorre o Vetor de Inteiros atual. | |
m[i][j] = 0; //Inicializa com 0. | |
} | |
} | |
return m; //Retorna o Ponteiro para a Matriz Alocada | |
} | |
int main() | |
{ | |
int m = 4000; | |
int i,j; | |
double **a = alocarMatriz(m, m); | |
double **b = alocarMatriz(m, m); | |
double s,soma,t1,t2; | |
s= 1.1; | |
soma = 0; | |
for (i=1; i<m;i++){ | |
for (j=1; j<m;j++){ | |
if (((j/2)*j) == j){ | |
a[i][j] = 1; | |
} else { | |
a[i][j] = -1; | |
} | |
b[i][j] = 0; | |
} | |
} | |
t1 = mysecond_(); | |
for(i=1;i<m;i++)for(j=1;j<m;j++){ | |
for(j=1;j<m;j++){ | |
b[i][j] = s * a[i][j]; | |
soma = soma + b[i][j]; | |
} | |
} | |
t2 = mysecond_(); | |
printf("Soma= %.20f Tempo = %.20f\n", soma, t2-t1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment