Pella86

Untitled

Aug 9th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.04 KB | None | 0 0
  1. /* This is a program to test rot13 speed */
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <map>
  6. #include <ctime>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. /* define function with standard mapping */
  12. class Rot13 {
  13.  
  14.     map<char, char> myrotdict;
  15.  
  16. public:
  17.     Rot13();
  18.     void convert_inplace(string& mystr);
  19.     string convert(const string& mystr);
  20. };
  21.  
  22. Rot13::Rot13(){
  23.  
  24.     // initialize the mapping
  25.     string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  26.  
  27.     for(int i = 0; i < 26*2; ++i){
  28.  
  29.         int b = (i + 13)%26;
  30.         int idx = (i<26)? b : b + 26;
  31.  
  32.         myrotdict[characters[i]] = characters[idx];
  33.     }
  34. }
  35.  
  36. void Rot13::convert_inplace(string& mystr){
  37.  
  38.     for(unsigned int i = 0; i < mystr.length(); ++i){
  39.         char c = mystr[i];
  40.         if(myrotdict.find(c) != myrotdict.end()){
  41.             char newc = myrotdict[c];
  42.             mystr[i] = newc;
  43.         }
  44.     }
  45. }
  46.  
  47.  
  48. string Rot13::convert(const string& mystr){
  49.     string newstr = mystr;
  50.  
  51.     for(unsigned int i = 0; i < mystr.length(); ++i){
  52.         char c = mystr[i];
  53.         if(myrotdict.find(c) != myrotdict.end()){
  54.             char newc = myrotdict[c];
  55.             newstr[i] = newc;
  56.         }
  57.     }
  58.  
  59.     return newstr;
  60. }
  61.  
  62.  
  63. void bitrot_inplace(string mystr){
  64.     for (unsigned int i = 0; i < mystr.length(); ++i){
  65.         mystr[i] = ((mystr[i] > 0x40 && mystr[i] < 0x4E) || (mystr[i] > 0x60 && mystr[i] < 0x6E)) ? 13 : -13;
  66.     }
  67. }
  68.  
  69. string bitrot(const string&  mystr){
  70.     string newstr = mystr;
  71.     for (unsigned int i = 0; i < mystr.length(); ++i){
  72.         cout << "----" << endl;
  73.         char c = mystr[i];
  74.         cout << c << endl;
  75.         newstr[i] = ((c > 0x40 && c < 0x4E) || (c > 0x60 && c < 0x6E)) ? 13 : -13;
  76.         cout << newstr[i] << endl;
  77.     }
  78.  
  79.     return newstr;
  80. }
  81.  
  82.  
  83.  
  84.  
  85. void bitshiftrot_inplace(string& mystr){
  86.     string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  87.     for (unsigned int i = 0; i < mystr.length(); ++i){
  88.         char c = mystr[i];
  89.         if(characters.find(c) != string::npos){
  90.             c = (((c & 0b11111) + 12) % 26 + 1)+(c & 0b1100000);
  91.             mystr[i] = c;
  92.         }
  93.     }
  94.  
  95. }
  96.  
  97.  
  98. string bitshiftrot(string mystr){
  99.     string newstr;
  100.     for (unsigned int i = 0; i < mystr.length(); ++i){
  101.         char c = mystr[i];
  102.         c = (((c & 0b11111) + 12) % 26 + 1)+(c & 0b1100000);
  103.         newstr[i] = c;
  104.     }
  105.  
  106.     return newstr;
  107. }
  108.  
  109.  
  110. int main()
  111. {
  112.     string teststr = "Hello my dear friends!";
  113.  
  114.     Rot13 myrot = Rot13();
  115.  
  116.     int trials = 10000000;
  117.  
  118.     clock_t begin, end;
  119.     double elapsed;
  120.  
  121.     cout << " Program begins" << endl;
  122.  
  123.     cout << "Test string:" << endl;
  124.  
  125.     cout << teststr << endl;
  126.  
  127.     cout << "In place rotation" << endl;
  128.  
  129.     begin = clock();
  130.  
  131.     for(int i = 0; i < trials; ++i){
  132.         myrot.convert_inplace(teststr);
  133.     }
  134.  
  135.     end = clock();
  136.  
  137.     elapsed = double(end - begin) / CLOCKS_PER_SEC;
  138.  
  139.     cout << "time elapsed: " << elapsed << endl;
  140.  
  141.     cout << teststr << endl;
  142.  
  143.     cout << "Return value" << endl;
  144.  
  145.     begin = clock();
  146.  
  147.     for(int i = 0; i < trials; ++i){
  148.         string newstr = myrot.convert(teststr);
  149.     }
  150.  
  151.     end = clock();
  152.  
  153.     elapsed = double(end - begin) / CLOCKS_PER_SEC;
  154.  
  155.     cout << "time elapsed: " << elapsed << endl;
  156.  
  157.     cout << "Bit rot in place" << endl;
  158.  
  159.     begin = clock();
  160.  
  161.     for(int i = 0; i < trials; ++i){
  162.          bitrot_inplace(teststr);
  163.     }
  164.  
  165.     end = clock();
  166.  
  167.     elapsed = double(end - begin) / CLOCKS_PER_SEC;
  168.  
  169.     cout << "time elapsed: " << elapsed << endl;
  170.  
  171. /*
  172.     cout << "Bit rot" << endl;
  173.  
  174.     cout << bitrot(teststr) << endl;
  175.  
  176. */
  177.     cout << "Bit shift rot in place" << endl;
  178.  
  179.     begin = clock();
  180.  
  181.     for(int i = 0; i < trials; ++i){
  182.         bitshiftrot_inplace(teststr);
  183.     }
  184.  
  185.     end = clock();
  186.  
  187.     elapsed = double(end - begin) / CLOCKS_PER_SEC;
  188.  
  189.     cout << "time elapsed: " << elapsed << endl;
  190.  
  191.  
  192. /*
  193.     cout << "Bit shift rot" << endl;
  194.  
  195.     cout << bitshiftrot(teststr) << endl;
  196. */
  197.  
  198.     return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment