Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working? Look here: #61

Open
perara opened this issue Apr 10, 2023 · 0 comments
Open

Not working? Look here: #61

perara opened this issue Apr 10, 2023 · 0 comments

Comments

@perara
Copy link

perara commented Apr 10, 2023

#include <iostream>
#include <iomanip>
#include <chrono>
#include <iterator>

class tqdm {
public:
    class iterator {
    public:
        using iterator_category = std::input_iterator_tag;
        using value_type = int;
        using difference_type = int;
        using pointer = int*;
        using reference = int&;

        iterator(tqdm* parent, int current)
            : parent_(parent), current_(current) {}

        int operator*() const { return current_; }
        iterator& operator++() {
            ++current_;
            parent_->update();
            return *this;
        }

        bool operator!=(const iterator& other) const {
            return current_ != other.current_;
        }

    private:
        tqdm* parent_;
        int current_;
    };

    tqdm(int total_iterations, int report_every = 1000)
        : total_iterations_(total_iterations),
          report_every_(report_every),
          start_time_(std::chrono::steady_clock::now()) {}

    iterator begin() {
        return iterator(this, 0);
    }

    iterator end() {
        return iterator(this, total_iterations_);
    }

    void update() {
        ++current_iteration_;
        if (current_iteration_ % report_every_ == 0) {
            display_progress_bar();
        }
    }

private:
    int total_iterations_;
    int report_every_;
    int current_iteration_ = 0;
    std::chrono::steady_clock::time_point start_time_;

    void display_progress_bar() {
        float progress = float(current_iteration_) / float(total_iterations_);
        auto current_time = std::chrono::steady_clock::now();
        auto elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(current_time - start_time_).count();
        float iterations_per_second = current_iteration_ / (elapsed_time / 1000.0);

        int bar_width = 50;
        std::cout << "[";
        int position = bar_width * progress;
        for (int i = 0; i < bar_width; ++i) {
            if (i < position) std::cout << "=";
            else if (i == position) std::cout << ">";
            else std::cout << " ";
        }

        std::cout << "] " << int(progress * 100.0) << "%  " << iterations_per_second << " iter/s\r";
        std::cout.flush();
    }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant