Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. having trouble timing my program...

having trouble timing my program...

Scheduled Pinned Locked Moved C / C++ / MFC
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Moochie5
    wrote on last edited by
    #1

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

    N 1 Reply Last reply
    0
    • M Moochie5

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

      N Offline
      N Offline
      Neville Franks
      wrote on last edited by
      #2

      %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"

      M 1 Reply Last reply
      0
      • N Neville Franks

        %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"

        M Offline
        M Offline
        Moochie5
        wrote on last edited by
        #3

        Yes that was it, thanks a lot!

        M 1 Reply Last reply
        0
        • M Moochie5

          Yes that was it, thanks a lot!

          M Offline
          M Offline
          Moochie5
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups