Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include <intrin.h>
  7. #include <stdint.h>
  8.  
  9. double *cholesky(double *A, int n) {
  10.     double *L = (double*)calloc(n * n, sizeof(double));
  11.     if (L == NULL)
  12.         exit(EXIT_FAILURE);
  13.  
  14.     for (int i = 0; i < n; i++)
  15.         for (int j = 0; j < (i + 1); j++) {
  16.             double s = 0;
  17.             for (int k = 0; k < j; k++)
  18.                 s += L[i * n + k] * L[j * n + k];
  19.             L[i * n + j] = (i == j) ?
  20.                 sqrt(A[i * n + i] - s) :
  21.                 (1.0 / L[j * n + j] * (A[i * n + j] - s));
  22.         }
  23.  
  24.     return L;
  25. }
  26.  
  27. void show_matrix(double *A, int n) {
  28.     for (int i = 0; i < n; i++) {
  29.         for (int j = 0; j < n; j++)
  30.             printf("%2.5f ", A[i * n + j]);
  31.         printf("\n");
  32.     }
  33. }
  34.  
  35. uint64_t rdtsc() {
  36.     return __rdtsc();
  37. }
  38.  
  39.  
  40. int main() {
  41.  
  42.     using  namespace std;
  43.     /*int n = 3;
  44.     double m1[] = { 25, 15, -5,
  45.         15, 18,  0,
  46.         -5,  0, 11 };
  47.     double *c1 = cholesky(m1, n);
  48.     show_matrix(c1, n);
  49.     printf("\n");
  50.     free(c1);*/
  51.  
  52.     //Inicio da medicao de tempo
  53.     uint64_t uiInicio, uiFim;
  54.     int iTam = 1;
  55.    
  56.    
  57.     //definição da matriz  
  58.             int n = 5;
  59.             double m2[] = { 1, 2, 4, 7, 11,
  60.                     2, 13, 23, 38, 58,
  61.                     4, 23, 77, 122, 182,
  62.                     7, 38, 122, 294, 430,
  63.                     11, 58, 182, 430, 855 };
  64.    
  65.  
  66.  
  67.     uiInicio = rdtsc();
  68.     for (int i = 0; i < iTam; i++) {
  69.         double *c2 = cholesky(m2, n);
  70.         free(c2);
  71.     }
  72.     //show_matrix(c2, n);
  73.    
  74.  
  75.     //Fim da medicao de tempo
  76.     uiFim = rdtsc();
  77.  
  78.     cout << (uiFim - uiInicio) / iTam<<endl;
  79.  
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement