C++ fwrite calculating elapsed Time

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 appreciate some help
Thanks,
--Rudy
clock() is measuring the CPU time used by your code. While fwrite() is executing, your code is waiting, so your CPU time is not incrementing.

Use wall clocks: high_resolution_clock::now() from C++, microsec_clock::local_time() from Boost, clock_gettime() from POSIX, etc.
Last edited on
Something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
using namespace std::chrono ; // #include <chrono>
typedef typename std::conditional< high_resolution_clock::is_steady,
                                   high_resolution_clock,
                                   steady_clock >::type clock_type ;

const auto start_time = clock_type::now() ;

unsigned long long size_t = fwrite(send, 1, transfer_size*sizeof(unsigned long long), wpFile);
fflush(wpFile) ;

const auto end_time = clock_type::now() ;

auto elapsed_milliseconds = duration_cast<milliseconds>( end_time - start_time ).count() ;
Last edited on
Thanks a lot,
How do I find #include <chrono>?
I found this
http://sccn.ucsd.edu/~arno/download/chrono/chrono.h

also, I found and added this:
http://sccn.ucsd.edu/~arno/download/chrono/chrono.c

But when I included these, I started getting bunch of errors!

What is the right way of adding #include <chrono>?

Thanks,
--Rudy
> How do I find #include <chrono>

It is a standard C++ header.
http://liveworkspace.org/code/3NjK1W$2


I am using Visual Studio 2010.
And the type of application that I am using is a Window form application.
My code so far work fine, once I add
#include <chrono>
It complains saying : error C1083: Cannot open include fine 'chrono'

I even tried <chrono.h>
But the same thing!!!

Any idea how to get around this problem?

Thanks,
--Rudy

> And the type of application that I am using is a Window form application.

The CLI has a clock that ticks at 100 nano second intervals; use that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "stdafx.h"
using namespace System;

#include <iostream>
#include <cstdlib>
#include <cmath>

int main()
{

    auto start_time = DateTime::Now ;

    // put the operation to time here
    int r = 0 ;
    for( int i=0 ; i < 1000000 ; ++i )
        r += int( std::ceil( std::sqrt( double( std::rand() ) ) ) ) % 3 ;

    auto end_time = DateTime::Now ;

    // ticks are in 100 nano second intervals
    auto elapsed_milliseconds = ( end_time.Ticks - start_time.Ticks ) / 10000 ;

    std::cout << elapsed_milliseconds << " milliseconds.\n" ;

    Console::ReadLine() ;
    return r ;
}

I tired this, but still not successful.
I put the code above and my operation is this:

unsigned long long size_t = fwrite(send, 1, transfer_size*sizeof(unsigned long long), wpFile);

whether I do 4KB of data of 512KB of data the answer is still 1ms.

I really think there is no way for windows to have a lower resolution of 1ms.


The issue is that I can put a for loop around my operation to do let's say 1000 times each.
But the problem is that since EACH single operation (whether 4KB or 512KB) is less than 1ms, and is rounded to 1ms, and therefore looping it also results in the same time for both 4KB or 64KB.

Is there anyway that I can measure anything lower than 1ms?

There is. Look into http://msdn.microsoft.com/en-us/library/windows/desktop/ms644904(v=vs.85).aspx and http://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx which get the CPU's ticks and the CPU's frequency, divide one by the other and you'll get a very very precise timer.
Assuming you're doing this to profile... you're approaching this problem the wrong way.

Is there anyway that I can measure anything lower than 1ms?


Even if there is, the results will be unreliable.

If you're doing this for profiling puroses, any time under 1 second (read: a full second), shouldn't be taken very seriously. Modern computers are multitasking monsters, and there are too many outside factors that can have subtle impacts on performance. When dealing with these fast speeds, those subtle impacts get exaggerated.

For example, code taking 1 ms longer to run due to outside process interference or disk usage or <insert other issue here>. When your entire run time is 1 second, that outside performance only accounts for 0.1% of the total execution time. When the entire run time is 2 ms, it accounts for 50%.


Rather that looking for a higher resolution timer... you probably should just increase your iteration count and/or read/write size.
Last edited on
PERFECT....

Thanks a lot,
QueryPerformanceCounter function and QueryPerformanceFrequency function actually worked fine.
In fact I am getting very different measures for writing a 4KB and 32KB, which is really nice.

Thank you guys,
--Rudy
Topic archived. No new replies allowed.