如何计算总时间剩余时间. [英] How to calculate total time & remaining time.

查看:108
本文介绍了如何计算总时间剩余时间.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

time_t now_t, start_t;
time(&start_t);
wipe_status(drive_path,0L, 0L);
while((long) ((long)file_num)*((long)DEFAULT_FILE_SIZE)<=a)
{
       time(&now_t);
       wipe_status(drive_path,file_num, difftime(now_t, start_t));
}



void wipe_status(char * drive_path,long file_num, long gap)
{
       
   p=a/20;
    fprintf(stdout, "\nInfo: %ld Mb written in %5.2f min\n", (long) ((long)file_num)*((long)DEFAULT_FILE_SIZE), (float)((float)file_num*(.0298)));
    printf("\n Total Time:%5.2f min\n",(float)p*(.0298));
    printf("\n Remaining Time:%5.2f min\n",((float)p*(.0298)-(float)((float)file_num*(.0298))));     

 fprintf(stdout, "\nProgress ............%ld Percent ",  ((100*file_num)/p));
 fprintf(stdout, "\nRemaining ...........%ld Percent ",  (100-((100*file_num)/p)));
      
 fflush(stdout);
}


这是错误的总时间&剩余时间.

请帮帮我.

谢谢


This is given wrong total time & remaining time.

Please help me.

Thanks

推荐答案

这很简单,您需要做的是:
到目前为止所花费的时间
完成百分比

现在,我们可以算出剩下的时间了.
考虑一下,如果我们完成了20%(1/5)的处理,那么总时间将是当前处理时间的5倍.如果我们完成了50%(1/2)的处理,那么总时间将是当前时间的两倍.
如果您注意到那里的趋势,则总时间将为1/fraction_complete.

This is quite simple, all you need is:
The time taken so far
The percentage complete

Now, we can work out how long is left.
Think about this for a minute, if we have done 20% (1/5th) of the processing, then the total time is going to be 5x our current processing time. if we have done 50% (1/2) of the processing, then the total time will be twice our current time.
If you notice the trend there, the total time is going to be 1/fraction_complete.

//These variables would be calculated somewhere, this is just for demonstration
double nPercentage = 0.2; //This is 20% complete
double nElapsed = 5.0; //Taken us 5 minutes to complete that 20% (or 5 seconds, whatever it doesn't matter)
//The actualy formula
double nTotalTime = (1.0 / nPercentage) * nElapsed;
double nRemaining = nTotalTime - nElapsed;



只要您保持一致,时间单位就不重要.

如果您愿意,这是我用于测试的代码:



Time units are not important, as long as you are consistent.

This is the code I used for testing, if you care:

#include <stdio.h>
#include <Windows.h>

//This uses the High Precision Event Timers. This could be overkill for estimating time remaining on copying files and things like that, but it is what I had lying around.
void TimerStart(LARGE_INTEGER *pSpec) {
	QueryPerformanceCounter(pSpec);
}
 
double TimerQuery(LARGE_INTEGER *pSpec) {
	LARGE_INTEGER li, liFreq;
	QueryPerformanceCounter(&li);
	QueryPerformanceFrequency(&liFreq); //If you want to optimise, do this once and save the result
	return (li.QuadPart - pSpec->QuadPart) / (double)liFreq.QuadPart;
}

int main() {
	LARGE_INTEGER start;
	TimerStart(&start);
	for (int i = 0; i < 200; ++i) {
		Sleep(100);
		double nPercentage = i / 200.0; //Ranges from 0-1
		double nElapsed = TimerQuery(&start); //In seconds
		double nTotalTime = (1.0 / nPercentage) * nElapsed;
		double nRemaining = nTotalTime - nElapsed;
		printf("%2.1f%% complete, Estimated %f seconds remaining\n", (nPercentage * 100.0), nRemaining);
	}
	printf("Total execution time: %f seconds\n", TimerQuery(&start));
	return 0;
}




这是一种简单的方法.只要大约花费相同的时间来完成任何1%的操作,它就会是准确的.
例如,复制文件.如果使用复制/剩余文件数作为百分比,则估计1GB文件与1KB文件花费相同的时间.在这种情况下,也可以采用长期平均值,或者使用数据大小而不是文件计数作为百分比的度量.
由于您使用的是数据大小,因此这不是问题,但是如果您需要在其他任何阶段进行计时,请记住这一点.




This is a simple approach. It will only be accurate, so long as it takes roughly the same time to complete any 1% of operations.
For example, copying files. If you use the number of files copied/remaining as the percentage then a 1GB file will be estimated as taking the same time to copy as a 1KB file. In this case either take a long term average as well, or use data size, rather than file count as the metric of percentage.
Since you are using data size, this should not be an issue, but keep it in mind if you need to do timings at any other stage.


下一行的目的是什么(什么是a)?
What is the purpose of the following line (and what is a)?
p=a/20


还请说明您所使用的公式背后的原因.变量file_num的意义是什么?为什么要乘以0.298?


Please also explain the reasoning behind the formulas you are using. What is the significance of the variable file_num? Why do you multiply by 0.298?


感谢您的答复.

但是
通过上面的示例,总执行时间是正确的.但是对于大尺寸(大于2 gb),剩余时间是错误的.剩余时间应按降序排列,但
当代码开始执行时,剩余时间按递增顺序开始.

请帮帮我.

谢谢
thanks for giving reply.

but
By avove example Total execution time is right.but remaining time is wrong for large size(more than 2 gb).remaining time should be decreasing order but
when code excute in begain the remaining timing in increasing order.

Please help me.

thanks


这篇关于如何计算总时间剩余时间.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆