繁体   English   中英

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

[英]How to measure thread time for progress bar?

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

进度条

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测。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM