2024-03-09 15:44:29 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <chrono>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "dynamic-array.h"
|
2024-03-10 12:16:20 +00:00
|
|
|
#include "queue.h"
|
2024-03-09 15:44:29 +00:00
|
|
|
#include "singly-linked-list.h"
|
|
|
|
#include "doubly-linked-list.h"
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* run a simple test
|
|
|
|
*/
|
2024-03-10 12:16:20 +00:00
|
|
|
void test_sequence(seq::Queue* sqn, int n, int m, std::ostream* os)
|
2024-03-09 15:44:29 +00:00
|
|
|
{
|
|
|
|
assert((m > 0) && (n > m));
|
2024-03-10 12:16:20 +00:00
|
|
|
if(os) *os << "Enqueue even numbers from 0 to " << 2*(n-1) << ".\n";
|
|
|
|
for(int i = 0; i < n; i++) sqn->enqueue(2*i);
|
2024-03-09 15:44:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* return time measurement in units of seconds
|
|
|
|
*/
|
2024-03-10 12:16:20 +00:00
|
|
|
float test_with_time_measurement(seq::Queue* sqn, int iterations)
|
2024-03-09 15:44:29 +00:00
|
|
|
{
|
|
|
|
int sequence_length = 200001;
|
|
|
|
int deletions = 10;
|
|
|
|
test_sequence(sqn, sequence_length, deletions, &std::cout);
|
|
|
|
|
|
|
|
int log_entries = 10;
|
|
|
|
std::cout << "\nNow repeat the above " << iterations << " times:\n";
|
|
|
|
auto t0 = std::chrono::high_resolution_clock::now();
|
|
|
|
for(int i = 0; i < iterations; i++)
|
|
|
|
{
|
|
|
|
test_sequence(sqn, sequence_length, deletions, nullptr);
|
|
|
|
if((i+1) % (iterations/log_entries) == 0)
|
|
|
|
{
|
|
|
|
std::cout << "\t" << i+1 << "\n";
|
|
|
|
std::cout.flush(); // make sure that status output is shown without delay
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto t1 = std::chrono::high_resolution_clock::now();
|
|
|
|
return 1.0e-06 * std::chrono::duration_cast<std::chrono::microseconds>(t1-t0).count();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
int iterations = 200;
|
2024-03-10 12:16:20 +00:00
|
|
|
/*
|
2024-03-09 15:44:29 +00:00
|
|
|
std::cout << "*** test with dynamic array ***\n";
|
|
|
|
seq::DynamicArray dyna;
|
|
|
|
float dyna_time = test_with_time_measurement(&dyna, iterations);
|
2024-03-10 12:16:20 +00:00
|
|
|
*/
|
2024-03-09 15:44:29 +00:00
|
|
|
std::cout << "\n\n*** test with singly linked list ***\n";
|
|
|
|
seq::SinglyLinkedList sll;
|
|
|
|
float sll_time = test_with_time_measurement(&sll, iterations);
|
2024-03-10 12:42:52 +00:00
|
|
|
|
2024-03-09 15:44:29 +00:00
|
|
|
std::cout << "\n\n*** test with doubly linked list ***\n";
|
|
|
|
seq::DoublyLinkedList dll;
|
|
|
|
float dll_time = test_with_time_measurement(&dll, iterations);
|
2024-03-10 12:42:52 +00:00
|
|
|
|
2024-03-10 12:16:20 +00:00
|
|
|
//std::cout << "\n\nRuntime for dynamic array:\t" << dyna_time << " s\n";
|
2024-03-09 15:44:29 +00:00
|
|
|
std::cout << "Runtime for singly linked list:\t" << sll_time << " s\n";
|
2024-03-10 12:42:52 +00:00
|
|
|
std::cout << "Runtime for doubly linked list:\t" << dll_time << " s\n";
|
2024-03-09 15:44:29 +00:00
|
|
|
}
|