【问题标题】:How to measure the rate of popping/pushing per second in a queue?如何测量队列中每秒弹出/推送的速率?
【发布时间】:2014-01-13 00:56:54
【问题描述】:

如果我们假设我们有一个典型的队列实现(带有代表节点的链表、一个计数器、一个推送方法和一个弹出方法),那么衡量到达率和速率的最佳方法是什么新项目离开列表?我需要两个单独的线程,一个两个测量每个速率吗?

欢迎任何伪代码/想法!

(我只是快速写了这个来帮助任何答案。为简单起见省略了模板)

class my_queue{
public:
    struct Node{
        Node* next;
        Node* previous;
        int data;
    }

    Node* head;
    Node* tail;
    int queue_size;

    my_queue(){}

    int pop(){
        Node* old_head = head;
        Node* new_head = old_head->previous;
        new_head->next = null;
        head = new_head;
        int data = old_head->data
        delete old_head;
        queue_size--;
        return data;
    }

    void push(int data){
        Node* new_tail = new Node();
        new_node->data = data;
        Node* old_tail = tail;
        old_tail->previous = new_tail;
        new_node->next = old_tail;
        tail = new_tail;
        queue_size++;
    }

    int getSize(){
        return queue_size;
    }
};

【问题讨论】:

  • 您可以使用提供函数调用计数的分析器来代替修改代码。在给定时间段内使用您要测量的工作负载运行程序。

标签: c++ multithreading c++11 queue chrono


【解决方案1】:

看起来很简单:您只需使用两个原子的 push/pop 计数器,每次操作都会递增。然后,您使用一个每秒运行的线程,记录当前状态(或随心所欲地使用它)并将计数器重置为 0。

您唯一需要注意的是使用一个计时器来计算函数所花费的时间,以确保您不会偏离 1 秒间隔太远(您还应该复制两个计数器如果您花费大量时间处理,请在开始时重置它们。

【讨论】:

  • @MartinJames 反正我只有一个消费者-生产者。
猜你喜欢
  • 2019-05-05
  • 1970-01-01
  • 1970-01-01
  • 2019-03-12
  • 2012-12-31
  • 2022-11-21
  • 1970-01-01
  • 1970-01-01
  • 2017-08-03
相关资源
最近更新 更多