gpt4 book ai didi

c++ - 如何测量进度条的线程时间?

转载 作者:行者123 更新时间:2023-12-02 10:23:50 26 4
gpt4 key购买 nike

我想关注线程进度。我已经以图形方式实现了进度条,但是我想知道如何实时有效地测量线程的进度。

进度条

template<typename T>
inline T Saturate(T value, T min = static_cast<T>(0.0f), T max = static_cast<T>(1.0f))
{
return value < static_cast<T>(min) ? static_cast<T>(min) : value > static_cast<T>(max) ? static_cast<T>(max) : value;
}

void ProgressBar(float progress, const Vector2& size)
{
Panel* window = getPanel();

Vector2 position = //some position
progress = Saturate(progress);

window->renderer->FillRect({ position, size }, 0xff00a5ff);
window->renderer->FillRect(Rect(position.x, position.y, Lerp(0.0f, size.w, progress), size.h), 0xff0000ff);

//progress will be shown as a %
std::string progressText;
//ToString(value, how many decimal places)
progressText = ToString(progress * 100.0f, 2) + "%";

const float textWidth = font->getWidth(progressText) * context.fontScale,
textX = Clamp(Lerp(position.x, position.x + size.w, progress), position.x, position.x + size.w - textWidth);
window->renderer->DrawString(progressText, Vector2(textX, position.y + font->getAscender(progressText) * context.fontScale * 0.5f), 0xffffffff, context.fontScale, *font.get());
}

在游戏循环中的某处,示例用法
static float prog = 0.0f;
float progSpeed = 0.01f;
static float progDir = 1.0f;
prog += progSpeed * (1.0f / 60.0f) * progDir;

ProgressBar(prog, { 100.0f, 30.0f });

我知道如何衡量执行时间:
uint t1 = getTime();
//... do sth
uint t2 = getTime();
uint executionTime = t2 - t1;

但是进度条当然会在执行后进行更新,因此不会实时显示。

我应该使用新线程吗?还有其他方法可以做到这一点吗?

最佳答案

对于进度条,您所能做的就是根据您已经完成的工作(带有要完成的全部工作的估计(或确切的知识))显示估计或完成了多长时间。

您所知道的是已完成的工作和所花费的时间。做所有事情所花费的时间总会是一个估计。通常,您可以根据已经完成的工作估算出不错的成绩,但并不总是如此。

(在大多数情况下)不可能制作精确的进度条。您能做的最好的就是guess测。

关于c++ - 如何测量进度条的线程时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54913246/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com