having trouble timing my program...
-
I have to do a program that reads from a file and copies that text file to a new one and time how long the process takes to do so. The program copies the files correctly but does not display the time correctly. I will show the entire code since it is pretty small. Right now the duration is equal to finish time - start time / CLOCKS_PER_SEC. The output will say The process took 1202590843 seconds to complete, but when i step through it and put a watch on duration it shows the correct time like 11.567000000
#include #include #include #include #define BUFFER_LEN 200 // # of bytes to read/write // The producer process reads information from the file name // in_test then writes it to the file named out_test. void main(int argc, char *argv[]) { clock_t start,finish; double duration; char buffer[BUFFER_LEN + 1]; // CreateFile parameters DWORD dwShareMode = 0; // share mode LPSECURITY_ATTRIBUTES lpFileSecurityAttributes = NULL; // HANDLE hTemplateFile = NULL; // HANDLE sourceFile; // Source of pipeline DWORD numberOfBytesRead; LPOVERLAPPED lpOverlapped = NULL; // Not used here // WriteFile HANDLE sinkFile; DWORD numberOfBytesWritten; // Open source file start=clock(); //Start Timing the Process sourceFile = CreateFile ( "C://Constitution.txt", GENERIC_READ, dwShareMode, lpFileSecurityAttributes, OPEN_ALWAYS, FILE_ATTRIBUTE_READONLY, hTemplateFile ); if(sourceFile == INVALID_HANDLE_VALUE) { fprintf(stderr, "File Open operation failed\n"); ExitProcess(1); } // Open the sink file sinkFile = CreateFile ( "C://NewVersionConstitution.txt", GENERIC_WRITE, dwShareMode, lpFileSecurityAttributes, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, hTemplateFile ); if(sinkFile == INVALID_HANDLE_VALUE) { fprintf(stderr, "File Open operation failed\n"); ExitProcess(1); } // Main loop to copy the file while ( ReadFile( sourceFile, buffer, BUFFER_LEN, &numberOfBytesRead, lpOverlapped ) && numberOfBytesRead > 0 ) { WriteFile(sinkFile, buffer, BUFFER_LEN, &numberOfBytesWritten, lpOverlapped); } //while (BUFFER_LEN == 200); // Terminating. Close the sink and source files CloseHandle(sourceFile); CloseHandle(sinkFile); finish=clock(); //End timing of process //duration=(double)(finish-start) / 10000; duration=(double)(finish-start) / CLOCKS_PER_SEC; printf("The process took %d ",duration);
-
I have to do a program that reads from a file and copies that text file to a new one and time how long the process takes to do so. The program copies the files correctly but does not display the time correctly. I will show the entire code since it is pretty small. Right now the duration is equal to finish time - start time / CLOCKS_PER_SEC. The output will say The process took 1202590843 seconds to complete, but when i step through it and put a watch on duration it shows the correct time like 11.567000000
#include #include #include #include #define BUFFER_LEN 200 // # of bytes to read/write // The producer process reads information from the file name // in_test then writes it to the file named out_test. void main(int argc, char *argv[]) { clock_t start,finish; double duration; char buffer[BUFFER_LEN + 1]; // CreateFile parameters DWORD dwShareMode = 0; // share mode LPSECURITY_ATTRIBUTES lpFileSecurityAttributes = NULL; // HANDLE hTemplateFile = NULL; // HANDLE sourceFile; // Source of pipeline DWORD numberOfBytesRead; LPOVERLAPPED lpOverlapped = NULL; // Not used here // WriteFile HANDLE sinkFile; DWORD numberOfBytesWritten; // Open source file start=clock(); //Start Timing the Process sourceFile = CreateFile ( "C://Constitution.txt", GENERIC_READ, dwShareMode, lpFileSecurityAttributes, OPEN_ALWAYS, FILE_ATTRIBUTE_READONLY, hTemplateFile ); if(sourceFile == INVALID_HANDLE_VALUE) { fprintf(stderr, "File Open operation failed\n"); ExitProcess(1); } // Open the sink file sinkFile = CreateFile ( "C://NewVersionConstitution.txt", GENERIC_WRITE, dwShareMode, lpFileSecurityAttributes, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, hTemplateFile ); if(sinkFile == INVALID_HANDLE_VALUE) { fprintf(stderr, "File Open operation failed\n"); ExitProcess(1); } // Main loop to copy the file while ( ReadFile( sourceFile, buffer, BUFFER_LEN, &numberOfBytesRead, lpOverlapped ) && numberOfBytesRead > 0 ) { WriteFile(sinkFile, buffer, BUFFER_LEN, &numberOfBytesWritten, lpOverlapped); } //while (BUFFER_LEN == 200); // Terminating. Close the sink and source files CloseHandle(sourceFile); CloseHandle(sinkFile); finish=clock(); //End timing of process //duration=(double)(finish-start) / 10000; duration=(double)(finish-start) / CLOCKS_PER_SEC; printf("The process took %d ",duration);
%d in printf is for int's etc. not doubles. Have a look at the format spec for printf and see what to use for doubles. Might be %f but you need to check. Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"
-
%d in printf is for int's etc. not doubles. Have a look at the format spec for printf and see what to use for doubles. Might be %f but you need to check. Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"
-
The program now outputs duration however, this is something I dont understand. If I run the program normally, meaning without debugging(stepping through it) it says the process takes 0.08000 seconds but if I insert a breakpoint when the file is closing and debug it, it will say the process takes 24.795 secs to do. When I was debugging I put a watch on duration and that was the 24.795 was the correct output. If I run it without the debugging it says 0.08. How or why does debugging change the value of the variable duration?