Advertisement
gibgezr

Pointer vs. Object C++ test

Feb 6th, 2015
1,179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.81 KB | None | 0 0
  1. //main.cpp
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <cstdlib>
  6. #include <ctime>
  7. #include <algorithm>
  8. #include <chrono>
  9. #include <iomanip>
  10.  
  11. #include "NameGenerator.h"
  12.  
  13. #define KEY_NOT_FOUND -1
  14.  
  15. //versions of search and comparison functions that work with pointers to Items
  16. int binary_searchP(std::vector<Item *> V, std::string key)
  17. {
  18.     int min = 0;
  19.     int max = V.size() - 1;
  20.     // continue searching while [imin,imax] is not empty
  21.     while (max >= min)
  22.     {
  23.         // calculate the midpoint for roughly equal partition
  24.         int mid = (max - min) / 2 + min;
  25.         if (V[mid]->name == key)
  26.             // key found at index imid
  27.             return mid;
  28.         // determine which subarray to search
  29.         else if (V[mid]->name < key)
  30.             // change min index to search upper subarray
  31.             min = mid + 1;
  32.         else
  33.             // change max index to search lower subarray
  34.             max = mid - 1;
  35.     }
  36.     // key was not found
  37.     return KEY_NOT_FOUND;
  38. }
  39.  
  40.  
  41. bool sortByNameP(Item *A, Item *B)
  42. {
  43.     return (A->name < B->name);
  44. }
  45.  
  46. //versions of search and comparison functions that work with Items
  47. int binary_search(std::vector<Item> V, std::string key)
  48. {
  49.     int min = 0;
  50.     int max = V.size() - 1;
  51.     // continue searching while [imin,imax] is not empty
  52.     while (max >= min)
  53.     {
  54.         // calculate the midpoint for roughly equal partition
  55.         int mid = (max - min) / 2 + min;
  56.         if (V[mid].name == key)
  57.             // key found at index imid
  58.             return mid;
  59.         // determine which subarray to search
  60.         else if (V[mid].name < key)
  61.             // change min index to search upper subarray
  62.             min = mid + 1;
  63.         else
  64.             // change max index to search lower subarray
  65.             max = mid - 1;
  66.     }
  67.     // key was not found
  68.     return KEY_NOT_FOUND;
  69. }
  70.  
  71.  
  72. bool sortByName(Item A, Item B)
  73. {
  74.     return (A.name < B.name);
  75. }
  76.  
  77.  
  78. void main()
  79. {
  80.     std::chrono::time_point<std::chrono::high_resolution_clock> nextTime;
  81.     std::chrono::time_point<std::chrono::high_resolution_clock> startTime;
  82.     long long millisecondsElapsed = 0;
  83.     long long millisecondsElapsedP = 0;
  84.  
  85.     {
  86.         srand(0); //known seed
  87.         NameGenerator nameGen;
  88.         std::vector<Item> inventory;
  89.  
  90.         //start timing
  91.         startTime = std::chrono::high_resolution_clock::now();
  92.  
  93.         std::cout << "Generating inventory list" << std::endl;
  94.         for (int i = 0; i < 1000000; ++i)
  95.         {
  96.             Item I;
  97.             I.name = nameGen.GenerateName();
  98.             I.price = rand() % 1000;
  99.             I.weight = rand() % 10;
  100.             inventory.push_back(I);
  101.         }
  102.  
  103.         std::cout << inventory.size() << " items generated." << std::endl;
  104.  
  105.         std::sort(inventory.begin(), inventory.end(), sortByName);
  106.  
  107.         std::cout << "Inventory sorted..." << std::endl;
  108.  
  109.         bool done = false;
  110.  
  111.         while (!done)
  112.         {
  113.             std::string name = nameGen.GenerateName();
  114.  
  115.             std::cout << "Searching for " << name << std::endl;
  116.             int index = binary_search(inventory, name);
  117.  
  118.             if (index >= 0)
  119.             {
  120.                 done = true;
  121.                 std::cout << "Found item at index " << index << std::endl;
  122.                 std::cout << inventory[index].name << ", price: " << inventory[index].price << ", weight: " << inventory[index].weight << std::endl;
  123.  
  124.             }
  125.             else
  126.             {
  127.                 std::cout << "Did not find any item named '" << name << "' in the inventory list." << std::endl << std::endl;
  128.             }
  129.         }
  130.  
  131.         nextTime = std::chrono::high_resolution_clock::now();
  132.         millisecondsElapsed = std::chrono::duration_cast<std::chrono::milliseconds>(nextTime - startTime).count();
  133.  
  134.         std::cout << std::endl << "Elapsed milliseconds for object version before memory is freed: " << millisecondsElapsed << std::endl;
  135.     }
  136.  
  137.     nextTime = std::chrono::high_resolution_clock::now();
  138.     millisecondsElapsed = std::chrono::duration_cast<std::chrono::milliseconds>(nextTime - startTime).count();
  139.  
  140.     std::cout << "Elapsed milliseconds for object version after memory de-allocation: " << millisecondsElapsed << std::endl << std::endl;
  141.  
  142.     //pointer version
  143.     {
  144.         srand(0); //known seed
  145.         NameGenerator nameGen;
  146.         std::vector<Item *> inventory;
  147.  
  148.         //start timing
  149.         startTime = std::chrono::high_resolution_clock::now();
  150.        
  151.         std::cout << "Generating inventory list" << std::endl;
  152.         for (int i = 0; i < 1000000; ++i)
  153.         {
  154.             Item *I = new Item();
  155.             I->name = nameGen.GenerateName();
  156.             I->price = rand() % 1000;
  157.             I->weight = rand() % 10;
  158.             inventory.push_back(I);
  159.         }
  160.  
  161.         std::cout << inventory.size() << " items generated." << std::endl;
  162.  
  163.         std::sort(inventory.begin(), inventory.end(), sortByNameP);
  164.  
  165.         std::cout << "Inventory sorted..." << std::endl;
  166.  
  167.         bool done = false;
  168.  
  169.         while (!done)
  170.         {
  171.             std::string name = nameGen.GenerateName();
  172.  
  173.             std::cout << "Searching for " << name << std::endl;
  174.             int index = binary_searchP(inventory, name);
  175.  
  176.             if (index >= 0)
  177.             {
  178.                 done = true;
  179.                 std::cout << "Found item at index " << index << std::endl;
  180.                 std::cout << inventory[index]->name << ", price: " << inventory[index]->price << ", weight: " << inventory[index]->weight << std::endl;
  181.  
  182.             }
  183.             else
  184.             {
  185.                 std::cout << "Did not find any item named '" << name << "' in the inventory list." << std::endl << std::endl;
  186.             }
  187.         }
  188.  
  189.         nextTime = std::chrono::high_resolution_clock::now();
  190.         millisecondsElapsedP = std::chrono::duration_cast<std::chrono::milliseconds>(nextTime - startTime).count();
  191.  
  192.         std::cout << std::endl << "Elapsed milliseconds for pointer version before memory is freed: " << millisecondsElapsedP << std::endl;
  193.  
  194.         //memory cleanup
  195.         for (unsigned int i = 0; i < inventory.size(); ++i)
  196.         {
  197.             delete inventory[i];
  198.         }
  199.         nextTime = std::chrono::high_resolution_clock::now();
  200.         millisecondsElapsedP = std::chrono::duration_cast<std::chrono::milliseconds>(nextTime - startTime).count();
  201.  
  202.         std::cout << "Elapsed milliseconds for pointer version after memory de-allocation: " << millisecondsElapsedP << std::endl << std::endl;
  203.     }
  204.  
  205.     float speedup = static_cast<float>(millisecondsElapsed) / static_cast<float>(millisecondsElapsedP);
  206.  
  207.     std::cout << "Speed increase of pointer version over object version: " << std::setprecision(2) << speedup << "x" << std::endl;
  208.    
  209. }
  210.  
  211. //NameGenerator.h
  212. #pragma once
  213. #include <vector>
  214. #include <string>
  215.  
  216. class NameGenerator
  217. {
  218. public:
  219.     std::vector<std::string> attribute1;
  220.     std::vector<std::string> colour;
  221.     std::vector<std::string> itemtype;
  222.     std::vector<std::string> attribute2;
  223.  
  224.     NameGenerator();
  225.     std::string GenerateName();
  226. };
  227.  
  228. class Item
  229. {
  230. public:
  231.     std::string name;
  232.     int weight;
  233.     int price;
  234. };
  235.  
  236. //NameGenerator.cpp
  237. #include "NameGenerator.h"
  238.  
  239. NameGenerator::NameGenerator()
  240. {
  241.     attribute1.push_back("Princely");
  242.     attribute1.push_back("Kingly");
  243.     attribute1.push_back("Drippy");
  244.     attribute1.push_back("Slathering Great Huge");
  245.     attribute1.push_back("Tiny");
  246.     attribute1.push_back("Doom-laden");
  247.     attribute1.push_back("Freakishly Small");
  248.     attribute1.push_back("Lady-like");
  249.  
  250.     colour.push_back("Red");
  251.     colour.push_back("Green");
  252.     colour.push_back("Rusty");
  253.     colour.push_back("Pink-Laced");
  254.     colour.push_back("Polkadot");
  255.     colour.push_back("Shimmering");
  256.     colour.push_back("Glowing");
  257.     colour.push_back("Plain");
  258.  
  259.     itemtype.push_back("Dagger");
  260.     itemtype.push_back("Greatsword");
  261.     itemtype.push_back("Matchbook");
  262.     itemtype.push_back("10 foot Pole");
  263.     itemtype.push_back("50 foot Rope");
  264.     itemtype.push_back("Elven Arrow");
  265.     itemtype.push_back("Hammer");
  266.     itemtype.push_back("Frying Pan");
  267.     itemtype.push_back("Bed Roll");
  268.  
  269.     attribute2.push_back("of Doom");
  270.     attribute2.push_back("of Enlightenment");
  271.     attribute2.push_back("of Sexiness");
  272.     attribute2.push_back("of Pulchritude");
  273.     attribute2.push_back("of Vacuming");
  274.     attribute2.push_back("with Tassles and Dangly Bits");
  275.     attribute2.push_back("that Groans Menacingly");
  276.     attribute2.push_back("of Antioch");
  277.  
  278. }
  279.  
  280. std::string NameGenerator::GenerateName()
  281. {
  282.     std::string name = attribute1[rand() % attribute1.size()] +
  283.         " " + colour[rand() % colour.size()] +
  284.         " " + itemtype[rand() % itemtype.size()] +
  285.         " " + attribute2[rand() % attribute2.size()];
  286.  
  287.     return name;
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement