Timer to measure file creation time

Jan 9, 2009 at 7:01pm
Hi everyone,

Over the last few months on and off i've been in contact with a friend through email and have made a little C++ program that I still don't quite grasp, (this is the first time i've done this language) What I have so far is a program that does let you create multiple files of a specified size, file 0,1,2 etc. What I need is someway of using a timer to state the amount of time this operation took after the file is made. It would be brilliant if there was someway of stating it in milliseconds, but any accurate timer will do.

e.g file 0 is made it took 1 second to make this file

Any help or suggestions is appreciated. I messed around with simple timers a month ago but could not get an accurate time displayed and got more and more confused :-{ Everytime file creation looped the time increased dramatically and it was an obvious error

__________________________________________________________________________________



#include <iostream>
#include <fstream> //For file stuff
#include <stdlib.h>
#include <string.h>

#include <ctime> // For time()
#include <cstdlib> // For srand() and rand()

using namespace std;

//ofstream::pos_type size;
long size_kbs; //This will be the size of the file in kilobytes
char *some_data; //A pointer of type char
int max_files;
char filename[11];
char filenum[5];


int main (int argc, char* argv[]) {

if (argc < 3 ){ //argc is the number of command line arguments. If less than 2, file size was not specified
size_kbs = 32;
max_files = 1; //Default one file if number not specified
cout <<"File size was not specified so default 32KB will be used!" <<endl;
}


else {
size_kbs = atol(argv[1]); //If the file size was specified on command line then set size_kbs to it. argv[1] is the first argument after program name.
max_files = atoi(argv[2]);

cout <<"File size will be " <<size_kbs <<" kilobytes." <<endl <<"Number of files will be " <<max_files <<"." <<endl;
}
some_data = new char[1024]; //Dynamically allocate array to be 1KB in size.
srand(time(0)); // Initialize random number generator.

//Generate a pseudo-random 8 bit number between 1 and ten
//Do this 1024 times and fill up the array some_data


for (int i=0; i<1024; i++) {
char random = (char)((rand() % 10) + 1);
some_data[i] = random;
}

for(int i=0; i<max_files; i++) {//open for loop

sprintf(filenum, "%d", i); //Had to use this instead of itoa as it turns out itoa is not standard

strcpy(filename, "file"); //strcpy can be dangerous as it there is no check to make sure the area copied to is big enough for data.
strcat(filename, filenum);//same goes for strcat.

ofstream file (filename, ios::out|ios::binary); //Open a file caled "file.bin"


if (file.is_open()) //If file open succeeds
{

file.seekp (0, ios::beg); //Go to beginning of file


//Next we put the required amount of data in the
file.
//some_data is 1024 bytes or 1 kilobyte in size
//Repeat size_kbs times e.g 8 times for a 8KB file


for (int i=0; i<size_kbs; i++) {
file.write (some_data, 1024);
}

cout <<filename <<" written." <<endl; //Notify that file is written
file.close(); //Close the file now we are finished with it.

}
else cout << "Unable to open file"; //If file open failed
}
delete[] some_data; //Release the memory used by some_data




return EXIT_SUCCESS; //finish
}






__________________________________________________________________________________

Jan 9, 2009 at 8:29pm
Since you've already included time.h you can use functions out of there.

for example:
1
2
3
4
5
6
7
t_time start, end;
start = time(NULL);

...

end = time(NULL);
cout << "Creating the file took " << end-start << " seconds" << endl;


There is another function called gettimeofday(). I have never used it, so i don't know much about it, but it stores the time in microseconds.
1
2
3
4
5
6
7
timeval start, end;
gettimeofday(&start, NULL);

...

gettimeofday(&end, NULL);
cout << "Creating the file took " << end.tv_sec*1000 + end.tv_usec - start.tv_sec*1000 - start.tv_usec << " microseconds" << endl;
Jan 9, 2009 at 8:31pm
I admit, the way to do it with clocks() is way more elegant.
Jan 9, 2009 at 8:34pm
gettimeofday() is not standard. It's a UNIX system call, if I'm not mistaken.
Jan 9, 2009 at 11:20pm
I've looked this up, and helios is right, it is a unix only call.
time.h in windows is only accurate to seconds, if you want milliseconds you'll have to use a library, or somewhat fudge it with ticks.
Jan 9, 2009 at 11:43pm
Except for clock(), all ctime functions have the same precision. clock() has a resolution of 1 ms in Windows, and of 1 us in Linux.
Jan 10, 2009 at 4:36pm
Thanks for the replies everyone, I will mess around with it :)
Jan 11, 2009 at 7:45pm
Have a look at GetFrequencyCounter() (I think it's called). There are a set of Frequency functions that offer a much higher resolution of accuracy. The ones used in gaming for getting the time between frames when rendering :)
Topic archived. No new replies allowed.