CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2011
    Posts
    19

    fwrite calculating elapsed Time

    Hi All,
    I have a question.
    I would like to actually have some measure of roughly how long it takes to do a fwrite to a drive.

    when i do the following:


    clock_t begin = clock();
    unsigned long long size_t = fwrite(send, 1, transfer_size*sizeof(unsigned long long), wpFile);
    clock_t end = clock();
    double long elapsed_secs = double long(end - begin) / CLOCKS_PER_SEC;

    Unfortunately, I don't get any different result for different transfer size!!!

    My guess is that the clock_t , once it issues a fwrite command, some how stops its measurement, and it comes back again, when I am already done with fwrite.
    I do get the almost same measure, whether my transfer size is 32KB Byte or 16MB !
    Which I was indeed expecting to see a huge difference.
    I wouldn't really want the exact real timing measure (well off course it will be nice to know); and all I care about is to see some difference in time whether I am doing KB transfer vs MB transfer.

    Does any one know of any other function that will give me some rough measurement of the actual time being elapsed for fwrite function?

    I would really appreciate some help here.

    Thanks,
    --Rudy

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: fwrite calculating elapsed Time

    Quote Originally Posted by rudy01 View Post
    I would like to actually have some measure of roughly how long it takes to do a fwrite to a drive.

    when i do the following:


    clock_t begin = clock();
    unsigned long long size_t = fwrite(send, 1, transfer_size*sizeof(unsigned long long), wpFile);
    clock_t end = clock();
    double long elapsed_secs = double long(end - begin) / CLOCKS_PER_SEC;

    Unfortunately, I don't get any different result for different transfer size!!!
    There are two issues here. First, to get more precise timings have a look at the profiling FAQ. Make sure that you time a fully optimized build, else your timings will not mean anything.
    Second, fwrite may not actually write anything to disk; the data could just be stored in a buffer. To disable buffering, you can call setbuf or fflush. Still, writing to disk is likely to give you widely varying timings. You can only rely on average values when writing lots of data over a longer period of time.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: fwrite calculating elapsed Time

    Quote Originally Posted by rudy01 View Post
    Hi All,
    I have a question.
    I would like to actually have some measure of roughly how long it takes to do a fwrite to a drive.
    When it comes to disk writes, writing code to find the exact time to write to the disk using fwrite is rarely helpful. I once tried this, and wasted a lot of time with rather useless statistics in the long run.

    Disk accesses can be volatile -- what can be slow in one run may be faster in another run, or even on different drives and/or machines (a drive may have cached the writes, etc.). Add to that what D_drmmr stated concerning buffering.

    You might as well write two applications, run them with a lot of data, and see which one finishes first most of the time. You should only test on a full-optimized build (as was already stated), and on the drive/hardware that you really intend to run the program on.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Aug 2011
    Posts
    19

    Re: fwrite calculating elapsed Time

    Thanks for all the help guys.
    It actually worked. "QueryPerformanceCounter" and "QueryPerformanceFrequency" are really what I need.
    They are giving me a very precise different timing measures for even small differences in the transfer size.

    And, off course, thanks for mentioning setbuf and fflush. I had totally forgot about the cashing.
    Thanks again for your time,
    --Rudy

  5. #5
    Join Date
    Nov 2003
    Posts
    1,902

    Re: fwrite calculating elapsed Time

    Here is some file-throughput code I posted once: http://forums.codeguru.com/attachmen...0&d=1315577055

    gg

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured