#include <string.h>
#include <iostream>
#include <set>
#include <string>
#include <benchmark/benchmark.h>
#define SAFE_DEL(ptr) { if (ptr) delete ptr; ptr = nullptr; }
template <typename T>
struct LinkNode {
LinkNode() {
next = nullptr;
}
LinkNode(const T &d) : data(d) {
next = nullptr;
}
LinkNode(T &&d) : data(std::move(d)) {
next = nullptr;
}
T data;
LinkNode<T>*next;
};
template <typename T>
class LinkList {
public:
LinkList(size_t cap = 1024) {
max_capacity_ = cap;
try {
headptr_ = new LinkNode<T>();
}
catch (std::exception &e) {
std::cerr << e.what() << std::endl;
exit(-1);
}
}
~LinkList() {
SAFE_DEL(headptr_);
}
inline void set_max_capacity(size_t cap) {
max_capacity_ = cap;
}
void insert(T &&data) {
if (count > max_capacity_) {
std::cerr << "link list up to max capacity = " << max_capacity_ << std::endl;
return;
}
LinkNode<T>*ptr = nullptr;
try {
ptr = new LinkNode<T>(std::forward<T>(data));
}
catch (std::exception &e) {
std::cerr << e.what() << std::endl;
exit(-1);
}
if (nullptr != headptr_->next) {
ptr->next = headptr_->next;
}
else {
ptr->next = headptr_;
}
headptr_->next = ptr;
++count;
}
void show() const {
auto ptr = headptr_->next;
while (ptr != headptr_) {
std::cout << ptr->data << std::endl;
ptr = ptr->next;
}
}
inline bool empty() const {
return nullptr == headptr_ || nullptr == headptr_->next;
}
inline size_t size() const {
return count;
}
inline const LinkNode<T>*header() const {
return headptr_;
}
private:
size_t count = 0;
size_t max_capacity_ = 1024;
LinkNode<T>*headptr_ = nullptr;
};
template<typename T>
bool FindCircleStartElement(const LinkList<T>&yourList, T &element) {
if (true == yourList.empty() || 1 == yourList.size()) {
return false;
}
const LinkNode<T>*slow_ptr = yourList.header()->next;
const LinkNode<T>*fast_ptr = yourList.header()->next->next;
while (slow_ptr != fast_ptr) {
if (nullptr != fast_ptr->next || nullptr != fast_ptr->next->next) {
slow_ptr = slow_ptr->next;
fast_ptr = fast_ptr->next->next;
}
else {
return false;
}
}
fast_ptr = yourList.header();
while (slow_ptr != fast_ptr) {
slow_ptr = slow_ptr->next;
fast_ptr = fast_ptr->next;
}
element = std::move(slow_ptr->data);
return true;
}
template<typename T>
bool FindCircleStartElement1(const LinkList<T>&yourList, T &element) {
if (true == yourList.empty() || 1 == yourList.size()) {
return false;
}
std::set<const LinkNode<T>*> filter;
auto ptr = yourList.header();
while (nullptr != ptr) {
if (end(filter) == filter.find(ptr)) {
filter.insert(ptr);
ptr = ptr->next;
continue;
}
element = std::move(ptr->data);
return true;
}
return false;
}
static void BM_FindCircleStartElement(benchmark::State &state) {
LinkList<int>intlist(1024 * 8);
size_t size = state.range(0);
for (int i = 0;i < size;i++) {
intlist.insert(std::move(i));
}
for (auto _ : state) {
int element = -1;
FindCircleStartElement(intlist, element);
}
state.SetBytesProcessed(size * (uint64_t)(state.iterations()));
}
static void BM_FindCircleStartElement1(benchmark::State &state) {
LinkList<int>intlist(1024 * 8);
size_t size = state.range(0);
for (int i = 0;i < size;i++) {
intlist.insert(std::move(i));
}
for (auto _ : state) {
int element = -1;
FindCircleStartElement1(intlist, element);
}
state.SetBytesProcessed(size * (uint64_t)(state.iterations()));
}
BENCHMARK(BM_FindCircleStartElement)->Iterations(10000)->Arg(8)->Arg(64)->Arg(128)->Arg(512)->Arg(1024)->Arg(2048)->Arg(4196);
BENCHMARK(BM_FindCircleStartElement1)->Iterations(10000)->Arg(8)->Arg(64)->Arg(128)->Arg(512)->Arg(1024)->Arg(2048)->Arg(4196);
BENCHMARK_MAIN();
//g++ -o Test test.cpp -std=c++11 -lpthread -lbenchmark
google benchmark应用
最新推荐文章于 2024-09-13 22:45:09 发布