Richard Heathfield wrote:
santosh said:
> he*********@gmail.com wrote:
>>I have an application which writes huge number of bytes into the
binary files which is just some marshalled data.
int len = Data.size(); //arrary size
You seem to using C++.
The line you quoted sure looks like C to me (C99, admittedly).
>> for (int i = 0; i < len; ++i)
fwrite(&Data[i], 1, 1, f);
Can't you write more than one byte at a time? This is probably an
inefficient method for writing large number of bytes.
How can you tell? Have you measured - and on *his* system? If the stream
is buffered, it won't make so much as a halfpenny-worth of difference
whether he writes one object at a time or all len objects at once.
<snip>
You are right. However it seemed like an inefficient method when a single
fwrite call could do the same job. As you note _if_ the stream is buffered,
there wouldn't be any difference worth noting between the two methods, but
there is a small penalty to be paid for making N calls to fwrite instead of
one call. Below are the measurements on my machine. As you note, the
difference in negligible.
$ ./fwrt_test
Usage: ./fwrt_test N
If N is 0, single fwrite call
If N is 1, one fwrite call for each data element
$ ./fwrt_test 0
wrote 40000000 bytes (10000000 items, 4 bytes/item)
time taken: 0.120000
$ ./fwrt_test 1
wrote 40000000 bytes (10000000 items, 4 bytes/item), one item at a time.
time taken: 0.630000
$ ./fwrt_test 0
wrote 40000000 bytes (10000000 items, 4 bytes/item)
time taken: 0.120000
$ ./fwrt_test 1
wrote 40000000 bytes (10000000 items, 4 bytes/item), one item at a time.
time taken: 0.600000
$ ./fwrt_test 0
wrote 40000000 bytes (10000000 items, 4 bytes/item)
time taken: 0.120000
$ ./fwrt_test 1
wrote 40000000 bytes (10000000 items, 4 bytes/item), one item at a time.
time taken: 0.630000
code follows:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N_ELEMENTS 10000000UL /* ten million elements */
void usage(char *prg_name)
{
fprintf(stdout, "Usage: %s N\nIf N is 0, single fwrite call\n"
"If N is 1, one fwrite call for each data element\n",
prg_name
);
return;
}
int main(int argc, char **argv)
{
char choice;
int *buf;
size_t bsize, ctr;
clock_t begin, end;
FILE *fp;
if (argc < 2) {
usage(argv[0]);
exit(EXIT_FAILURE);
}
else {
choice = argv[1][0];
if (choice != '0' && choice != '1') {
usage(argv[0]);
exit(EXIT_FAILURE);
}
}
/* allocate buffer, fill with pseudo-random integers */
bsize = N_ELEMENTS * sizeof *buf;
if ((buf = malloc(bsize)) == NULL) {
fprintf(stderr, "malloc failed - %lu bytes.\n", (unsigned
long)bsize);
exit(EXIT_FAILURE);
}
else
for (ctr = 0; ctr < N_ELEMENTS; ctr++) buf[ctr] = rand();
/* open a disk file */
if ((fp = fopen("outfile", "w")) == NULL) {
fprintf(stderr, "fopen failed.\n");
free(buf);
exit(EXIT_FAILURE);
}
/* write to file either at once or one element at a time; time it. */
if (choice == '0') {
begin = clock();
if (fwrite(buf, sizeof *buf, N_ELEMENTS, fp) != N_ELEMENTS) {
fprintf(stderr, "fwrite failed.\n");
fclose(fp);
free(buf);
exit(EXIT_FAILURE);
}
end = clock();
fprintf(stdout, "wrote %lu bytes (%lu items, %lu bytes/item)\n"
"time taken: %f\n", (unsigned long)bsize, N_ELEMENTS,
(unsigned long)sizeof *buf,
(double)(end - begin) / CLOCKS_PER_SEC
);
}
else {
begin = clock();
for (ctr = 0; ctr < N_ELEMENTS; ctr++) {
if (fwrite(buf+ctr, sizeof *buf, 1, fp) != 1) {
fprintf(stderr, "fwrite failed.\n");
fclose(fp);
free(buf);
exit(EXIT_FAILURE);
}
}
end = clock();
fprintf(stdout, "wrote %lu bytes (%lu items, %lu bytes/item), one"
" item at a time.\ntime taken: %f\n", (unsigned long)bsize,
N_ELEMENTS, (unsigned long)sizeof *buf,
(double)(end - begin) / CLOCKS_PER_SEC
);
}
fclose(fp);
free(buf);
return EXIT_SUCCESS;
}