Advertisement
techno-

original

Apr 27th, 2023
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/time.h>
  4.  
  5. #define DEBUG 0
  6.  
  7. /* Translation of the DNA bases
  8.    A -> 0
  9.    C -> 1
  10.    G -> 2
  11.    T -> 3
  12.    N -> 4*/
  13.  
  14. #define M  1000000 // Number of sequences
  15. #define N  200  // Number of bases per sequence
  16.  
  17. unsigned int g_seed = 0;
  18.  
  19. int fast_rand(void) {
  20.     g_seed = (214013*g_seed+2531011);
  21.     return (g_seed>>16) % 5;
  22. }
  23.  
  24. // The distance between two bases
  25. int base_distance(int base1, int base2){
  26.  
  27.   if((base1 == 4) || (base2 == 4)){
  28.     return 3;
  29.   }
  30.  
  31.   if(base1 == base2) {
  32.     return 0;
  33.   }
  34.  
  35.   if((base1 == 0) && (base2 == 3)) {
  36.     return 1;
  37.   }
  38.  
  39.   if((base2 == 0) && (base1 == 3)) {
  40.     return 1;
  41.   }
  42.  
  43.   if((base1 == 1) && (base2 == 2)) {
  44.     return 1;
  45.   }
  46.  
  47.   if((base2 == 2) && (base1 == 1)) {
  48.     return 1;
  49.   }
  50.  
  51.   return 2;
  52. }
  53.  
  54. int main(int argc, char *argv[] ) {
  55.  
  56.   int i, j;
  57.   int *data1, *data2;
  58.   int *result;
  59.   struct timeval  tv1, tv2;
  60.  
  61.   data1 = (int *) malloc(M*N*sizeof(int));
  62.   data2 = (int *) malloc(M*N*sizeof(int));
  63.   result = (int *) malloc(M*sizeof(int));
  64.  
  65.   /* Initialize Matrices */
  66.   for(i=0;i<M;i++) {
  67.     for(j=0;j<N;j++) {
  68.       /* random with 20% gap proportion */
  69.       data1[i*N+j] = fast_rand();
  70.       data2[i*N+j] = fast_rand();
  71.     }
  72.   }
  73.  
  74.   gettimeofday(&tv1, NULL);
  75.  
  76.   for(i=0;i<M;i++) {
  77.     result[i]=0;
  78.     for(j=0;j<N;j++) {
  79.       result[i] += base_distance(data1[i*N+j], data2[i*N+j]);
  80.     }
  81.   }
  82.  
  83.   gettimeofday(&tv2, NULL);
  84.    
  85.   int microseconds = (tv2.tv_usec - tv1.tv_usec)+ 1000000 * (tv2.tv_sec - tv1.tv_sec);
  86.  
  87.   /* Display result */
  88.   if (DEBUG == 1) {
  89.     int checksum = 0;
  90.     for(i=0;i<M;i++) {
  91.       checksum += result[i];
  92.     }
  93.     printf("Checksum: %d\n ", checksum);
  94.   } else if (DEBUG == 2) {
  95.     for(i=0;i<M;i++) {
  96.       printf(" %d \t ",result[i]);
  97.     }
  98.   } else {
  99.     printf ("Time (seconds) = %lf\n", (double) microseconds/1E6);
  100.   }    
  101.  
  102.   free(data1); free(data2); free(result);
  103.  
  104.   return 0;
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement