window下tqdm进度条

原代码是linux下运行,修改后可在window下运行。

#ifndef TQDM_H
#define TQDM_H#include <chrono>
#include <ctime>
#include <numeric>
#include <ios>
#include <string>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <math.h>
#include <algorithm>
#include <io.h>
class tqdm {
private:// time, iteration counters and deques for rate calculationsstd::chrono::time_point<std::chrono::system_clock> t_first = std::chrono::system_clock::now();std::chrono::time_point<std::chrono::system_clock> t_old = std::chrono::system_clock::now();int n_old = 0;std::vector<double> deq_t;std::vector<int> deq_n;int nupdates = 0;int total_ = 0;int period = 1;unsigned int smoothing = 50;bool use_ema = true;float alpha_ema = 0.1;std::vector<const char*> bars = { " ", "▏", "▎", "▍", "▌", "▋", "▊", "▉", "█" };bool in_screen = (system("test $STY") == 0);bool in_tmux = (system("test $TMUX") == 0);bool is_tty = _isatty(1);bool use_colors = true;bool color_transition = true;int width = 40;std::string right_pad = "▏";std::string label = "";void hsv_to_rgb(float h, float s, float v, int& r, int& g, int& b) {if (s < 1e-6) {v *= 255.;r = v; g = v; b = v;}int i = (int)(h*6.0);float f = (h*6.) - i;int p = (int)(255.0*(v*(1. - s)));int q = (int)(255.0*(v*(1. - s * f)));int t = (int)(255.0*(v*(1. - s * (1. - f))));v *= 255;i %= 6;int vi = (int)v;if (i == 0) { r = vi; g = t;  b = p; }else if (i == 1) { r = q;  g = vi; b = p; }else if (i == 2) { r = p;  g = vi; b = t; }else if (i == 3) { r = p;  g = q;  b = vi; }else if (i == 4) { r = t;  g = p;  b = vi; }else if (i == 5) { r = vi; g = p;  b = q; }}public:tqdm() {if (in_screen) {set_theme_basic();color_transition = false;}else if (in_tmux) {color_transition = false;}}void reset() {t_first = std::chrono::system_clock::now();t_old = std::chrono::system_clock::now();n_old = 0;deq_t.clear();deq_n.clear();period = 1;nupdates = 0;total_ = 0;label = "";}void set_theme_line() { bars = { "─", "─", "─", "╾", "╾", "╾", "╾", "━", "═" }; }void set_theme_circle() { bars = { " ", "◓", "◑", "◒", "◐", "◓", "◑", "◒", "#" }; }void set_theme_braille() { bars = { " ", "⡀", "⡄", "⡆", "⡇", "⡏", "⡟", "⡿", "⣿" }; }void set_theme_braille_spin() { bars = { " ", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠇", "⠿" }; }void set_theme_vertical() { bars = { "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█", "█" }; }void set_theme_basic() {bars = { " ", " ", " ", " ", " ", " ", " ", " ", "#" };right_pad = "|";}void set_label(std::string label_) { label = label_; }void disable_colors() {color_transition = false;use_colors = false;}void finish() {progress(total_, total_);printf("\n");fflush(stdout);}void progress(int curr, int tot) {if (is_tty && (curr%period == 0)) {total_ = tot;nupdates++;auto now = std::chrono::system_clock::now();double dt = ((std::chrono::duration<double>)(now - t_old)).count();double dt_tot = ((std::chrono::duration<double>)(now - t_first)).count();int dn = curr - n_old;n_old = curr;t_old = now;if (deq_n.size() >= smoothing) deq_n.erase(deq_n.begin());if (deq_t.size() >= smoothing) deq_t.erase(deq_t.begin());deq_t.push_back(dt);deq_n.push_back(dn);double avgrate = 0.;if (use_ema) {avgrate = deq_n[0] / deq_t[0];for (unsigned int i = 1; i < deq_t.size(); i++) {double r = 1.0*deq_n[i] / deq_t[i];avgrate = alpha_ema * r + (1.0 - alpha_ema)*avgrate;}}else {double dtsum = std::accumulate(deq_t.begin(), deq_t.end(), 0.);int dnsum = std::accumulate(deq_n.begin(), deq_n.end(), 0.);avgrate = dnsum / dtsum;}// learn an appropriate period length to avoid spamming stdout// and slowing down the loop, shoot for ~25Hz and smooth over 3 secondsif (nupdates > 10) {period = (int)(std::min(std::max((1.0 / 25)*curr / dt_tot, 1.0), 5e5));smoothing = 25 * 3;}double peta = (tot - curr) / avgrate;double pct = (double)curr / (tot*0.01);if ((tot - curr) <= period) {pct = 100.0;avgrate = tot / dt_tot;curr = tot;peta = 0;}double fills = ((double)curr / tot * width);int ifills = (int)fills;printf("\015 ");if (use_colors) {if (color_transition) {// red (hue=0) to green (hue=1/3)int r = 255, g = 255, b = 255;hsv_to_rgb(0.0 + 0.01*pct / 3, 0.65, 1.0, r, g, b);printf("\033[38;2;%d;%d;%dm ", r, g, b);}else {printf("\033[32m ");}}for (int i = 0; i < ifills; i++) std::cout << bars[8];if (!in_screen && (curr != tot)) printf("%s", bars[(int)(8.0*(fills - ifills))]);for (int i = 0; i < width - ifills - 1; i++) std::cout << bars[0];printf("%s ", right_pad.c_str());if (use_colors) printf("\033[1m\033[31m");printf("%4.1f%% ", pct);if (use_colors) printf("\033[34m");std::string unit = "Hz";double div = 1.;if (avgrate > 1e6) {unit = "MHz"; div = 1.0e6;}else if (avgrate > 1e3) {unit = "kHz"; div = 1.0e3;}printf("[%4d/%4d | %3.1f %s | %.0fs<%.0fs] ", curr, tot, avgrate / div, unit.c_str(), dt_tot, peta);printf("%s ", label.c_str());if (use_colors) printf("\033[0m\033[32m\033[0m\015 ");if ((tot - curr) > period) fflush(stdout);}}
};
#endif

主函数: 

#include "MyClass.h"
#include <windows.h>
int main() {int N = 2000;tqdm bar;std::cout << "Overhead of loop only:" << std::endl;for (int i = 0; i < 100000000; i++) {bar.progress(i, 100000000);}bar.finish();std::cout << "Basic:" << std::endl;bar.reset();bar.set_theme_basic();for (int i = 0; i < N; i++) {bar.progress(i, N);Sleep(1000);}bar.finish();std::cout << "Braille:" << std::endl;bar.reset();bar.set_theme_braille();for (int i = 0; i < N; i++) {bar.progress(i, N);Sleep(300);}bar.finish();std::cout << "Line:" << std::endl;bar.reset();bar.set_theme_line();for (int i = 0; i < N; i++) {bar.progress(i, N);Sleep(300);}bar.finish();std::cout << "Circles:" << std::endl;bar.reset();bar.set_theme_circle();for (int i = 0; i < N; i++) {bar.progress(i, N);Sleep(300);}bar.finish();bar.reset();std::cout << "Vertical bars:" << std::endl;bar.reset();bar.set_theme_vertical();for (int i = 0; i < N; i++) {bar.progress(i, N);Sleep(3000);}bar.finish();return 0;
}

源码 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/45712.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

CollectionUtils的使用

1、非空判断 判断集合是否为空 List<String>对象list&#xff0c;可以使用CollectionUtils中的isEmpty方法来判断list是否为空。代码如下 List<String> list new ArrayList<>(); boolean isEmpty CollectionUtils.isEmpty(list); System.out.println(is…

numpy实现sigmoid函数

numpy实现sigmoid函数 在Python中&#xff0c;可以使用NumPy库的numpy.exp函数来计算e的指数&#xff0c;然后通过除法将其映射到0和1之间&#xff0c;实现sigmoid函数。以下是实现sigmoid函数的示例代码&#xff1a; import numpy as npdef sigmoid(x):return 1 / (1 np.ex…

WordPress 主题技巧:给文章页增加“谁来过”模块。

模块功能&#xff1a; 我个人目前在做一个电影类的网站&#xff0c;在开发文章页的模版时候&#xff0c;突然觉得给文章页增加一个“谁对本电影感兴趣”的功能模块可能会比较有趣&#xff0c;这个功能有点类似于‘足迹’的感觉&#xff0c;用户可以通过这个功能&#xff0c;发…

Log4j的原理及应用详解(三)

本系列文章简介&#xff1a; 在软件开发的广阔领域中&#xff0c;日志记录是一项至关重要的活动。它不仅帮助开发者追踪程序的执行流程&#xff0c;还在问题排查、性能监控以及用户行为分析等方面发挥着不可替代的作用。随着软件系统的日益复杂&#xff0c;对日志管理的需求也日…

前端的页面代码

根据老师教的前端页面的知识&#xff0c;加上我也是借鉴了老师上课所说的代码&#xff0c;马马虎虎的写出了页面。如下代码&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Title</ti…

k8s 部署 metribeat 实现 kibana 可视化 es 多集群监控指标

文章目录 [toc]环境介绍老(来)板(把)真(展)帅(示)helm 包准备配置监控集群获取集群 uuid生成 api_key配置 values.yaml 配置 es 集群获取集群 uuid 和 api_key配置 values.yaml 查看监控 缺少角色的报错 开始之前&#xff0c;需要准备好以下场景 一套 k8s 环境 k8s 内有两套不同…

PostgreSQL 导入 .gz 备份文件

要在PostgreSQL中导入.gz压缩的备份文件&#xff0c;你需要先解压缩该文件&#xff0c;然后使用psql工具导入。以下是步骤和示例代码&#xff1a; 解压缩备份文件&#xff1a; gunzip your_backup_file.gz 确保你有足够的权限来导入数据库。 使用psql导入解压后的文件&…

电脑如何重新分盘——保姆级教程

方法一&#xff1a; 通过此电脑&#xff0c;鼠标右键点击此电脑&#xff0c;点击管理 点击磁盘管理进入 二&#xff0c;磁盘分区 我这里选择的是“磁盘0”的C盘进行操作&#xff0c;一般新电脑拿到手的时候都处于这么一个状态&#xff0c;只有一个磁盘分区。现在我要把C盘拆分…

怎么知道服务器100M带宽可以支持多少人访问?

服务器100M带宽能够支持多少人访问取决于多种因素&#xff0c;包括但不限于网页大小、用户行为、数据传输类型以及预期的使用模式。以下是一些评估100M带宽可以支持多少人访问的考虑因素&#xff1a; 1. 网页大小&#xff1a; - 假设平均网页大小为1MB(虽然实际情况可能更小或更…

从汇编层看64位程序运行——参数传递的底层实现

大纲 小于等于6个参数一个参数总结 两个参数总结 三个参数总结 四个参数总结 五个参数总结 六个参数总结 大于6个参数七个参数总结 在32位系统中&#xff0c;参数的传递主要依靠栈来进行。那么64位系统上&#xff0c;是否依旧符合这个规则呢&#xff1f;答案是“不是”。64位系…

Redis6.2.1版本集群新加副本

测试数据 通过redis-benchmark生成测试数据 ./bin/redis-benchmark -h 172.31.4.18 -p 6381 -a Redis_6.2.1_Sc --cluster -t set -d 128 -n 10000000 -r 100000000 -c 200新加节点 172.31.4.18:6381> AUTH Redis_6.2.1_Sc OK172.31.4.18:6381> cluster meet 172.31.4…

Vue 组件之间的通信方式

Vue 组件之间的通信方式有多种&#xff0c;可以根据具体场景和需求选择合适的方式&#xff1a; Props / Events&#xff1a; 父组件通过 props 向子组件传递数据&#xff0c;子组件通过事件&#xff08;$emit&#xff09;向父组件发送消息。 适用于父子组件之间…

FFmpeg开发环境搭建

FFmpeg是音视频开发必备的库&#xff0c;也是唯一的库。本文主要讲解在ubuntu22和macOS14环境下的编译安装。 为什么要自己编译呢&#xff1f;其中一个很重要的原因就是ffmpeg在编译时可以加入很多插件&#xff0c;这种特定的库网络上可能找不到编译好的版本&#xff0c;另外如…

uniapp引入 uview( HBuilder 和 npm 两种安装方式) #按需引入

方式一、HBuilder 安装 uview 1.1. HBuider安装-链接-》》 1.2. 在uni.scss 中引入 import "uni_modules/uview-ui/theme.scss";1.3. main.js 引入&#xff08;import Vue from ‘vue’ 下面&#xff09; import uView from "uni_modules/uview-ui"; V…

在自定义总线下注册设备

1、自定义总线下注册设备 //my_bus_dev.c #include<linux/module.h> #include<linux/init.h> #include<linux/kernel.h> #include<linux/kobject.h> #include<linux/slab.h> #include<linux/sysfs.h> #include<linux/device.h> #in…

solidity实战练习3——荷兰拍卖

//SPDX-License-Identifier:MIT pragma solidity ^0.8.24; interface IERC721{function transFrom(address _from,address _to,uint nftid) external ; }contract DutchAuction { address payable immutable seller;//卖方uint immutable startTime;//拍卖开始时间uint immut…

Eureka 介绍与使用

Eureka 是一个开源的服务发现框架&#xff0c;它主要用于在分布式系统中管理和发现服务实例。它由 Netflix 开发并开源&#xff0c;是 Netflix OSS 中的一部分。 使用 Eureka 可以方便地将新的服务实例注册到 Eureka 服务器&#xff0c;并且让其他服务通过 Eureka 服务器来发现…

今日科技圈最新时事新闻(2024年7月12日

一、智能硬件与电子产品 小米Redmi G Pro 2024游戏本新版本发布 发布时间&#xff1a;7月12日上午10点产品亮点&#xff1a; 搭载英特尔酷睿i7-14650HX处理器&#xff0c;拥有16个核心和24个线程&#xff0c;性能释放高达130W。配备140W满血释放的RTX 4060显卡&#xff0c;提…

属于马云的时代结束了

关注卢松松&#xff0c;会经常给你分享一些我的经验和观点。 马云突然回国了&#xff0c;还出现在阿里巴巴的大厦里。大家都非常激动&#xff0c;阿里沸腾了&#xff0c;整个杭州&#xff0c;甚至全网都沸腾了&#xff0c;日本慌了&#xff0c;美国坐不住了&#xff0c;欧洲陷…

目前分布式光纤测温系统的主流架构有哪些?

分布式光纤测温技术的主流架构&#xff0c;历经多个阶段的发展和演变&#xff0c;每种架构都有其独特的特点和优势。回顾过去的发展历程&#xff0c;我们可以看到三种主要架构的演进&#xff0c;每一次创新都在不同程度上推动了技术的进步和市场的发展。 首先&#xff0c;2005…