Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* This is a program to test rot13 speed */
- #include <iostream>
- #include <string>
- #include <map>
- #include <ctime>
- using namespace std;
- /* define function with standard mapping */
- class Rot13 {
- map<char, char> myrotdict;
- public:
- Rot13();
- void convert_inplace(string& mystr);
- string convert(const string& mystr);
- };
- Rot13::Rot13(){
- // initialize the mapping
- string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
- for(int i = 0; i < 26*2; ++i){
- int b = (i + 13)%26;
- int idx = (i<26)? b : b + 26;
- myrotdict[characters[i]] = characters[idx];
- }
- }
- void Rot13::convert_inplace(string& mystr){
- for(unsigned int i = 0; i < mystr.length(); ++i){
- char c = mystr[i];
- if(myrotdict.find(c) != myrotdict.end()){
- char newc = myrotdict[c];
- mystr[i] = newc;
- }
- }
- }
- string Rot13::convert(const string& mystr){
- string newstr = mystr;
- for(unsigned int i = 0; i < mystr.length(); ++i){
- char c = mystr[i];
- if(myrotdict.find(c) != myrotdict.end()){
- char newc = myrotdict[c];
- newstr[i] = newc;
- }
- }
- return newstr;
- }
- void bitrot_inplace(string mystr){
- for (unsigned int i = 0; i < mystr.length(); ++i){
- mystr[i] = ((mystr[i] > 0x40 && mystr[i] < 0x4E) || (mystr[i] > 0x60 && mystr[i] < 0x6E)) ? 13 : -13;
- }
- }
- string bitrot(const string& mystr){
- string newstr = mystr;
- for (unsigned int i = 0; i < mystr.length(); ++i){
- cout << "----" << endl;
- char c = mystr[i];
- cout << c << endl;
- newstr[i] = ((c > 0x40 && c < 0x4E) || (c > 0x60 && c < 0x6E)) ? 13 : -13;
- cout << newstr[i] << endl;
- }
- return newstr;
- }
- void bitshiftrot_inplace(string& mystr){
- string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
- for (unsigned int i = 0; i < mystr.length(); ++i){
- char c = mystr[i];
- if(characters.find(c) != string::npos){
- c = (((c & 0b11111) + 12) % 26 + 1)+(c & 0b1100000);
- mystr[i] = c;
- }
- }
- }
- string bitshiftrot(string mystr){
- string newstr;
- for (unsigned int i = 0; i < mystr.length(); ++i){
- char c = mystr[i];
- c = (((c & 0b11111) + 12) % 26 + 1)+(c & 0b1100000);
- newstr[i] = c;
- }
- return newstr;
- }
- int main()
- {
- string teststr = "Hello my dear friends!";
- Rot13 myrot = Rot13();
- int trials = 10000000;
- clock_t begin, end;
- double elapsed;
- cout << " Program begins" << endl;
- cout << "Test string:" << endl;
- cout << teststr << endl;
- cout << "In place rotation" << endl;
- begin = clock();
- for(int i = 0; i < trials; ++i){
- myrot.convert_inplace(teststr);
- }
- end = clock();
- elapsed = double(end - begin) / CLOCKS_PER_SEC;
- cout << "time elapsed: " << elapsed << endl;
- cout << teststr << endl;
- cout << "Return value" << endl;
- begin = clock();
- for(int i = 0; i < trials; ++i){
- string newstr = myrot.convert(teststr);
- }
- end = clock();
- elapsed = double(end - begin) / CLOCKS_PER_SEC;
- cout << "time elapsed: " << elapsed << endl;
- cout << "Bit rot in place" << endl;
- begin = clock();
- for(int i = 0; i < trials; ++i){
- bitrot_inplace(teststr);
- }
- end = clock();
- elapsed = double(end - begin) / CLOCKS_PER_SEC;
- cout << "time elapsed: " << elapsed << endl;
- /*
- cout << "Bit rot" << endl;
- cout << bitrot(teststr) << endl;
- */
- cout << "Bit shift rot in place" << endl;
- begin = clock();
- for(int i = 0; i < trials; ++i){
- bitshiftrot_inplace(teststr);
- }
- end = clock();
- elapsed = double(end - begin) / CLOCKS_PER_SEC;
- cout << "time elapsed: " << elapsed << endl;
- /*
- cout << "Bit shift rot" << endl;
- cout << bitshiftrot(teststr) << endl;
- */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment