beautify output
This commit is contained in:
parent
c30264257a
commit
5ac57571eb
@ -1,6 +1,8 @@
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <iomanip> // setw for padleft
|
||||
|
||||
#include "Timing.h"
|
||||
|
||||
Timing* Timing::mInstance = 0;
|
||||
@ -52,19 +54,20 @@ void Timing::stopRecord(const std::string& name) {
|
||||
* Set prettyPrint to true to display mm:ss.ms instead of ms.
|
||||
*/
|
||||
void Timing::print(const bool prettyPrint) const {
|
||||
std::cout << "-----" << std::endl << "Results: " << std::endl << "-----" << std::endl;
|
||||
std::cout << "--------------------" << std::endl << "Results: " << std::endl << "--------------------" << std::endl;
|
||||
|
||||
std::string blub;
|
||||
auto it = mResults.begin();
|
||||
while(it != mResults.end()) {
|
||||
if (prettyPrint) {
|
||||
std::cout << it->first << ": " << parseDate((int) it->second.count()) << std::endl;
|
||||
} else {
|
||||
std::cout << it->first << ": " << it->second.count() << "ms" << std::endl;
|
||||
std::cout << it->first << ": " << std::setfill(' ') << std::setw(10) << std::fixed << std::setprecision(4) << it->second.count() << "ms" << std::endl;
|
||||
}
|
||||
it++;
|
||||
}
|
||||
|
||||
std::cout << "-----" << std::endl;
|
||||
std::cout << "--------------------" << std::endl;
|
||||
}
|
||||
|
||||
/**
|
||||
|
2
common.h
2
common.h
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
// custom swap function
|
||||
// custom swap function for just pointer swapping (check difference)
|
||||
void swap(uint32_t* a, uint32_t* b)
|
||||
{
|
||||
uint32_t temp = *a;
|
||||
|
@ -8,15 +8,14 @@
|
||||
std::vector<uint32_t > readFromFile(const char* filePath)
|
||||
{
|
||||
std::vector<uint32_t > numbers;
|
||||
std::cout << "read file: " << filePath << "..." << std::endl;
|
||||
//std::cout << "read file: " << filePath << "..." << std::endl;
|
||||
std::ifstream in(filePath);
|
||||
if (in.is_open())
|
||||
{
|
||||
std::string line;
|
||||
std::getline(in, line);
|
||||
int count = std::stoi(line);
|
||||
|
||||
std::cout << "total: " << count << " elems" << std::endl;
|
||||
//std::cout << "total: " << count << " elems" << std::endl;
|
||||
|
||||
int idx = 0;
|
||||
while (in.good() && idx < count) {
|
||||
|
99
main.cpp
99
main.cpp
@ -11,85 +11,84 @@
|
||||
|
||||
// TODO:
|
||||
// - combine partition function
|
||||
// - wirth
|
||||
// - fix Median of Medians
|
||||
// - fix randomized select
|
||||
// - use custom swap for just pointer swapping (check difference)
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
// read test values from input file
|
||||
Timing::getInstance()->startRecord("init");
|
||||
std::string type = "init\t\t\t\t\t";
|
||||
Timing::getInstance()->startRecord(type);
|
||||
std::vector<uint32_t > numbers = readFromFile("testdata");
|
||||
std::cout << "just read " << numbers.size() << " values" << std::endl;
|
||||
Timing::getInstance()->stopRecord("init");
|
||||
Timing::getInstance()->stopRecord(type);
|
||||
|
||||
// index of median
|
||||
// index of median is exactly middle for odd datasets, not supported for even :p
|
||||
if (numbers.size() % 2 == 0)
|
||||
{
|
||||
// TODO: just use the next element? calc arithmetic mean?
|
||||
std::cout << "TODO: define how to handle even datasets" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint32_t idxMed = (numbers.size() - 1) / 2;
|
||||
std::cout << "idx median = " << idxMed << " of " << numbers.size() << std::endl;
|
||||
std::cout << "calculating median on index " << idxMed << " of " << numbers.size() << " elems..." << std::endl;
|
||||
|
||||
// vollst<73>ndige Sortierung mit Quicksort und Ausgabe des mittleren Elements
|
||||
Timing::getInstance()->startRecord("quicksort");
|
||||
std::cout << "quicksort median: " << getQuicksortMedian(numbers, idxMed) << std::endl;
|
||||
Timing::getInstance()->stopRecord("quicksort");
|
||||
// create an array from vector for algorithms using array params
|
||||
uint32_t* array = new uint32_t[numbers.size()];
|
||||
|
||||
// using std quicksort for array
|
||||
// TODO: decide how the fuck to create a dynamic size array from vector...
|
||||
//size_t* array = new size_t[numbers.size]; // create and fill new array
|
||||
//size_t* array = &numbers[0]; // just a pointer to first element as array
|
||||
//size_t* array[numbers.size()]; // invalid c++ -> non-constant expression!
|
||||
//std::copy(numbers.begin(), numbers.end(), array);
|
||||
//size_t* tmp = numbers.data(); // c++11 returns pointer to first elem
|
||||
uint32_t* array = new uint32_t[999999]; // create and fill new array
|
||||
std::copy(numbers.begin(), numbers.end(), array);
|
||||
Timing::getInstance()->startRecord("array quicksort");
|
||||
std::qsort(array, numbers.size(), sizeof(uint32_t), compare);
|
||||
std::cout << "array quicksort median: " << array[idxMed] << std::endl;
|
||||
Timing::getInstance()->stopRecord("array quicksort");
|
||||
// vollständige Sortierung mit Quicksort und Ausgabe des mittleren Elements
|
||||
type = "quicksort median\t\t\t";
|
||||
Timing::getInstance()->startRecord(type);
|
||||
std::cout << type << getQuicksortMedian(numbers, idxMed) << std::endl;
|
||||
Timing::getInstance()->stopRecord(type);
|
||||
|
||||
// comparison "quick" sort with std::sort (introsort)
|
||||
std::copy(numbers.begin(), numbers.end(), array);
|
||||
Timing::getInstance()->startRecord("array std sort");
|
||||
std::sort(array, array + numbers.size());
|
||||
std::cout << "array std sort median: " << array[idxMed] << std::endl;
|
||||
Timing::getInstance()->stopRecord("array std sort");
|
||||
// comparison of std::qsort and std::sort
|
||||
// using std quicksort for array
|
||||
type = "array quicksort\t\t\t\t";
|
||||
std::copy(numbers.begin(), numbers.end(), array);
|
||||
Timing::getInstance()->startRecord(type);
|
||||
std::qsort(array, numbers.size(), sizeof(uint32_t), compare);
|
||||
std::cout << type << array[idxMed] << std::endl;
|
||||
Timing::getInstance()->stopRecord(type);
|
||||
|
||||
// comparison "quick" sort with std::sort (introsort)
|
||||
type = "array std sort\t\t\t\t";
|
||||
std::copy(numbers.begin(), numbers.end(), array);
|
||||
Timing::getInstance()->startRecord(type);
|
||||
std::sort(array, array + numbers.size());
|
||||
std::cout << type << array[idxMed] << std::endl;
|
||||
Timing::getInstance()->stopRecord(type);
|
||||
|
||||
// vorgestellter Randomzized - Select rekursiv implementiert
|
||||
Timing::getInstance()->startRecord("randomized select");
|
||||
std::cout << "randomized select: " << getRandomizedSelectMedian(numbers, 0, numbers.size() - 1, idxMed + 1) << std::endl;
|
||||
Timing::getInstance()->stopRecord("randomized select");
|
||||
type = "randomized select\t\t\t";
|
||||
Timing::getInstance()->startRecord(type);
|
||||
std::cout << type << getRandomizedSelectMedian(numbers, 0, numbers.size() - 1, idxMed + 1) << std::endl;
|
||||
Timing::getInstance()->stopRecord(type);
|
||||
|
||||
// ein weiterer Median - Algorithmus aus der Literatur - implemented with std::vector
|
||||
Timing::getInstance()->startRecord("vector median of medians");
|
||||
std::cout << "vector median of medians: " << getMedianOfMedians(numbers, idxMed + 1) << std::endl;
|
||||
Timing::getInstance()->stopRecord("vector median of medians");
|
||||
type = "vector median of medians\t\t";
|
||||
Timing::getInstance()->startRecord(type);
|
||||
std::cout << type << getMedianOfMedians(numbers, idxMed + 1) << std::endl;
|
||||
Timing::getInstance()->stopRecord(type);
|
||||
|
||||
// ein weiterer Median - Algorithmus aus der Literatur - realized with array
|
||||
/*std::copy(numbers.begin(), numbers.end(), array);
|
||||
Timing::getInstance()->startRecord("array median of medians");
|
||||
std::cout << "array median of medians: " << getMedianOfMedians(array, 0, numbers.size() - 1, idxMed + 1) << std::endl;
|
||||
Timing::getInstance()->stopRecord("array median of medians");*/
|
||||
/*type = "array median of medians";
|
||||
std::copy(numbers.begin(), numbers.end(), array);
|
||||
Timing::getInstance()->startRecord(type);
|
||||
std::cout << type << ": \t\t" << getMedianOfMedians(array, 0, numbers.size() - 1, idxMed + 1) << std::endl;
|
||||
Timing::getInstance()->stopRecord(type);*/
|
||||
|
||||
// noch ein ein weiterer Median - Algorithmus weil wir so cool sind
|
||||
std::vector<uint32_t> numbers_wirth(numbers); // Copy because wirth works in-place
|
||||
Timing::getInstance()->startRecord("wirth");
|
||||
std::cout << "wirth kth element: " << getWirthKthSmallest(numbers_wirth, idxMed) << std::endl;
|
||||
Timing::getInstance()->stopRecord("wirth");
|
||||
type = "wirth kth element\t\t\t";
|
||||
Timing::getInstance()->startRecord(type);
|
||||
std::cout << type << getWirthKthSmallest(numbers, idxMed) << std::endl;
|
||||
Timing::getInstance()->stopRecord(type);
|
||||
|
||||
// Verwendung des C++ STL function templates nth_element
|
||||
Timing::getInstance()->startRecord("nth element");
|
||||
type = "nth element\t\t\t\t";
|
||||
Timing::getInstance()->startRecord(type);
|
||||
std::nth_element(numbers.begin(), numbers.begin() + idxMed, numbers.end());
|
||||
std::cout << "nth element: " << numbers[idxMed] << std::endl;
|
||||
Timing::getInstance()->stopRecord("nth element");
|
||||
std::cout << type << numbers[idxMed] << std::endl;
|
||||
Timing::getInstance()->stopRecord(type);
|
||||
|
||||
//Timing::getInstance()->getResults();
|
||||
Timing::getInstance()->print();
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user