简体   繁体   English

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

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

I want to follow a thread progress. 我想关注线程进度。 I have already implemented progress bar graphically, but I am wondering how to efficiently measure progress of the thread in a real time. 我已经以图形方式实现了进度条,但是我想知道如何实时有效地测量线程的进度。

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());
}

and somewhere in a game loop, example usage 在游戏循环中的某处,示例用法

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 });

I know how to measure execution time: 我知道如何衡量执行时间:

uint t1 = getTime();
//... do sth
uint t2 = getTime();
uint executionTime = t2 - t1;

but of course progress bar will be updated after execution, so it won't be shown in a real time. 但是进度条当然会在执行后进行更新,因此不会实时显示。

Should I use a new thread? 我应该使用新线程吗? Are there any other methods to do this? 还有其他方法吗?

All you can do for a progress bar is show an estimate or how long you have progressed based on the work you have already done (with an estimate (or maybe exact knowledge) of the total work to do. 对于进度条,您所能做的就是根据您已经完成的工作(带有要完成的全部工作的估计(或确切的知识))显示估计或完成了多长时间。

All you know is what work has been done and the time that took. 您所知道的是已完成的工作和所花费的时间。 The time it takes to do everything is always going to be an estimate. 完成所有工作所需的时间始终是估算值。 You can usually do pretty good by basing the estimate on the already completed work, but not always. 通常,您可以通过基于已经完成的工作进行估算来做得很好,但并非总是如此。

Making an exact progress bar is (in most cases) Impossible. (在大多数情况下)不可能制作准确的进度条。 Best you can do is a guesstimate. 您能做的最好的就是guess测。

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

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