Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
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
Posted
Updated 27-Oct-11 3:02am
v2

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.

C++
//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:
C++
#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;
}


EDIT:
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.
 
Share this answer
 
v2
Comments
Richard MacCutchan 27-Oct-11 12:17pm    
Excellent answer and advice.
What is the purpose of the following line (and what is a)?
C++
p=a/20

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?
 
Share this answer
 
Comments
Albert Holguin 27-Oct-11 11:01am    
OP posted as solution:
int p;
long a;

file_num is copy of files in the drive
a=lpTotalNumberOfFreeBytes/(1024*1024);
freebytes in the drive.
and copy the files in whole drive.
default file size=20 mb.
p=a=/20;
total time=(.298)*p.
where .298 min is copy time for 20 mb means 1 file_num.

but it is giving wrong total time.
Richard MacCutchan 27-Oct-11 11:44am    
I am not surprised you are getting what you consider the wrong values, since your algorithm is based on assumptions and not reality. If you are trying to calculate the time taken (or will be taken) to copy some data from one place to the other, then you need to sample the time stamps after each block or set of blocks and adjust your values according to what time has been taken. Using an arbitrary value of .298 is totally meaningless.
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
 
Share this answer
 
Comments
Richard MacCutchan 28-Oct-11 8:41am    
Don't post questions as solutions! Use the button under the message that you are replying to; it's not clear who this message is for.
tnpatel 28-Oct-11 10:26am    
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys types.h="">
#include <sys\stat.h>
#include "windows.h"
#include "global.h"
#include "wiper.c"
int totalfiles=0;
int p;
int q;
long a;
char stop_wiping = 0;
//double dif;
LARGE_INTEGER start;
LARGE_INTEGER start1;
double nRemaining;
double nTotalTime;
double nElapsed;
double nPercentage;
double nRemaining1;
double nTotalTime1;
double nElapsed1;
double nPercentage1;
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;
}


void usage()
{
fprintf(stdout, "\n%s - %s\nUsage: %s DRIVE_PATH\n", PROGRAM_NAME, PROGRAM_VER, PROGRAM_NAME);
}


int get_opts(int argc, char *argv[], char drive_path[256])
{
int i, p;
char ch;

if (argc != 2){
usage();
return -1;
}
strcpy(drive_path, argv[1]);

p = strlen(drive_path) - 1;
//remove final slash if exists
if (drive_path[p] == '/' || drive_path[p]=='\\')
{
drive_path[p] = '\0';
}

return 0;
}


int can_write(char * drive_path){

unsigned char filepath[256];
int r = 0;

sprintf(filepath, "%s/%s", drive_path, TEST_FILENAME);
FILE * f = fopen(filepath, "w+b");

if (f == NULL){
r = -1;
goto exit;
}

if (fputs(filepath, f) < 0){
r = -1;
goto exit;
}

exit:
if (f != NULL){
fclose(f);
if (remove(filepath) != 0){
r = -1;
}
}

return r;
}
/////////////////////////////////////
#if (OS == 1)
long abort_listener(){
getchar();
stop_wiping = 1;
return 0;
}
#else
void * abort_listener(void * args){
getchar();
stop_wiping = 1;
return 0;
}
#endif


void clear_console_screen()
{
#if (OS == 1)
system("CLS");
#else
system("clear");
#endif
}
void wipe_status1(long file_num, long gap)
{
// clear_console_screen();
q=a/20;
// printf(" file no :%d",q);
fprintf(stdout, "%s - %s\n\nStatus: Deleting process in progress [Don't Press ENTER to stop]", PROGRAM_NAME, PROGRAM_VER);
printf("\n%2.1f%% complete, Estimated %f seconds remaining\n", (nPercentage1 ), nRemaining1);
// printf("Total execution time: %f min\n", (TimerQuery(&start1)/60));
fflush(stdout);
}
void delete_tmp_files(char * drive_path, long file_num)
{
unsigned char filepath[256];
long i;
time_t now_t1, start_t1;
time(&start_t1);
wipe_status1(0L, 0L);
fprintf(stdout, "\nStatus: Deleting temporary files.\n");

for(i=1; i<=file_num; i++){
sprintf(filepath, "%s/%s%ld", drive_path, FILE_PREFIX, i);
long b;
b=a/20;
nPercentage1 = (file_num *100.0)/ b;
// printf("\n file no :%d",p);
nElapsed1 = TimerQuery(&start1); //In seconds
nTotalTime1 = (100.0 / nPercentage1) * nElapsed1;
nRemaining1 = nTotalTime1 - nElapsed1;
if (remove(filepath) != 0){
fprintf(stdout, "\nError: Unable to remove temporary files.");
break;
}
}
}


void wipe_status(char * drive_path,long file_num, long gap)
{
clear_console_screen();
// printf("%d",a);
p=a/20;

// printf ("\n111It total time=%.2lf seconds.\n", p*dif );
fprintf(stdout, "%s - %s\n\nStatus: Wiping free space in progress [Press ENTER to stop]", PROGRAM_NAME, PROGRAM_VER);
fprintf(stdout, "\nNote: Don't stop with CTRL+C otherwise the temporary files will not be deleted");
fprintf(stdout, "\nInfo: %ld Mb written in %5.2f min\n", (long) ((long)file_num)*((long)DEFAULT_FILE_SIZE), ((float)gap/60));
// printf("\n Total Time:%5.2f min\n",(float)p*(.0
Andrew Brock 28-Oct-11 9:11am    
Probably best to start a new question, link back to this one. That way you can post your code.

My formula works, it is either the way you are calculating total and completed data transferred or you are overflowing an integer (an integer can hold a maximum file size of 2GB).

Please start a new question and post any relevant code.
tnpatel 28-Oct-11 10:28am    
thanks ...............

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900