google benchmark应用

256 篇文章 3 订阅
149 篇文章 2 订阅
#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值