// ---------------------------------------------------------------------------- void performanceTest() { clock_t start = clock(); clock_t elapsed = start; double min_ratio = 99999.9, max_ratio = 0; int size_at_min_ratio = 0, size_at_max_ratio = 0; while ((elapsed - start) < 5 * CLOCKS_PER_SEC) { int size = rand() % 10000 + 1000; std::vector<int> array; array.reserve(size); SortedLinkedList list; list.init(); for (int i = 0; i < size; ++i) { int value = rand() % 1000; array.push_back(value); } clock_t sort_start = clock(); for (int i = 0; i < size; ++i) { list.add(array[i]); } clock_t list_sort_elapsed = clock(); std::sort(array.begin(), array.end()); clock_t array_sort_elapsed = clock(); list.destroy(); double array_time = static_cast<double>(array_sort_elapsed - list_sort_elapsed) / CLOCKS_PER_SEC; double list_time = static_cast<double>(list_sort_elapsed - sort_start) / CLOCKS_PER_SEC; double ratio = list_time / array_time; INF("SORT size [%i]: array [%.8lf], list [%.8lf], ratio [%.8lf]", size, array_time, list_time, ratio); if (ratio > max_ratio) { max_ratio = ratio; size_at_max_ratio = size; } if (ratio < min_ratio) { min_ratio = ratio; size_at_min_ratio = size; } elapsed = clock(); } WRN("Max ratio list / array [%.8lf] at size [%i]", max_ratio, size_at_max_ratio); DBG("Min ratio list / array [%.8lf] at size [%i]", min_ratio, size_at_min_ratio); }
int main(int argc, const char * argv[]) { cout << "Beginning the testing with SortedArrayList..." << endl << endl; SortedArrayList * a = new SortedArrayList(); insertAllWords("random.txt", a); cout << "WERE ALL WORDS FOUND?: " << findAllWords("words.txt", a) << endl; removeAllWords("random.txt", a); cout << endl << endl << "Beginning the testing with SortedLinkedList..." << endl << endl; SortedLinkedList * b = new SortedLinkedList(); b->insert("zygote"); insertAllWords("random.txt", b); cout << "WERE ALL WORDS FOUND?: " << findAllWords("words.txt", b) << endl; removeAllWords("random.txt", b); return 0; }
bool batch_test(int test) { size_t size = 1402; size_t capacity = 45500; string * buf = new string[size]; ifstream in("sorted_short.txt"); for(size_t i = 0; i != size; ++i) in >> buf[i]; in.close(); // array insert if(test == 46) { SortedList * list = new SortedArrayList; list->batch(&SortedList::insert, string("random_short.txt")); SortedArrayList * p = static_cast<SortedArrayList *>(list); for(size_t i = 0; i != size; ++i) if((*p)[i] != buf[i]) return false; } // array remove else if(test == 47) { SortedList * list = new SortedArrayList; list->batch(&SortedList::remove, string("random_short.txt")); SortedArrayList * p = static_cast<SortedArrayList *>(list); if(p->size_cassej() != 0) return false; } // linked-list insert else if(test == 48) { SortedList * list = new SortedLinkedList; list->batch(&SortedList::insert, string("random_short.txt")); SortedLinkedList * p = static_cast<SortedLinkedList *>(list); for(size_t i = 0; i != size; ++i) if((*p)[i] != buf[i]) return false; } // linked-list remove else if(test == 49) { SortedList * list = new SortedLinkedList; list->batch(&SortedList::remove, string("random_short.txt")); SortedLinkedList * p = static_cast<SortedLinkedList *>(list); if(p->size_cassej() != 0) return false; } else exit(1); return true; }
void removeAllWords(SortedLinkedList &A, char *file) //O(n^2) { ifstream in; in.open(file); Timer t; string word; double etime; t.start(); while(in>>word) { A.remove(word); } t.elapsedUserTime(etime); cout<<etime<<endl; }
void testSLL(){ SortedLinkedList * a = new SortedLinkedList(); a->insert("zxc"); a->insert("cat"); a->insert("dog"); a->insert("sup"); a->insert("mom"); a->insert("apple"); a->insert("moma"); a->insert("zz"); a->print(); cout << endl << endl << "REMOVED AFTER THIS :" << endl << endl; a->remove("zxc"); a->remove("apple"); a->remove("sup"); a->remove("zz"); a->print(); }