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

问题描述

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

推荐答案

//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.


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?



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

05-30 00:22
查看更多