Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //main.cpp
- #include <iostream>
- #include <string>
- #include <vector>
- #include <cstdlib>
- #include <ctime>
- #include <algorithm>
- #include <chrono>
- #include <iomanip>
- #include "NameGenerator.h"
- #define KEY_NOT_FOUND -1
- //versions of search and comparison functions that work with pointers to Items
- int binary_searchP(std::vector<Item *> V, std::string key)
- {
- int min = 0;
- int max = V.size() - 1;
- // continue searching while [imin,imax] is not empty
- while (max >= min)
- {
- // calculate the midpoint for roughly equal partition
- int mid = (max - min) / 2 + min;
- if (V[mid]->name == key)
- // key found at index imid
- return mid;
- // determine which subarray to search
- else if (V[mid]->name < key)
- // change min index to search upper subarray
- min = mid + 1;
- else
- // change max index to search lower subarray
- max = mid - 1;
- }
- // key was not found
- return KEY_NOT_FOUND;
- }
- bool sortByNameP(Item *A, Item *B)
- {
- return (A->name < B->name);
- }
- //versions of search and comparison functions that work with Items
- int binary_search(std::vector<Item> V, std::string key)
- {
- int min = 0;
- int max = V.size() - 1;
- // continue searching while [imin,imax] is not empty
- while (max >= min)
- {
- // calculate the midpoint for roughly equal partition
- int mid = (max - min) / 2 + min;
- if (V[mid].name == key)
- // key found at index imid
- return mid;
- // determine which subarray to search
- else if (V[mid].name < key)
- // change min index to search upper subarray
- min = mid + 1;
- else
- // change max index to search lower subarray
- max = mid - 1;
- }
- // key was not found
- return KEY_NOT_FOUND;
- }
- bool sortByName(Item A, Item B)
- {
- return (A.name < B.name);
- }
- void main()
- {
- std::chrono::time_point<std::chrono::high_resolution_clock> nextTime;
- std::chrono::time_point<std::chrono::high_resolution_clock> startTime;
- long long millisecondsElapsed = 0;
- long long millisecondsElapsedP = 0;
- {
- srand(0); //known seed
- NameGenerator nameGen;
- std::vector<Item> inventory;
- //start timing
- startTime = std::chrono::high_resolution_clock::now();
- std::cout << "Generating inventory list" << std::endl;
- for (int i = 0; i < 1000000; ++i)
- {
- Item I;
- I.name = nameGen.GenerateName();
- I.price = rand() % 1000;
- I.weight = rand() % 10;
- inventory.push_back(I);
- }
- std::cout << inventory.size() << " items generated." << std::endl;
- std::sort(inventory.begin(), inventory.end(), sortByName);
- std::cout << "Inventory sorted..." << std::endl;
- bool done = false;
- while (!done)
- {
- std::string name = nameGen.GenerateName();
- std::cout << "Searching for " << name << std::endl;
- int index = binary_search(inventory, name);
- if (index >= 0)
- {
- done = true;
- std::cout << "Found item at index " << index << std::endl;
- std::cout << inventory[index].name << ", price: " << inventory[index].price << ", weight: " << inventory[index].weight << std::endl;
- }
- else
- {
- std::cout << "Did not find any item named '" << name << "' in the inventory list." << std::endl << std::endl;
- }
- }
- nextTime = std::chrono::high_resolution_clock::now();
- millisecondsElapsed = std::chrono::duration_cast<std::chrono::milliseconds>(nextTime - startTime).count();
- std::cout << std::endl << "Elapsed milliseconds for object version before memory is freed: " << millisecondsElapsed << std::endl;
- }
- nextTime = std::chrono::high_resolution_clock::now();
- millisecondsElapsed = std::chrono::duration_cast<std::chrono::milliseconds>(nextTime - startTime).count();
- std::cout << "Elapsed milliseconds for object version after memory de-allocation: " << millisecondsElapsed << std::endl << std::endl;
- //pointer version
- {
- srand(0); //known seed
- NameGenerator nameGen;
- std::vector<Item *> inventory;
- //start timing
- startTime = std::chrono::high_resolution_clock::now();
- std::cout << "Generating inventory list" << std::endl;
- for (int i = 0; i < 1000000; ++i)
- {
- Item *I = new Item();
- I->name = nameGen.GenerateName();
- I->price = rand() % 1000;
- I->weight = rand() % 10;
- inventory.push_back(I);
- }
- std::cout << inventory.size() << " items generated." << std::endl;
- std::sort(inventory.begin(), inventory.end(), sortByNameP);
- std::cout << "Inventory sorted..." << std::endl;
- bool done = false;
- while (!done)
- {
- std::string name = nameGen.GenerateName();
- std::cout << "Searching for " << name << std::endl;
- int index = binary_searchP(inventory, name);
- if (index >= 0)
- {
- done = true;
- std::cout << "Found item at index " << index << std::endl;
- std::cout << inventory[index]->name << ", price: " << inventory[index]->price << ", weight: " << inventory[index]->weight << std::endl;
- }
- else
- {
- std::cout << "Did not find any item named '" << name << "' in the inventory list." << std::endl << std::endl;
- }
- }
- nextTime = std::chrono::high_resolution_clock::now();
- millisecondsElapsedP = std::chrono::duration_cast<std::chrono::milliseconds>(nextTime - startTime).count();
- std::cout << std::endl << "Elapsed milliseconds for pointer version before memory is freed: " << millisecondsElapsedP << std::endl;
- //memory cleanup
- for (unsigned int i = 0; i < inventory.size(); ++i)
- {
- delete inventory[i];
- }
- nextTime = std::chrono::high_resolution_clock::now();
- millisecondsElapsedP = std::chrono::duration_cast<std::chrono::milliseconds>(nextTime - startTime).count();
- std::cout << "Elapsed milliseconds for pointer version after memory de-allocation: " << millisecondsElapsedP << std::endl << std::endl;
- }
- float speedup = static_cast<float>(millisecondsElapsed) / static_cast<float>(millisecondsElapsedP);
- std::cout << "Speed increase of pointer version over object version: " << std::setprecision(2) << speedup << "x" << std::endl;
- }
- //NameGenerator.h
- #pragma once
- #include <vector>
- #include <string>
- class NameGenerator
- {
- public:
- std::vector<std::string> attribute1;
- std::vector<std::string> colour;
- std::vector<std::string> itemtype;
- std::vector<std::string> attribute2;
- NameGenerator();
- std::string GenerateName();
- };
- class Item
- {
- public:
- std::string name;
- int weight;
- int price;
- };
- //NameGenerator.cpp
- #include "NameGenerator.h"
- NameGenerator::NameGenerator()
- {
- attribute1.push_back("Princely");
- attribute1.push_back("Kingly");
- attribute1.push_back("Drippy");
- attribute1.push_back("Slathering Great Huge");
- attribute1.push_back("Tiny");
- attribute1.push_back("Doom-laden");
- attribute1.push_back("Freakishly Small");
- attribute1.push_back("Lady-like");
- colour.push_back("Red");
- colour.push_back("Green");
- colour.push_back("Rusty");
- colour.push_back("Pink-Laced");
- colour.push_back("Polkadot");
- colour.push_back("Shimmering");
- colour.push_back("Glowing");
- colour.push_back("Plain");
- itemtype.push_back("Dagger");
- itemtype.push_back("Greatsword");
- itemtype.push_back("Matchbook");
- itemtype.push_back("10 foot Pole");
- itemtype.push_back("50 foot Rope");
- itemtype.push_back("Elven Arrow");
- itemtype.push_back("Hammer");
- itemtype.push_back("Frying Pan");
- itemtype.push_back("Bed Roll");
- attribute2.push_back("of Doom");
- attribute2.push_back("of Enlightenment");
- attribute2.push_back("of Sexiness");
- attribute2.push_back("of Pulchritude");
- attribute2.push_back("of Vacuming");
- attribute2.push_back("with Tassles and Dangly Bits");
- attribute2.push_back("that Groans Menacingly");
- attribute2.push_back("of Antioch");
- }
- std::string NameGenerator::GenerateName()
- {
- std::string name = attribute1[rand() % attribute1.size()] +
- " " + colour[rand() % colour.size()] +
- " " + itemtype[rand() % itemtype.size()] +
- " " + attribute2[rand() % attribute2.size()];
- return name;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement