Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. int rleCpu(char *in, int n, char* symbolsOut, int* countsOut) {
  2.  
  3.     if (n == 0)
  4.         return 0; // nothing to compress!
  5.  
  6.     int outIndex = 0;
  7.     int symbol = in[0];
  8.     int count = 1;
  9.  
  10.     for (int i = 1; i < n; ++i) {
  11.         if (in[i] != symbol) {
  12.             // run is over.
  13.             // So output run.
  14.             symbolsOut[outIndex] = symbol;
  15.             countsOut[outIndex] = count;
  16.             outIndex++;
  17.  
  18.             // and start new run:
  19.             symbol = in[i];
  20.             count = 1;
  21.         }
  22.         else {
  23.             ++count; // run is not over yet.
  24.         }
  25.     }
  26.  
  27.     // output last run.
  28.     symbolsOut[outIndex] = symbol;
  29.     countsOut[outIndex] = count;
  30.     outIndex++;
  31.  
  32.     return outIndex;
  33. }
  34.  
  35.  
  36. //check how long for cpu
  37.     char* symbolsOut;
  38.     int* countsOut;
  39.     std::chrono::time_point<std::chrono::high_resolution_clock> begin, end;
  40.     begin = std::chrono::high_resolution_clock::now();
  41.     symbolsOut = (char*)malloc(N*sizeof(char));
  42.     countsOut = (int*)malloc(N*sizeof(int));
  43.     rleCpu( t, N, symbolsOut, countsOut);
  44.     end = std::chrono::high_resolution_clock::now();
  45.     std::cout << "Time for the CPU:" << std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count() << "ms" << std::endl;
  46.     //
  47.  
  48.  
  49.     std::cout <<std::endl << "*************CPU 60 first OUTPUT CHARACTERS*********************";
  50.     std::cout << std::endl;
  51.     for (int j = 0; j < 60; j++)
  52.         std::cout << symbolsOut[j];
  53.     std::cout << std::endl;
  54.     std::cout << "*************CPU 60 first Number of occurances*********************" << std::endl;
  55.     for (int j = 0; j < 60; j++)
  56.         std::cout << countsOut[j];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement